Passa ai contenuti principali

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 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

Commenti

  1. 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
  2. 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
  3. 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
  4. 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
  5. SAP Training in Chennai
    This post is really nice and informative. The explanation given is really comprehensive and informative..

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

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

    RispondiElimina
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. This blog is very interesting about the java application.
    Java Training ​in Chennai

    RispondiElimina
  19. 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
  20. 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
  21. This blog is very interesting about the java application.
    qtp training in chennai

    RispondiElimina
  22. Thanks for sharing such a great information..Its really nice and informative..
    selenium training in chennai

    RispondiElimina
  23. Nice post!! Thanks for sharing. This is informative and really useful for me.

    Teradata Training in Delhi
    UG-NX Training Institute in Delhi

    RispondiElimina
  24. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Android Training in Chennai
    Ios Training in Chennai

    RispondiElimina
  25. Such a great articles in my carrier, It's wonderful commands like easiest understand words of knowledge in information's.
    Python Training in Chennai

    RispondiElimina

  26. 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..
    Android App Development Company

    RispondiElimina
  27. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Android Training in Chennai
    Ios Training in Chennai

    RispondiElimina
  28. 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..
    iOS App Development Company
    iOS App Development Company

    RispondiElimina
  29. I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.

    <Fitness SMS
    Fitness Text
    Salon SMS
    Salon Text
    Investor Relation SMS
    Investor Relation Text
    Mobile Marketing Services
    mobile marketing companies
    Sms API

    RispondiElimina
  30. great and nice blog thanks sharing..I just want to say that all the information you have given here is awesome...Thank you very much for this one.
    web design Company
    web development Company
    web design Company in chennai
    web development Company in chennai
    web design Company in India
    web development Company in India

    RispondiElimina
  31. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Android Training in Chennai
    Ios Training in Chennai

    RispondiElimina
  32. Excellent Post, Interesting Article.Indias Fastest Local Search Engine, you can Search Anything, From anywhere at any time CALL360

    RispondiElimina

Posta un commento

Post popolari in questo blog

Install Mysql JDBC Driver on WildFly 8.2

The tutorial explains how to install a jdbc driver on RedHat WildFly application server a module, in the sample will be used the mysql driver but the proposed steps can be applied to any other driver libraryUsed technologies in the tutorial:Wild Fly 8.2.0.FinalMysql Connector 5.1.33 1. Create the jboss moduleEnter in the file path ${EAP_HOME}/modules/system/layers/base and create the directories com/mysql/driver/main.Into the folder main copy the driver library jar (link) and create a file module.xml as showed in the sample below<module xmlns="urn:jboss:module:1.3" name="com.mysql.driver"> <resources> <resource-root path="mysql-connector-java-5.1.33.jar" /> </resources> <dependencies> <module name="javax.api"/> <module name="javax.transaction.api"/> </dependencies> </module>For an explanation about the meaning of the fields take a look at Jboss Module Documentation Page2. R…

org.w3c.dom.Document convertion Utils

How to convert an InputStream to a Document?
How to contert a Document to an InputStream? The snippet of code posted is an utility Java Class written in Java for org.w3c.dom.Document convertions, it supports:

Document to StringString to DocumentDocument to InputStreamInputStream to Document
package org.gma.util; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.StringWriter; import java.io.Writer; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.apache.xml.serialize.OutputFormat; import org.apache.xml.serialize.XMLSerializer; import org.w3c.dom.Document; import org.xml.sax.SAXException; public class DocumentUtils { /** * Convert a document to an Inputstream * @param document the document to convert * @param prettyPrint prettyPri…