0

Java application as a Windows Service

Posted by Guillermo GarcĂ­a on 10:40 PM in , ,
Sometimes, you will need to create a java application that runs as a service. As we know it, @linux this could be really easy using the "rc.d and init.d" scripts. But in windows, as many thing, these advanced features are always a little bit tricky.

To make the long story short, you need an app that can be register as a services in Windows OS and this one must capture some events from the "windows service manager", for example: start, pause and stop events.

As I always say, everything is possible if you have enough money and time. So, yeah, I think that you can do all this from the java application (even if you need to implement a native method), but if you doesn´t have time, you can use a wonderful wrapper that I found in the internet: Java Service Wrapper.

This Wrapper can transform any Java App in a service in Windows and Linux. The wrapper offers three type of integration with your application, and two of them can be implemented without source code modification. The third one needs source code modification to add custom handlers for the start, pause and stop events.

If you want to see it in detail, visit their webpage:
http://wrapper.tanukisoftware.org/doc/english/introduction.html

Now, when I used the wrapper, I found something that you should know:

I made a multi-client application, so, every client is handled by a different thread. In my original desing, my java main method creates the ServerSocket that waits for a client connection. When the client connects, the java main method creates a thread for that specific client and then, waits for another one. The java main method do this inside an endless loop.

But when I tried to use the wrapper with my original desing, the wrapper hangs. The reason was simple: The wrapper thinks that my application doesn't start well, because the main method seems to be hanged (but its not, it is waiting for client connections).

So I fixed it doing this:

The java main method creates a thread that have the ServerSocket and waits for clients. With this, the java main method creates a thread and then it ends. Now the wrapper thinks the application was started correctly and my application runs smoothly.

Conclusion: Never implements endless loop in the main method. Creates a Thread instead.

0 Comments

Copyright © 2009 ggarciao.com
- Cup of Java -
All rights reserved.