| JBoss Quartz |
| Written by F.Marchioni | |||||||||||||||||||||||||||||||||||||||||
|
Many business processes require asynchronous job scheduling. The Java/J2EE community has produced several attempts at solving this issue: starting with Java 1.3, Sun added the java.util.Timer and java.util.TimerTask classes to help add basic Timer functionality to the core language. Although the Timer and TimerTask can work for simple requirements, an heavy duty job scheduling needs much more. Quartz is an open source project that offers an extensive set of job scheduling features. The two fundamental units of Quartz's scheduling package are jobs and triggers.
package sample;
public class TestJob implements org.quartz.Job {
public TestJob() { }
public void execute(org.quartz.JobExecutionContext jobExecutionContext)
throws org.quartz.JobExecutionException {
System.out.println("Job executed!");
}
}
There are two basic kinds of Triggers: SimpleTrigger and CronTrigger. SimpleTrigger provides basically the same functionality you get from the Timer API. It should be used if the Job should be triggered once, followed possibly by repeats at a specific interval. You can specify start date, end date, repeat count, and repeat interval for this kind of trigger. With this description, you may not find it surprising to find that the properties of a SimpleTrigger include: a start-time, and end-time, a repeat count, and a repeat interval. All of these properties are exactly what you'd expect them to be, with only a couple special notes related to the end-time property.
long startTime = System.currentTimeMillis() + 5000L;
SimpleTrigger trigger = new SimpleTrigger("myTrigger",
null,
new Date(startTime),
null,
0,
0L);
This will create a trigger that fires now and repeats every 10 seconds. If you want to repeat the trigger zero or more times you can use the repeat parameter which can be set to SimpleTrigger.REPEAT_INDEFINITELY for triggers that will fire non-stop.
SimpleTrigger trigger = new SimpleTrigger("myTrigger",
null,
new Date(),
null,
SimpleTrigger.REPEAT_INDEFINITELY,
10L * 1000L);
CronTrigger is another type of trigger which provides a better flexibility to schedule jobs on a more realistic basis. CronTriggers allow us to express schedules such as "every weekday at 7:00 p.m." or "every five minutes on Saturday and Sunday." Here's an example of Cron trigger: CronTrigger trigger new CronTrigger( "Income Report", "Report Generation" ); trigger.setCronExpression( "0 30 10-13 ? * WED,FRI" ); Ok, now that we have some basic concepts about quartz we need to learn how to use this framework. Basically you can run Quartz in two modes: 1) Standalone Quartz server:This way you are responsible of starting/stopping your own Quartz scheduler. This is the simpler way and could be used for smaller projects :
package sample;
import java.util.Date;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import org.quartz.impl.StdSchedulerFactory;
public class QuartzServer {
public static void main(String[] args) {
QuartzServer server = new QuartzServer();
try {
server.startScheduler();
} catch (SchedulerException ex) {
ex.printStackTrace();
}
}
protected void startScheduler() throws SchedulerException {
SchedulerFactory sf=new StdSchedulerFactory();
Scheduler sched=sf.getScheduler();
sched.start();
JobDetail jd=new JobDetail("myjob",sched.DEFAULT_GROUP,TestJob.class);
SimpleTrigger st=new SimpleTrigger("mytrigger",sched.DEFAULT_GROUP,new Date(),
null,SimpleTrigger.REPEAT_INDEFINITELY,60L*1000L);
sched.scheduleJob(jd, st);
}
}
Here the 'default' scheduler -defined in "quartz.properties"- is retreived from the SchedulerFactory and a Job is enlisted to fire every 60 seconds. (The TestJob class is exposed at the beginning of this article) 2) Embed Quartz inside an application serverLarger projects may as well use Quartz embedded inside the container. This leads to a lot of advanced features like job clustering which we'll explore in the next articles.
If you want to patch correctly Quartz, at first remove the Quartz libraries under server/lib and replace them with quartz-1.6.X.jar and quartz-jboss-1.6.X.jar (For this sample we're using Version 1.6.1 (latest stable release). Remove as well "quartz-ra.rar" in the "deploy" folder. <?xml version="1.0" encoding="UTF-8"?> <server> Ok so now we're ready to rock! This is a sample Servlet doGet method which looks up the Scheduler registered in the JNDI tree and schedules a new Job that simply fires every 5 minutes. public void doGet(HttpServletRequest request, HttpServletResponse response)
|
|||||||||||||||||||||||||||||||||||||||||
| Comments |
|
|
|||||
|
|||||
|
|||||
|
|||||
|
|||||
|
|||||

A job is an executable task that can be scheduled, while a trigger provides a schedule for a job
Hint: have you got a class cast exception when you lookup the StdScheduler ? I guess you have added Quartz libraries under WEB-INF/lib : remove them from there !