Home  >  Quartz

Quartz 2 Scheduler SimpleTrigger Example with SimpleScheduleBuilder

By Arvind Rai, May 23, 2015
In this page, we will provide Quartz 2 scheduler SimpleTrigger example with SimpleScheduleBuilder. SimpleTrigger is used to fire a Job at a given start time and if needed, it can repeat for a given interval of time or can run infinite. SimpleScheduleBuilder implements ScheduleBuilder. SimpleScheduleBuilder defines interval based schedules for the trigger. Here we will discuss a complete example step by step.

SimpleTrigger is instantiated using TriggerBuilder. Find the code snippet.
SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("greattrigger", "mygroup")
    .startAt(new Date(Calendar.getInstance().getTimeInMillis()+ 3000))
While instantiating TriggerBuilder provides different methods for its configuration.

1.withIdentity() : This method is used to provide a unique identity to trigger. We pass trigger name and group name.
2.startAt() : Using this method we set the time when trigger will start for the first time.
3. withSchedule() : Here we provide a ScheduleBuilder. In our example we are using SimpleScheduleBuilder.

SimpleScheduleBuilder is instantiated using simpleSchedule() method. We can configure SimpleScheduleBuilder using its different method.

1. withIntervalInSeconds() : Using this method, we specify the interval time in seconds for trigger execution.
2. withRepeatCount() : Here we specify how many times the trigger will repeat. If we specify n, it means complete number of time of trigger execution is n+1.

Complete Example

Find the gradle file to resolve Quartz 2 Jar dependencies.
apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'concretepage'
version = '1' 
repositories {
dependencies {
	compile 'org.quartz-scheduler:quartz:2.2.1'

jar {
	manifest {
		attributes 'Main-Class': 'com.concretepage.SimpleTriggerExample'
Find the job which prints date.
package com.concretepage.job;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
public class PrintDateJob implements Job {
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		JobKey jobKey = context.getJobDetail().getKey();
		System.out.println(jobKey+": "+ new Date());
Find the scheduler now. It schedules the job after 3 second and will repeat with 2 second interval for 5+1 number of time. Scheduler will sleep for 20 second to let the job complete and after that it gracefully shutdown.
package com.concretepage;
import java.util.Calendar;
import java.util.Date;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.SimpleTrigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import com.concretepage.job.PrintDateJob;
public class SimpleTriggerExample {
	public static void main(String[] args) throws SchedulerException {
		SchedulerFactory schedulerFactory = new StdSchedulerFactory();
		Scheduler scheduler = schedulerFactory.getScheduler();
		JobDetail jobDetail = JobBuilder.newJob(PrintDateJob.class).withIdentity("goodjob", "mygroup").build();
		SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("greattrigger", "mygroup")
			    .startAt(new Date(Calendar.getInstance().getTimeInMillis()+ 3000))
		scheduler.scheduleJob(jobDetail, trigger);
		try {
			//wait for 20 seconds to finish the job
		} catch (InterruptedException e) {
		//shutdown scheduler gracefully
Find the output.
 mygroup.goodjob: Sat May 23 11:55:27 IST 2015
mygroup.goodjob: Sat May 23 11:55:29 IST 2015
mygroup.goodjob: Sat May 23 11:55:31 IST 2015
mygroup.goodjob: Sat May 23 11:55:33 IST 2015
mygroup.goodjob: Sat May 23 11:55:35 IST 2015
mygroup.goodjob: Sat May 23 11:55:37 IST 2015 

Download Complete Source Code


©2019 concretepage.com | Privacy Policy | Contact Us