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

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


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.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



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