Quartz scheduler example
Quartz is a powerful and advance scheduler framework, as it can specify the exact date and time to run your scheduler job.
1. Download Quartz
You can get the Quartz library from official website – http://www.quartz-scheduler.org/download/
or Maven central repository
Quartz Task, Job, JobDetail, Trigger, Scheduler
Firstly, try to understand the Quartz work sequence…
1. Scheduler Task – Pure Java class, the task you want to schedule.
2. Scheduler Job – Get the “Scheduler Task” from “JobDetail” via “task name“, and specify which schedule task (method) to run.
3. Scheduler JobDetail – Define a “task name” and link it with “Scheduler Job”.
4. Trigger – When will run your “Scheduler JobDetail”.
5. Scheduler – Link “JobDetail” and “Trigger” together and schedule it.
2. Scheduler Job – Get the “Scheduler Task” from “JobDetail” via “task name“, and specify which schedule task (method) to run.
3. Scheduler JobDetail – Define a “task name” and link it with “Scheduler Job”.
4. Trigger – When will run your “Scheduler JobDetail”.
5. Scheduler – Link “JobDetail” and “Trigger” together and schedule it.
1. Scheduler Task
Create a pure Java class, this is the class you want to schedule.
package com.mkyong.common;
public class RunMeTask
{
public void printMe() {
System.out.println("Run Me ~");
}
}
2. Scheduler Job
Create a ‘job’ to implement the Quartz Job interface, and also the execute() method. Get the scheduler task from Job Details via “task name”, and specify which schedule task (method) to run.
package com.mkyong.common;
import java.util.Map;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class RunMeJob implements Job
{
public void execute(JobExecutionContext context)
throws JobExecutionException {
Map dataMap = context.getJobDetail().getJobDataMap();
RunMeTask task = (RunMeTask)dataMap.get("runMeTask");
task.printMe();
}
}
3. Scheduler JobDetail
Initialize a JobDetail object, link your scheduler job with setJobClass(RunMeJob.class); method, define a task name and put it into Job data map, dataMap.put(“runMeTask”, task);. This “task name” is the only link between your Job and JobDetail.
P.S : job.setName(“runMeJob”) has no special function, just a descriptive name, you can put anything.
RunMeTask task = new RunMeTask();
//specify your sceduler task details
JobDetail job = new JobDetail();
job.setName("runMeJob");
job.setJobClass(RunMeJob.class);
Map dataMap = job.getJobDataMap();
dataMap.put("runMeTask", task);
4. Quartz Triggers
Quartz triggers are used to define when the Quartz will run your declared scheduler job. There are two types of Quartz triggers :
- SimpleTrigger – allows to set start time, end time, repeat interval to run yout job.
- CronTrigger – allows Unix cron expression to specify the dates and times to run your job.
Code snippet – SimpleTrigger
SimpleTrigger trigger = new SimpleTrigger();
trigger.setName("runMeJobTesting");
trigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
trigger.setRepeatInterval(30000);
Code snippet – CronTrigger
//configure the scheduler time
CronTrigger trigger = new CronTrigger();
trigger.setName("runMeJobTesting");
trigger.setCronExpression("0/30 * * * * ?");
5. Scheduler
Link the “JobDetail” and “Trigger” together and schedule it.
//schedule it
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
6. Full Example
Here’s two examples to show how to integrate the JobDetail, Triggers and Scheduler together.
SimpleTrigger example
In this Quartz SimpleTrigger example, it will run the printMe() method, every 30 seconds with a 1 second delay for the first time of execution.
package com.mkyong.common;
import java.util.Date;
import java.util.Map;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
public class QuartzAppSimpleTrigger
{
public static void main( String[] args ) throws Exception
{
RunMeTask task = new RunMeTask();
//specify your sceduler task details
JobDetail job = new JobDetail();
job.setName("runMeJob");
job.setJobClass(RunMeJob.class);
Map dataMap = job.getJobDataMap();
dataMap.put("runMeTask", task);
//configure the scheduler time
SimpleTrigger trigger = new SimpleTrigger();
trigger.setName("runMeJobTesting");
trigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
trigger.setRepeatInterval(30000);
//schedule it
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
}
}
CronTrigger example
In this Quartz CronTrigger example, it will run the printMe() method, every 30 seconds.
package com.mkyong.common;
import java.util.Map;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.impl.StdSchedulerFactory;
public class QuartzAppCronTrigger
{
public static void main( String[] args ) throws Exception
{
RunMeTask task = new RunMeTask();
//specify your sceduler task details
JobDetail job = new JobDetail();
job.setName("runMeJob");
job.setJobClass(RunMeJob.class);
Map dataMap = job.getJobDataMap();
dataMap.put("runMeTask", task);
//configure the scheduler time
CronTrigger trigger = new CronTrigger();
trigger.setName("runMeJobTesting");
trigger.setCronExpression("0/30 * * * * ?");
//schedule it
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
}
}
The Unix cron expression is highly flexible and powerful, you can learn and see many advance cron expression examples in following website.
1. http://en.wikipedia.org/wiki/CRON_expression
2. http://www.quartz-scheduler.org/docs/examples/Example3.html
2. http://www.quartz-scheduler.org/docs/examples/Example3.html
Reference
http://www.quartz-scheduler.org/docs/index.html