2010년 4월 5일 월요일

quartz

퍼온글:

http://www.easywayserver.com/blog/java-job-scheduling-in-web-application-with-quartz-api/


Java – Job Scheduling in web application with quartz API

Scheduling job in web application is important part of application. Today most of the companies and programmer keep eye on the performance of the application. They do it by running most of work in background of the application without the knowledge of the user. Manual work is going to automize in these days and run in idle time of working hours.

If you want to send email at fixed time, send newsletter, taking backup of database, synchronizing of databases, setting up reminder, all need to run schedule job at particular interval.

Java is bundled with timer class to perform scheduling task and make your own logics to perform scheduling by while loop with sleep thread. But this is not good way to schedule more than one job at a time.

Timer class is good option for scheduling task, but it still doesn’t have optional day selection like in cron of linux.

Quartz is scheduling API which fulfill your all need and easy to use and initialization of scheduling.

We can use simple trigger with millisecond and repeat jobs and set repeat intervals. Advance Trigger CronTrigger works exactly same unix cron. In CronTrigger we can define, selected days e.g. Wednesday, Friday weekly, monthly and yearly.

In quartz, we can monitor our jobs and in between can stop jobs.

Quartz scheduling can be used with servlet initialization and struts initialization.

In servlet initialization, we have to define in web.xml and in struts we have to define plugin in struts-config.xml.

1. Quartz with Simple Servlet

web.xml

<web-app>
<display-name>timer</display-name>

<servlet>
<servlet-name>InitializeServlet</servlet-name>
<servlet-class>com.cron.InitializeServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

</web-app>

InitializeServlet.java

package com.cron;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

public class InitializeServlet extends HttpServlet {

public void init() throws ServletException {

try {
System.out.println("Initializing NewsLetter PlugIn");

CronScheluder objPlugin = new CronScheluder();

}
catch (Exception ex) {
ex.printStackTrace();
}

}

}

CronScheluder.java

package com.cron;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

public class CronScheluder {

public CronScheluder() throws Exception {

SchedulerFactory sf = new StdSchedulerFactory();

Scheduler sche = sf.getScheduler();

sche.start();

JobDetail jDetail = new JobDetail("Newsletter", "NJob", MyJob.class);

//"0 0 12 * * ?" Fire at 12pm (noon) every day
//"0/2 * * * * ?" Fire at every 2 seconds every day

CronTrigger crTrigger = new CronTrigger("cronTrigger", "NJob", "0/2 * * * * ?");

sche.scheduleJob(jDetail, crTrigger);
}
}

MyJob.java

package com.cron;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class MyJob implements Job {

public void execute(JobExecutionContext context)
throws JobExecutionException {

System.out.println("Cron executing ");

}
}

Another option to use quartz api

2. Quartz with ServletContextListener

web.xml

<web-app>

<display-name>timer</display-name>

<listener>
<listener-class>com.cron.StartCron</listener-class>
</listener>

</web-app>

StartCron.java

package com.cron;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class StartCron implements ServletContextListener{

public void contextDestroyed(ServletContextEvent arg0)
{
System.out.println("Stopping Application successfully");
}

public void contextInitialized(ServletContextEvent arg0)
{
System.out.println("Initializing Application successfully");

try{
CronScheluder objPlugin = new CronScheluder();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

All rest of code is same and use

CronScheluder.java

MyJob.java

3. Quartz with Struts

You have to define in struts-config.xml file a plugin

 <plug-in className="com.cron.StrutsImp">
<set-property property="startOnLoad" value="true"/>
<set-property property="startupDelay" value="0"/>
</plug-in>

StrutsImp.java

package com.cron;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;

public class StrutsImp implements PlugIn {

public static final String PLUGIN_NAME_KEY = StrutsImp.class.getName();

public void init(ActionServlet servlet, ModuleConfig config)
throws ServletException {

try {
System.out.println("Initializing PlugIn");

ServletContext context = null;

context = servlet.getServletContext();

CronScheluder objPlugin = new CronScheluder(context);

context.setAttribute(PLUGIN_NAME_KEY, objPlugin);

System.out.println("scheluder started successfully...");

}
catch (Exception ex) {
ex.printStackTrace();
}

}

public void destroy()
{

}

}

댓글 없음:

댓글 쓰기