martedì 22 gennaio 2013

Running Java Applications as a Windows Service with NSSM

Sometimes there is the request to run a Java Application as a windows service, for this scenario it is possible to use a java wrapper as NSSM. The example uses a java application that log on file the boot operation, the alive status every 10 seconds, and the exit operation. The code of the example can be downloaded with SVN fromthe repository https://subversion.assembla.com/svn/gma-sample/trunk/WindowsServiceTutorial.

package org.gma.tutorial.nssm;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Date;

/**
 * A Simple service to register as Windows Service, the program will log on a
 * file when it starts, ends and every 10 seconds it will log is alive state
 * 
 * 
 */
public class WindowsServiceTutorial {

	public static void main(String[] args) throws FileNotFoundException {

		
		
		
		/* Set the custom message to display */
		String customMessage = "default";
		if (args.length > 0) {
			customMessage = args[0];
		}
		/*
		 * Set the standard output stream as on a log file
		 */
		String pathname = "./log.txt";
		FileOutputStream out = new FileOutputStream(new File(pathname), true);
		PrintStream printStream = new PrintStream(out);
		System.setOut(printStream);

		/*
		 * Add the shutdown hook
		 */
		Runtime.getRuntime().addShutdownHook(new Thread(new ShutdownHook()));
		

		onStart();

		doWork(customMessage);
		
		
		
	}

	/**
	 * Log the alive state of the service
	 * 
	 * @param customMessage
	 *          A custom message
	 */
	private static void doWork(String customMessage) {
		while (true) {
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				System.out.println("Interrupted at " + new Date());
			}
			System.out.println("Alive at " + new Date() + " " + customMessage);
		}
	}

	/**
	 * Log the boot of the service
	 */
	private static void onStart() {
		System.out.println("Starts at " + new Date());
	}

	/**
	 * A shutdown hook
	 * 
	 * 
	 */
	private static class ShutdownHook implements Runnable {

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.lang.Runnable#run()
		 */
		public void run() {
			onStop();
		}

		/**
		 * Logs when the service is stopped
		 */
		private void onStop() {
			System.out.println("Ends at " + new Date());
			System.out.flush();
			System.out.close();
		}

	}

}
After the jar is created and copied to %YOUR_FOLDER% we need a batch to starts the application, the %1 is the first parameter of the batch file.
@ECHO OFF
REM start.bat  code
java -jar  WindowsServiceTutorial.jar %1
Finally we can download the NSSM wrapper and type the command
nssm install WindowsServicetutorial
A window will be showed to enter the path of the application and the options to start it:
Opening the Service manager we will our service installed and ready to be started and stopped.
Opening the log.txt file generated we can read the following log:
 Starts at Tue Jan 22 15:28:15 CET 2013
 Alive at Tue Jan 22 15:28:25 CET 2013 servicemessage
 Alive at Tue Jan 22 15:28:35 CET 2013 servicemessage
 Alive at Tue Jan 22 15:28:45 CET 2013 servicemessage
 Alive at Tue Jan 22 15:28:55 CET 2013 servicemessage
 Alive at Tue Jan 22 15:29:05 CET 2013 servicemessage
 Alive at Tue Jan 22 15:29:15 CET 2013 servicemessage
 Ends at Tue Jan 22 15:29:16

2 commenti: