This post describes how to convert the 'sdl-healthcheck' application to a Windows Service.
SDL-healthcheck is a Java application that creates a webservice around the SDL Web microservices in order to provide monitoring (heart beat) checks on them. Mark van der Wal wrote the application and provided startup and stop Shell scripts.
My requirement was to have the same application installable and executable as Windows Service.
Therefore, I had to wrap the Java application inside a service container and provide start, stop hooks for the service. I know that SDL Web Content Delivery microservices are also Java applications using Spring Boot, so my approach was to use the same mechanism to make the Java application run as Windows Service.
The service container used is 'procrun.exe' daemon from Apache Commons. Depending on the arguments passed to it, it can perform different Windows Service functionality such as install, remove service, start/stop service.
I ended up hacking together the installService PowerShell scripts from the CD microservices and they look like the ones below. One trick was to provide a StopClass and StopParams to the service. If a service should finish gracefully, then it should be programmed to do so. But I saw Mark was simply killing the process on his stop.sh script, so I ended up just calling java.lang.System.exit() method. This ensues the services stops when it is requested to do so.
installService.ps1
uninstallService.ps1
The following script will stop the service and then remove it:
SDL-healthcheck is a Java application that creates a webservice around the SDL Web microservices in order to provide monitoring (heart beat) checks on them. Mark van der Wal wrote the application and provided startup and stop Shell scripts.
My requirement was to have the same application installable and executable as Windows Service.
Therefore, I had to wrap the Java application inside a service container and provide start, stop hooks for the service. I know that SDL Web Content Delivery microservices are also Java applications using Spring Boot, so my approach was to use the same mechanism to make the Java application run as Windows Service.
The service container used is 'procrun.exe' daemon from Apache Commons. Depending on the arguments passed to it, it can perform different Windows Service functionality such as install, remove service, start/stop service.
I ended up hacking together the installService PowerShell scripts from the CD microservices and they look like the ones below. One trick was to provide a StopClass and StopParams to the service. If a service should finish gracefully, then it should be programmed to do so. But I saw Mark was simply killing the process on his stop.sh script, so I ended up just calling java.lang.System.exit() method. This ensues the services stops when it is requested to do so.
installService.ps1
$jvmoptions = "-Xrs", "-Xms48m", "-Xmx128m", "-Dfile.encoding=UTF-8" $currentFolder = Get-Location $name="SDLHealthcheck" $displayName="SDL Healthcheck" $description="SDL Healthcheck" $dependsOn="" $path=$PSScriptRoot cd $path\.. $rootFolder = Get-Location $procrun="procrun.exe" $application=$path + "\" + $procrun $fullPath=$path + "\" + $procrun $arguments = @() $arguments += "//IS//" + $name $arguments += "--DisplayName=" + $displayName $arguments += "--Description=" + $description $arguments += "--Install=" + $fullPath $arguments += "--Jvm=auto" $arguments += "--Startup=auto" $arguments += "--LogLevel=Info" $arguments += "--StartMode=jvm" $arguments += "--StartPath=" + $rootFolder $arguments += "--StartClass=org.springframework.boot.loader.JarLauncher" $arguments += "--StartParams=start" $arguments += "--StopMode=jvm" $arguments += "--StopClass=java.lang.System" $arguments += "--StopParams=exit" $classpath = ".\bin\*;.\lib\*;.\addons\*;.\config" foreach ($folder in Get-ChildItem -path $rootFolder -recurse | ?{ $_.PSIsContainer } | Resolve-Path -relative | Where { $_ -match 'services*' }) { $classpath = $folder + ";" + $folder + "\*;" + $classpath } $arguments += "--Classpath=" + $classpath # Check script is launched with Administrator permissions $isAdministrator = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator") if ($isAdministrator -eq $False) { $Host.UI.WriteErrorLine("ERROR: Please ensure script is launched with Administrator rights") Exit } Try { Write-Host "Installing '$name' as windows service..." -ForegroundColor Green if (Get-Service $name -ErrorAction SilentlyContinue) { Write-Warning "Service '$name' already exists in system." } else { & $application $arguments Start-Sleep -s 3 if (Get-Service $name -ErrorAction SilentlyContinue) { Write-Host "Service '$name' successfully installed." -ForegroundColor Green } else { $Host.UI.WriteErrorLine("ERROR: Unable to create the service '" + $name + "'") Exit } } if ((Get-Service $name -ErrorAction SilentlyContinue).Status -ne "Running") { Write-Host "Starting service '$name'..." -ForegroundColor Green & sc.exe start $name } else { Write-Host "Service '$name' already started." -ForegroundColor Green } } Finally { cd $currentFolder }
uninstallService.ps1
The following script will stop the service and then remove it:
function waitServiceStop { param ( [string]$svcName ) $svc = Get-Service $svcName # Wait for 30s $svc.WaitForStatus('Stopped', '00:00:30') if ($svc.Status -ne 'Stopped') { $Host.UI.WriteErrorLine("ERROR: Not able to stop service " + $serviceName) } else { Write-Host "Service '$serviceName' is stopped." -ForegroundColor Green } } # Check script is launched with Administrator permissions $isAdministrator = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator") if ($isAdministrator -eq $False) { $Host.UI.WriteErrorLine("ERROR: Please ensure script is launched with Administrator rights") Exit } $currentFolder = Get-Location $defaultServiceName="SDLHealthcheck" try { $scriptPath=$PSScriptRoot cd $scriptPath\.. $rootFolder = Get-Location $serviceName = $defaultServiceName if (-Not (Get-Service $serviceName -ErrorAction SilentlyContinue)) { $Host.UI.WriteErrorLine("ERROR: There is no service with name " + $serviceName) Exit } Write-Host "Stopping service '$serviceName'..." -ForegroundColor Green & sc.exe stop $serviceName waitServiceStop $serviceName Write-Host "Removing service '$serviceName'..." -ForegroundColor Green & sc.exe delete $serviceName if (-Not (Get-Service $serviceName -ErrorAction SilentlyContinue)) { Write-Host "Service '$serviceName' successfully removed." -ForegroundColor Green } } Finally { cd $currentFolder }
Comments