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 4

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.

Shutting Down the Java App Gracefully
My first shutdown approach was calling System.exit(0). Although this method called all the registered shutdown hooks concurrently and tried to shutdown the JVM gracefully, it failed to clean the pipe communication that the Windows service established with the SCM. So when I used the System.exit call, I got the following error while stopping the service:

System error 109 has occurred.
The pipe has been ended.

I figured out a better way: implement a separate shutdown method and call it. The framework proposed in this article invokes the public static void shutdown() method (if it is implemented) in a separate shutdown thread with a timeout of 20 seconds. This prevents the service from reaching a "not responding" state if the shutdown method never returns. You can configure the timeout period based on your own needs, but make sure it is less than the system timeout. Otherwise, the SCM will terminate your service process.

Beyond the Windows Service
Understanding JNI invocation API provides the flexibility to call Java apps in your C/C++ program. For instance, you may want to provide a web interface to your legacy C application by embedding a Jetty Servlet engine.

Narendra Venkataraman is a software architect at Tellytopia Inc. In his spare time, he blogs at Naren's Developer Diary.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date