Recent Posts

Monday, 12 September 2016

JSVC Tutorial


     Commons Daemon, formerly known as JSVC short form of Java Service, is a Java software library belonging to the Apache Commons Project. JSVC is a set of libraries and applications for making Java applications run on UNIX more easily. A daemon is a computer program that runs as a background process, executing tasks on a predefined schedule or in response to particular events, or in response to requests for information or services from other programs. To start up a Java program as a daemon using JSVC, we’ll need to do a few things,
1. Implement the Apache Commons Daemon interface.

2. Install JSVC by downloading and compiling the source (C) or via the command line with the sudo apt-get install jsvc command.

3. Create a bash shell script to launch the daemon and manage the basic set of daemon controls, namely: start, stop & restart. Others include reload & status.

Implementing the Java Daemon Interface
     Implementing the Daemon interface is a fairly simple undertaking. Once you have downloaded the library from the Apache Commons project site and referenced it in your Java project, you can go about creating your implementation as follows
package com.ashok.jsvc;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.commons.daemon.Daemon;
import org.apache.commons.daemon.DaemonContext;
import org.apache.commons.daemon.DaemonInitException;

class EchoTask extends TimerTask {
   @Override
   public void run() {
      System.out.println(new Date() + " running..!!;
   }
}

public class MyJSVCDaemon implements Daemon {

   private static Timer timer = null;

   public static void main(String[] args) {
      timer = new Timer();
      timer.schedule(new EchoTask(), 0, 1000);
   }

   @Override
   public void init(DaemonContext dc) throws DaemonInitException, Exception {
      System.out.println("MyJSVCDaemon initializing..!!");
   }

   @Override
   public void start() throws Exception {
      System.out.println("MyJSVCDaemon starting..!!");
      main(null);
   }

   @Override
   public void stop() throws Exception {
      System.out.println("MyJSVCDaemon stopping..!!");
      if (timer != null) {
         timer.cancel();
      }
   }

   @Override
   public void destroy() {
      System.out.println("done.");
   }
}
Shell start/stop script
     Debian daemon init scripts are stored in /etc/init.d/.
#!/bin/sh

# Setup variables
DESC="MyJSVCDaemon service"
EXEC=/usr/bin/jsvc
JAVA_HOME=/usr/lib/jvm/java-8-sun
CLASS_PATH="/usr/share/java/commons-daemon.jar":"/home/ashok/myjsvcdaemon.jar"
CLASS=com.ashok.jsvc.MyJSVCDaemon 
USER=root
PID=/tmp/myjsvcdaemon.pid
LOG_OUT=/tmp/myjsvcdaemon.out
LOG_ERR=/tmp/myjsvcdaemon.err

jsvc_exec()
{
   $EXEC -home "$JAVA_HOME" -cp $CLASS_PATH -user $USER -outfile $LOG_OUT -errfile $LOG_ERR -pidfile $PID $1 $CLASS
}

case "$1" in
   start)
   echo "Starting the $DESC..."         
      # Start the service
      jsvc_exec
   echo "The $DESC has started."
         ;;
   stop)
   echo "Stopping the $DESC..."
      # Stop the service
      jsvc_exec"-stop"
   echo "The $DESC has stopped."
         ;;
   restart)
      if [ -f "$PID" ]; then
      echo "Restarting the $DESC..."
         # Stop the service
         jsvc_exec"-stop"

         # Start the service
         jsvc_exec
      else
         echo "Service not running, will do nothing"
         exit 1
      fi
         ;;
   *)
         echo "usage: /etc/init.d/MyJSVCDaemon {start|stop|restart}" >&2
         exit 3
         ;;
esac
The resule will be
$ ./service start && sleep 5 && ./service stop
$ cat /tmp/myjsvcdaemon.out
MyJSVCDaemon initializing..!!
MyJSVCDaemon starting..!!
Mon Sep 12 14:26:54 EEST 2016 running..!!
Mon Sep 12 14:26:55 EEST 2016 running..!!
Mon Sep 12 14:26:56 EEST 2016 running..!!
Mon Sep 12 14:26:57 EEST 2016 running..!!
Mon Sep 12 14:26:58 EEST 2016 running..!!
MyJSVCDaemon stopping..!!
done.

1 comment: