Quartz 2 scheduler tutorial

Quartz, enterprise scheduler job framework, to help Java application to scheduler a job/task to run at a specified date and time.

This tutorial show you how to develop a scheduler job using latest Quartz library 2.1.5.

Note
Quartz 2 involves significant API changed, read this for older Quartz 1.6.3 example.

1. Download Quartz

You can get the Quartz library from official website or Maven central repository

File : pom.xml

<dependencies>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.1.5</version>
</dependency>
</dependencies>

Note
To deploy Quartz on Application server like JBoss, oracle or weblogic, you may need addition Quartz dependency, read this guide.

2. Quartz Job

Quartz job is defined what you want to run?

File : HelloJob

package com.mkyong.common;
 
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
public class HelloJob implements Job
{
public void execute(JobExecutionContext context)
throws JobExecutionException {
 
System.out.println("Hello Quartz!");
 
}
 
}

3. Quartz Trigger

Quartz trigger is defined when the Quartz will run your above Quartz’s job?

Like old Quartz, there are still two types of triggers in Quartz 2, but with APIs changed :

  • SimpleTrigger – Allows to set start time, end time, repeat interval.
  • CronTrigger – Allows Unix cron expression to specify the dates and times to run your job.

SimpleTrigger – Run every 5 seconds.

    Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("dummyTriggerName", "group1")
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(5).repeatForever())
.build();

CronTrigger – Run every 5 seconds.

    Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("dummyTriggerName", "group1")
.withSchedule(
CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
.build();

Note
Read this official documentation for more Quartz 2 trigger examples.

4. Scheduler

Scheduler class links both “Job” and “Trigger” together and execute it.

    	Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);

5. Full Example

Quartz 2 full examples with SimpleTrigger and CronTrigger.

SimpleTrigger example – Run very 5 seconds.

package com.mkyong.quartz;
 
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
 
public class SimpleTriggerExample {
public static void main(String[] args) throws Exception {
 
// Quartz 1.6.3
// JobDetail job = new JobDetail();
// job.setName("dummyJobName");
// job.setJobClass(HelloJob.class);
 
JobDetail job = JobBuilder.newJob(HelloJob.class)
.withIdentity("dummyJobName", "group1").build();
 
//Quartz 1.6.3
// SimpleTrigger trigger = new SimpleTrigger();
// trigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
// trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
// trigger.setRepeatInterval(30000);
 
// Trigger the job to run on the next round minute
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("dummyTriggerName", "group1")
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(5).repeatForever())
.build();
 
// schedule it
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
 
}
}

CronTrigger example – Same, run the job at every 5 seconds.

package com.mkyong.quartz; import org.quartz.CronScheduleBuilder;import org.quartz.JobBuilder;import org.quartz.JobDetail;import org.quartz.Scheduler;import org.quartz.Trigger;import org.quartz.TriggerBuilder;import org.quartz.impl.StdSchedulerFactory; public class CronTriggerExample {    public static void main( String[] args ) throws Exception    {    	//Quartz 1.6.3    	//JobDetail job = new JobDetail();    	//job.setName("dummyJobName");    	//job.setJobClass(HelloJob.class);    	    	JobDetail job = JobBuilder.newJob(HelloJob.class)		.withIdentity("dummyJobName", "group1").build(); 	//Quartz 1.6.3    	//CronTrigger trigger = new CronTrigger();    	//trigger.setName("dummyTriggerName");    	//trigger.setCronExpression("0/5 * * * * ?");     	Trigger trigger = TriggerBuilder		.newTrigger()		.withIdentity("dummyTriggerName", "group1")		.withSchedule(			CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))		.build();     	//schedule it    	Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);  }}

Download Source Code

Download it – Quartz2Example.zip (10kb)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s