RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Converting Your Java App into a Windows Service : Page 3

Reusing a framework usually is a good idea, but sometimes developing your own code for a specific task is easier. Developing code to convert a Java application into a Windows service turns out to be one of those rare cases.

Integrating JNI with the Windows Service API
I needed a continuously running Java app to demonstrate service capability, so I wrote a simple Dummy.java class that waits inside the main method for a stop event. I implemented a shutdown method, which upon invocation sets the stop event, causing the main thread to exit gracefully. The Dummy class also implements a shutdown hook, which is used mainly when it is launched with java.exe.

Typical Windows services (excluding device drivers) are user processes launched and managed by the Service Control Manager (SCM), also known as the services.exe process. A single process can contain many services. In my design, I prefer to contain one JVM instance per process and one Windows service per process.

Installing a service requires getting a handle to the SCM and creating a service, as follows:

SC_HANDLE schService = CreateService(..)

Uninstallation requires getting a handle to the service and calling delete on it:

SC_HANDLE schSCManager = OpenSCManager(.., SERVICE_ALL_ACCESS); 
SC_HANDLE schService = OpenService( schSCManager, ..);

To start up the service, you register the ServiceMain function with StartServiceCtrlDispatcher. The ServiceMain function contains your main functionality. In this case, call the InvokeMain function. Next, call RegisterServiceCtrlHandler(SERVICE_NAME, ServiceHandler);, which registers a Handler for receiving commands from the SCM. To prevent the whole service from crashing when a JVM abort occurs, launch the InvokeMain method in a separate thread.

To play with the Dummy service yourself, build the VS 2005 project DummyService, copy Dummy.java to C:\, and compile the code. Run the following commands to install, start, stop, and uninstall the service:

DummyService /i (installs the service)
net start DummyService 
net stop DummyService
DummyService /u (uninstalls the service)

At least 15-20 configurable parameters can result from integrating JNI with the Windows Service API. Here are the ones to consider when customizing the program to suit your requirements:

  • Windows service parameters:
    • Service name
    • Display name
    • Description
    • Path to executable
    • Startup type (automatic/manual)
    • Parameters can be stored in registry
    • Working directory
    • One service per process or many services in a single process
    • Interacts with desktop option (required if your service has a UI component)
    • Login username and password (run the service with a user account or local system account)
  • Java application-specific parameters:
    • Path to JVM.dll
    • JVM options (-D, -X)
    • Class name
    • Any command line arguments (though they are not recommended)
    • Shutdown timeout
  • Logging parameters:
    • Event logging versus file logging
    • Redirect any standard output/error to some file

During installation, depending on your requirements, you can store parameters under the recommended registry key location: HKLM\System\CurrentControlSet\[Service Name]\Parameters.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date