skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Saturday, October 13, 2012

Java Service Launcher Demo

Posted by Raju Gupta at 11:00 AM – 0 comments
 

This will help you understand Java Service Launcher(JSL) an open source code to convert your Java Batch jobs into windows services. Any java batch can be converted into a windows service using this. we will use a sample demo java batch so that jsut with minor customization you can change your java batch into a windows service. The required files are as follows:
1) JSL EXE to be renamed as per your requirement. (Here;: MyJavaService.exe)
2) INI file to be customized accoding to the need. (Usually in the same name as the exe i.e, MyJavaService.ini)
3) The java batch code which has to be run as windows service (MyJavaService.java)
4) A log4j configuration file for logging informations (This is not mandatory file. This is required for the above java file. This may or may not be used as per your requirement) (Here:: log4jConfig_local.xml)
Place this file in (D:/Data_Apps/cluster_apps4/log4jconfig/properties/JavaService/) else modify the java program according to your path and build a new jar.

Step to convert java batch to windows service
1) Double click on the MyJavaService.exe
2) Start "MyJavaService" that has newly been created in Control panel -> Administrative tools - > Services
3) Check the log in the following path (D:/Data_Apps/cluster_apps4/Gclfac/logs/MyJavaService.log) defined in the log4j config file.
4) Pause the service , check the log
5) Resume the service, check the log
6) Stop the service, Check the log

Thus you can customize the ini, replace the java batch with the required one and convert your java batch into windows service at one click.


import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

public class MyJavaService extends Thread{
 private static final String CLASSNAME = "com.gcl.batch.MyJavaService";
 static String filePath = "D:/Data_Apps/cluster_apps4/log4jconfig/properties/JavaService/log4jConfig_local.xml";
 static String data = "MY Java Service is running";
 static String paused = "My Java Service is paused";
 static String resumed = "My Java Service is resumed";
 static String stop = "My Java Service is stopped";
 static int line;
 static MyJavaService mjs;
 private static Logger logger  = LogManager.getLogger(CLASSNAME);
 public static void main(String[] args) {
  DOMConfigurator.configure(filePath);
  try {
   while(true)
   {
    mjs = new MyJavaService();
    mjs.start();
    mjs.join();
    line=0;
   }
  }
  catch (InterruptedException e) {
   logger.error("Thread interrupted");
   e.printStackTrace();
   System.exit(-1);
  }
 } 
 public void run()
 {
  logger.info(data);
  for(int i=0;i<100;i++)
  {
   line++;
   try {
    Thread.sleep(10);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  logger.debug(line);
 }

 public static void stopService()
 {
  logger.debug(stop);
  logger.debug("Stopped at line:"+line);
  System.exit(0);
 }

 public static void pauseService() throws InterruptedException
 {
  logger.debug(paused);
  logger.debug("Paused at line:"+line);
  mjs.suspend();
 }

 public static void resumeService()
 {
  logger.debug(resumed);
  logger.debug("Resumed from line:"+line);
  mjs.resume();
 }
}


Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger