Generally, there is no way for Windows services manager to inform a Java program that it should stop. When the JWinSvc service command handler receives a request to stop the service, it tries to shutdown a Java application gracefully and uses several approaches.
Using "System.exit() injection"
After receiving the stop request, JWinSvc uses a unique method "System.exit() injection" that calls "System.exit()" via JNI in a way as if the Java application itself called "System.exit()". This is the smoothest way to terminate Java applications.
Using "/shutdownCommand" parameter
As some existing Java applications use other ways to invoke a proper shutdown sequence, e.g. running an external utility or command that makes use of interprocess communication, to control the application (TCP connection...). JWinSvc accommodates itself to these ways of shutting down an application and allows to specify an external utility or command to be performed in <command line> in the described command line parameter.
Example:
jwinsvc "Test Service" /install /shutdownCommand:"C:\test app\shutdown.bat" java -jar test.jar
Note: Use CMD.EXE standard quoting to pass double quotes down to the shutdown command. Example:
jwinsvc "Test Service" /install /shutdownCommand:"C:\test app\shutdown.bat \"parameter with quotes\"" java -jar test.jar
Note: JWinSvc will recognize whether the entered command line specifies an executable or batch file. In case it is not considered as an executable, the command line is prefixed with "CMD.EXE /C " before attempting execution.
In this case, JWinSvc solves the problem of application shutdown by implementing a JNI-based mechanism that calls a selected method of the Java application when service stop is requested.
JWinSvc uses JNI (Java Native Interface) to call the "shutdown" method in the main application class. Your main class (the class with the public static void main method) must implement a "public static void" shutdown method. The method can have any name but it must be "public static void" without parameters. It also must not throw any exceptions. Example of such method:
public static void shutdownRequested()JWinSvc calls this method once the user or system requests the service to stop. This happens during Windows shutdown and also when a user manually stops the service either from the Services Manager or via the "net stop" command. The method must force your application to stop as soon as possible, as by default, JWinSvc terminates the application after 30 seconds forcibly. If you know the shutdown process takes longer, you can set a different timeout with the /timeout command.
You must explicitly inform JWinSvc that your application implements a shutdown method and what its name is (if different from "shutdown"). This is done with the /shutdown command. If your shutdown method is implemented as above and the shutdown process takes up to 40 seconds, the jwinsvc command will look as follows:
jwinsvc "My Service" /install /start /shutdown:shutdownRequested
/timeout:40000 java -jar myjar.jar
Note: JWinSvc attempts to invoke the "shutdown method" only if the /shutdown parameter is specified.
Even if your Java application's main class contains the appropriate shutdown method with the specified signature
public static void shutdown(), this method will be not called unless the /shutdown parameter is specified.
The shutdown method name is "shutdown" unless otherwise specified in the argument of the /shutdown parameter.
Only the method name of the Java application's shutdown method can be modified, the method itself has to be public, static and without
arguments and return value.
Trap Console, Service Console, Active SNMP, JWinSvc, Ping Console, CS-Care, Visual SNMP and WConsole
are trademarks of CSCare Inc.
Sun, Sun Microsystems, Java, 100% Pure Java and "Write Once, Run Anywhere" are trademarks or registered
trademarks of Sun Microsystems, Inc. in the United States and other
countries. Novell, NetWare, Novell IPX and/or other Novell products are
trademarks or registered trademarks of Novell, Inc. Microsoft and/or other
Microsoft products are trademarks or registered trademarks of Microsoft.
Other product names and/or company names mentioned herein may be the
trademarks or registered trademarks of their respective owners.