Quartz Scheduler - SimpleTrigger Example

By Arvind Rai, November 20, 2023
On this page, we will provide Quartz scheduler SimpleTrigger example. 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))
	    .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2)
    		.withRepeatCount(5)).build(); 
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.
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'concretepage'
version = '1' 
repositories {
    mavenLocal()
    mavenCentral()
}
dependencies {
	compile 'org.quartz-scheduler:quartz:2.2.1'
} 

jar {
	manifest {
		attributes 'Main-Class': 'com.concretepage.SimpleTriggerExample'
	}
} 
Find the job which prints date.
PrintDateJob.java
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 {
	@Override
	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.
SimpleTriggerExample.java
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))
			    .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2)
			    		.withRepeatCount(5)).build();
		scheduler.scheduleJob(jobDetail, trigger);
		scheduler.start();
		try {
			//wait for 20 seconds to finish the job
			Thread.sleep(20000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		//shutdown scheduler gracefully
		scheduler.shutdown(true);
	}
} 
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

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us