Advertise with us

Our eBooks

JBoss Quartz tutorial

Update: Please notice that an updated tutorial about Quartz Scheduler has been published on the following article: Quartz 2 tutorial on JBoss AS 7

Quartz is a full-featured, open source job scheduling service that can be integrated with, or used along side virtually any Java EE or Java SE application - from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs;

 The two fundamental units of Quartz's scheduling package are jobs and triggers.  

A job is an executable task that can be scheduled, while a trigger provides a schedule for a job

You can make Java component executable by the scheduler simply by making it implement the Job interface.   

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.

Here's an example of SimpleTrigger: 

long startTime = System.currentTimeMillis() + 5000L;

SimpleTrigger trigger = new SimpleTrigger("myTrigger",
 new Date(startTime),

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",
 new Date(),
 10L * 1000L);

A simpler way to create a Trigger is by means of the org.quartz.TriggerUtils class which allows to define a trigger using a simpler method notation.

For example if you were to create a trigger that fires every 10 seconds you could use simply:

JobDetail job = new JobDetail("job1", "group1", TestJob.class);

Trigger trigger = TriggerUtils.makeSecondlyTrigger(10);
 scheduler.scheduleJob(job, trigger);

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:
This creates a trigger that fires at 10:30, 11:30, 12:30, and 13:30, on every Wednesday and Friday.

 CronTrigger trigger  new CronTrigger(
 "Income Report",
 "Report Generation"
 "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:

© 2021 All Rights Reserved.

Please publish modules in offcanvas position.