Recently i got a small request from one of my client to provide him a windows/linux service which runs like a deamon and mails some files in the pc every fortnight. As i’m a java guy was trying to find out options how to do this. Luckily we guys have an option in form of Java Service Wrapper .
http://wrapper.tanukisoftware.org
This is a wonderful tool in hands of java developers. It is very simple to create service using this. Following are the things tou have to do.
Import Following Classes In the Main Class which you want interact with Windows/Linux Service.
import org.tanukisoftware.wrapper.WrapperListener; ( This is implemented so that we can listen to Widows/Linux Service actions Like Start|Stop)
import org.tanukisoftware.wrapper.WrapperManager; ( This is used to manage our service )
And you have to configure the Wrapper.conf provided here is basic configuration you have to change in default one
# Java Main class. This class must implement the WrapperListener interface
# or guarantee that the WrapperManager class is initialized. Helper
# classes are provided to do this for you. See the Integration section
# of the documentation for details.
wrapper.java.mainclass=com.webaging.DirectoryChangeMailer (This is class which gets invoked on Service Actions)
# Java Classpath (include wrapper.jar) Add class path elements as
# needed starting from 1
wrapper.java.classpath.1=../lib/*.jar Give path to all jars Similarly
wrapper.java.classpath.2=../conf This where you have all your property files like log4j.properties
# Java Library Path (location of Wrapper.DLL or libwrapper.so)
wrapper.java.library.path.1=../lib
Here is How you change Java Code
For Example Look at following Main Class
/**
*
*/
package com.webaging;
import java.io.IOException;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.tanukisoftware.wrapper.WrapperListener;
import org.tanukisoftware.wrapper.WrapperManager;
import com.webaging.listener.FilePresenceListener;
/**
* @author Ashwin
*
*/
public class DirectoryChangeMailer implements WrapperListener {
private static final transient Log logger = LogFactory
.getLog(DirectoryChangeMailer.class);
public static Properties prop = null;
private Scheduler sched = null;
static {
try {
prop = PropertiesLoaderUtils.loadAllProperties("mailer.properties");
} catch (IOException e) {
logger.error("Can not find mailer.properties", e);
}
}
private DirectoryChangeMailer() {
}
/**
* @param args
*/
public static void main(String[] args) {
// Start the application. If the JVM was launched from the native
// Wrapper then the application will wait for the native Wrapper to
// call the application's start method. Otherwise the start method
// will be called immediately.
WrapperManager.start(new DirectoryChangeMailer(), args);
}
public void controlEvent(int arg0) {
// TODO Auto-generated method stub
}
/*
* (non-Javadoc)
*
* @see org.tanukisoftware.wrapper.WrapperListener#start(java.lang.String[])
*/
public Integer start(String[] arg0) {
try {
System.out.println("Testing Console Output");
sched = StdSchedulerFactory.getDefaultScheduler();
Trigger durectoryReaderTrigger = TriggerUtils
.makeMinutelyTrigger(1);
durectoryReaderTrigger.setName("DirectoryListener");
durectoryReaderTrigger.setGroup("DirectoryListenerGroup");
durectoryReaderTrigger.setCalendarName("DirectoryListener");
JobDetail directoryListenerDetail = null;
try {
directoryListenerDetail = new JobDetail("DirectoryListenerJob",
"DirectoryListenerGroup", FilePresenceListener.class);
} catch (Throwable e) {
System.out.println("Exception in Creation of Job Detail"
+ e.getLocalizedMessage());
}
directoryListenerDetail.getJobDataMap().put("mailer.from",
prop.getProperty("mailer.from"));
directoryListenerDetail.getJobDataMap().put("mailer.to",
prop.getProperty("mailer.to"));
directoryListenerDetail.getJobDataMap().put("mailer.subject",
prop.getProperty("mailer.subject"));
directoryListenerDetail.getJobDataMap().put("mailer.content",
prop.getProperty("mailer.content"));
directoryListenerDetail.getJobDataMap().put("file.directory",
prop.getProperty("file.directory"));
directoryListenerDetail.getJobDataMap().put("archive.directory",
prop.getProperty("archive.directory"));
directoryListenerDetail.getJobDataMap().put("mailer.host",
prop.getProperty("mailer.host"));
logger.info("Job Scheduled For Every 1 Minute");
sched.scheduleJob(directoryListenerDetail, durectoryReaderTrigger);
sched.start();
} catch (Exception e) {
logger.debug("Exception in Scheduler", e);
e.printStackTrace();
}
return null;
}
/*
* (non-Javadoc)
*
* @see org.tanukisoftware.wrapper.WrapperListener#stop(int)
*/
public int stop(int arg0) {
// TODO Auto-generated method stub
try {
sched.shutdown();
} catch (Exception e) {
logger.error(e);
}
return 0;
}
}
The above Class is self explanatory. It actually implements Following following 3 methods of WrapperListener
public Integer start(String[] arg0) {
public int stop(int arg0) {
public void controlEvent(int arg0) {
One Of These three methods are called when ever you Apply any action on service like start/stop a service.
NOTE:- For all those use class ResourceAsStream method to load some file.
.class.getResource(“/”).getPath(); Gives you path to LIb so be Careful in placing your files to load
Here is sample Directory Structure of How I Placed Files
C:\FileListener
├───bin
│ InstallFileListener-NT.bat
│ PauseFileListener-NT.bat
│ ResumeFileListener-NT.bat
│ StartFileListener-NT.bat
│ StopFileListener-NT.bat
│ UninstallFileListener-NT.bat
│ wrapper-windows-x86-32.exe
│
├───conf
│ log4j.properties
│ wrapper.conf
│
├───lib
│ activation.jar
│ commons-beanutils.jar
│ commons-logging-api.jar
│ commons-logging.jar
│ FileListener.jar
│ log4j-1.2.5.jar
│ mail.jar
│ mailapi.jar
│ mailer.properties
│ quartz-all-1.5.2.jar
│ spring.jar
│ wrapper-windows-x86-32.dll
│ wrapper.jar
│
└───logs
wrapper.log
More tutorials on above subject