martedì 22 gennaio 2013

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 from the 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
Categories:

23 commenti:

  1. Thanks for sharing informative article on java application development. Your post helped to understand the career in Java. JAVA Training in Chennai

    RispondiElimina
  2. There are lots of information about latest technology and how to get trained in them, like Best Hadoop Training In Chennai have spread around the web, but this is a unique one according to me. The strategy you have updated here will make me to get trained in future technologies Hadoop Training in Chennai By the way you are running a great blog. Thanks for sharing this blogs..

    RispondiElimina
  3. I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..
    SalesForce Training in Chennai

    RispondiElimina
  4. Pretty article! I found some useful information in your blog, it was awesome to read,thanks for sharing this great content to my vision, keep sharing..
    Unix Training In Chennai

    RispondiElimina
  5. This information is impressive..I am inspired with your post writing style & how continuously you describe this topic. After reading your post,thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic
    Android Training In Chennai In Chennai

    RispondiElimina
  6. SAP Training in Chennai
    This post is really nice and informative. The explanation given is really comprehensive and informative..

    RispondiElimina
  7. Oracle Training in chennai
    Thanks for sharing such a great information..Its really nice and informative..

    RispondiElimina
  8. Selenium Training in Chennai
    Wonderful blog.. Thanks for sharing informative blog.. its very useful to me..

    RispondiElimina
  9. Data warehousing Training in Chennai
    I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly..

    RispondiElimina
  10. Whatever we gathered information from the blogs, we should implement that in practically then only we can understand that exact thing clearly, but it’s no need to do it, because you have explained the concepts very well. It was crystal clear, keep sharing..
    Websphere Training in Chennai

    RispondiElimina
  11. Oracle DBA Training in Chennai
    Thanks for sharing this informative blog. I did Oracle DBA Certification in Greens Technology at Adyar. This is really useful for me to make a bright career..

    RispondiElimina
  12. This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..
    Selenium Training in Chennai | QTP Training in Chennai

    RispondiElimina
  13. Thanks for Information Oracle Apps Technical is a collection of a bunch of collected applications like accounts payables, purchasing, inventory, accounts receivables, human resources, order management, general ledger and fixed assets, etc which have its own functionality for serving the business
    Oracle Apps Training In Chennai

    RispondiElimina
  14. Oracle Training in chennai | Oracle D2K Training In chennai
    This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..

    RispondiElimina
  15. Wow, brilliant article on dot net training in Chennai that I was searching for. Helps us a lot in referring at my dot net training institutes in Chennai. Thanks a lot. Keep writing more on dot net training Chennai, would love to follow your posts and refer to others in dot net training institute in Chennai.

    RispondiElimina
  16. Nice Article! Mostly I have gathered knowledge from the blogger, because its provides more information over the books & here I can get more experienced skills from the professional, thanks for taking your to discussing this topic.
    Regards,
    sas training in Chennai|sas course in Chennai|sas training institute in Chennai

    RispondiElimina
  17. That is a brilliant article on dot net training in Chennai that I was searching for. Helps us a lot in referring at our dot net training institute in Chennai. Thanks a lot. Keep writing more on dot net course in Chennai, would love to follow your posts and refer to others in dot net training institutes in Chennai.

    RispondiElimina
  18. Wow! It was the best article , actually you have posted something useful than others, because I read many articles related to this basics of programming, but I only get impressed with your post only, keep posting.
    Regards,
    ccna course in Chennai|ccna training in Chennai|ccna training institute in Chennai

    RispondiElimina
  19. This blog is very interesting about the java application.
    Java Training ​in Chennai

    RispondiElimina
  20. Best SAS Training Institute In Chennai It’s too informative blog and I am getting conglomerations of info’s about Oracle interview questions and answer .Thanks for sharing, I would like to see your updates regularly so keep blogging.

    RispondiElimina
  21. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it. The python programming language is very popular and most widely used.
    Python Training in Chennai | Python Course in Chennai

    RispondiElimina

Subscribe to RSS Feed Follow me on Twitter!