Creating a mircoservice with dropwizard is an easy task. You do some java coding and configure a YAML file to get a running microservice within a short time. The created jar artefact can be started and tested with the common java command. However if you would like to let your service run on a Microsoft server as a windows service you need a little helper tool from outside the java world.

Running a microservice on a windows server as a service has some important advantages that should be noticed when evaluating a microservice infrastructure:

  • Windows services are easy to manage by system administrators
  • A service can start automatically when the windows server is started
  • There is a GUI for configuring, starting and stopping a service

The java infrastructure does not offer a tool to have an application run as a windows service out-of-the-box. However there are some nice (free) helper tools that can handle this job for you:

  1. Apache Commons Daemon: http://commons.apache.org/proper/commons-daemon/index.html
    An Apache project to create services from java that run on Unix and Windows.
  2. NSSM: The non-**cking-service-manager https://nssm.cc
    A command-line / user-interface tool for installing and managing services for windows (just as the name says…).

I checked out both tools and managed to get a java application and a microservice running, so there is no wrong decision you can make when evaluating these helper tools. I find NSSM to be the easiest and fastest choice and I will use it for all service creations in the future (when running on windows systems). NSSM can be downloaded from the already mentioned website and you get one small (~ 320 kB) executable file that does not have to be installed, but runs as a portable application.

I created a dropwizard microservice (the one from this blogpost – a document service with 2 methods) and placed it on a windows server to get this directory structure:

  • C:\
    • \documentservice\
      • WinService-1.0-SNAPSHOT.jar
      • service-8080.yml
      • service-9090.yml
      • nssm.exe
    • \logs\
      • service.log

To install and configure a windows service you need administrator privileges, so open a CMD box and change into the c:\documentservice directory. To install the service type  nssm install DocumentService

This will open up the nssm user interface where you have to configure the java microservice. There are just a view options that are really necessary to get a running service. All are configurable in the Application tab:

  • Path: The path to java.exe (you need an installed JRE to execute java)
  • Startup directory: The path where your service will be executed (path of the jar file)
  • Arguments: the command line arguments passed to the java application, for a microservice it should be
    -jar [JARFILE].jar server [YAML-File]

It is a recommendation that you enter values for “Display name” and description (in the Details tab) to have a readable description for the service. These values will be shown by the windows service overview. There are a lot more configuration options for services like the user used to run the service, the priority of the process and even the processors that execute the service.

After clicking the “Install service” button, you should get a success message that says that your service was installed successfully. Now you can open the windows service overview and see your microservice in the list of all Windows services. You can now select and start your service.

NSSM does also allow you to edit (nssm edit DocumentService) or remove (nssm remove DocumentService) a service. It is a small but great tool to manage your java microservices on a windows platform.

By Meik Kaufmann

I am a certified oracle enterprise architect and like to share my knowledge and experience that I gain in the projects I am working on.

Leave a Reply

Your email address will not be published. Required fields are marked *