Autostart for your Spring Boot Application

Autostart for your Spring Boot Application

Table Of Contents

A few months ago I was asked to find a solution for starting and stopping a Spring Boot application under Windows automatically together with the computer this application was running on. After doing some research I found a nice fitting and open source solution with WinSW.

As you can read on the Github page of WinSW it “is an executable binary, which can be used to wrap and manage a custom process as a Windows service”. This windows service can be used to automatically start/stop your application on computer startup/shutdown. After downloading the binary (you can find it here) you have to perform the following simple steps to install your own custom windows service.

Step 1: Name the Service

First you take the downloaded winsw-2.1.2-bin.exe file and rename it to the name of your service. In this example I will call this MyCustomService.exe.

Step 2: Configure the Service

Next, you have to create a new MyCustomService.xml file and place it right next to the executable (it is mandatory that the file name is the same). This xml file holds all the configuration for your custom windows service. It could look like the following example:

<service>
    <id>MyCustomService</id> <!-- must be unique -->
    <name>MyCustomService</name>
    <description>This service runs my custom service.</description>
    <executable>java</executable>
    <arguments>-jar "%BASE%\myCustomService.jar"</arguments>
    <logpath>%BASE%\log</logpath>
    <log mode="roll-by-time">
    <pattern>yyyyMMdd</pattern>
    <download from="http://www.example.de/spring-application/myCustomService.jar" 
        to="%BASE%\myCustomService.jar"
        auth="basic" unsecureAuth="true"
        user="aUser" password="aPassw0rd"/>
    </log>
</service>

This configurations basically tells the windows service to:

  1. Download the jar file from the given URL and place it in the current folder
  2. Execute the just downloaded jar by executing the command java -jar myCustomService.jar
  3. Save all logs into the log folder (for more details about logging click here)

Step 3: Install the Service

To finally install the service as a Windows service you open your command line in the current folder and execute MyCustomService.exe install. After the installation you can directly test your service by executing MyCustomService.exe test. Now you can manage this service like any other default windows service. To put it in the autostart you have to navigate to your Windows services, select the newly service and set the Startup type to Automatic.

Conclusion

As seen in this short example WinSW can be used not only for executing java programs automatically on Windows startup but also for updating your programs automatically. In case you need to update this jar file on multiple Windows clients this can be a pretty neat feature, because you only have to replace the jar hosted on http://www.example.de/spring-application/myCustomService.jar and restart the computers.

Written By:

David Klassen

Written By:

David Klassen

Recent Posts

Dependency Injection and Inversion of Control

Inversion of control (IoC) is simply providing a callback (reaction) to an event that might happen in a system. In other words, instead of executing some logic directly, we invert the control to that callback whenever a specific event occurs.

Read more

6 Proven Cloud Cost Management Practices for Kubernetes

Teams looking to control and reduce their cloud costs can choose from multiple cloud cost management approaches. All of them require at least a basic understanding of what’s going on in your cloud infrastructure - this part relies on monitoring and reporting.

Read more

Building a Url Shortener With Node.Js

How to design a URL shortening service like tinyurl.com is a frequently asked question in system design interviews. URL shortener services convert long URLs into significantly shorter URL links.

Read more