Running Spring Boot as a Windows Service
Creating Windows Service for Spring Boot
Creating a service on Linux is a fairly simple affair; create an init.d, systemd or upstart config file and you’re good to go. But creating a Windows Service is a little more complicated. Spring Boot creates executable jars or wars that you can run using “java -jar appname.jar”, but you can’t just type this into a dialog to create a Windows Service.
To get around this problem, we’re going to use WinSW (Windows Service Wrapper) to set up our service for us. This is a small executable that will be responsible for both installing/removing the service and also running our application. It was initially developed for the Jenkins CI project, so that people could run it on Windows, but it is very adaptable.
Setup WinSW for Spring Boot
In order to setup WinSW, you need to perform the following steps:
- Download
winsw.exe
from the distribution site, and rename it to your taste (such asappname.exe
) - Write
appname.xml
(see XML Config File specification for more details). You need to create this file in the same folder as the appname.exe executable you downloaded, as WinSW finds its configuration by looking for a xml file with the same base name in the same directory. - [sourcecode lang=”xml”]<service>
<id>appname</id>
<name>AppName</name>
<description>This service runs the AppName system.</description>
<env name=”APPNAME_HOME” value=”%BASE%”/>
<executable>java</executable>
<arguments>-Xrs -Xmx256m -jar “%BASE%\appname.war” –spring.profiles.active=live</arguments>
<logmode>rotate</logmode>
</service>[/sourcecode] - Run
appname.exe install <OPTIONS>
in order to install the service wrapper. If there’s an error, you can check the return value from the application and look up the error on the Microsoft website. - Optional – Perform additional configuration in the Windows Service Manager, like using a service account, but see below for an alternative.
- Optional – Perform extra configurations if required (guidelines are available below).
- Run the service from the Windows Service Manager.
Running as a service account
If you need the application to run under a particular account, you have three options:
You can set the account details in the service properties dialog in Service Manager, but WinSW has native support for service accounts. You can use the /p
option when you install the service to be prompted for the account username and password.
*include example here*
Alternatively, you can include the information in the XML configuration file:
[sourcecode lang=”xml”]<service>
<id>appname</id>
<name>AppName</name>
<description>This service runs AppName system.</description>
<env name=”APPNAME_HOME” value=”%BASE%”/>
<executable>java</executable>
<arguments>-Xrs -Xmx256m -jar “%BASE%\appname.war” –spring.profiles.active=live</arguments>
<logmode>rotate</logmode>
<serviceaccount>
<domain>YOURDOMAIN</domain>
<user>useraccount</user>
<password>Pa55w0rd</password>
<allowservicelogon>true</allowservicelogon>
</serviceaccount>
</service>
[/sourcecode]
Windows Service Wrapper command line options
install
to install the service to Windows Service Controller.uninstall
to uninstall the service. The opposite operation of above.start
to start the service. The service must have already been installed.stop
to stop the service.restart
to restart the service. If the service is not currently running, this command acts likestart
.status
to check the current status of the service.- This command prints one line to the console.
NonExistent
indicates the service is not currently installedStarted
to indicate the service is currently runningStopped
to indicate that the service is installed but not currently running.