Simple Quartz Trigger Scheduler Example
Quartz is a java enterprise ready Job Scheduler. In this tutorial we will show you a simple Quartz trigger Scheduler Example.
Maven Dependencies
First add the necessary libraries to your project. We need both org.quartz-scheduler.quartz
and code>org.quartz-scheduler.quartz-jobs.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.memorynotfound.quartz</groupId>
<artifactId>simple-trigger</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>QUARTZ - ${project.artifactId}</name>
<dependencies>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
</dependencies>
</project>
Create a Quartz Job
We can create a Quartz job by implementing the org.quartz.Job
interface which obligates us to override the execute()
method which is fired when the job gets executed. The JobExecutionContext
allows you to retrieve meta information about the current executing job.
package com.memorynotfound.quartz;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class BakeWaffles implements Job {
@Override
public void execute(JobExecutionContext ctx) throws JobExecutionException {
System.out.println("baking waffles at: " + ctx.getFireTime());
}
}
Quartz Trigger Scheduler Example
Next lets put all the pieses together. First we create a job using the JobBuilder
we give the job an identity name waffleJob with a group name foodGroup. We can use this later on if we want to register a JobListener or TriggerListener to listen only to certain events. But this is for the next tutorial. Next we create a SimpleTrigger
again we give the trigger an identity and we schedule the trigger with an interval of 3 seconds and repeat it forever. Finally we create a scheduler using the StdSchedulerFactory
and schedule the job using the scheduleJob()
method.
package com.memorynotfound.quartz;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import static org.quartz.SimpleScheduleBuilder.*;
public class SimpleTriggerExample {
public static void main(String... args) throws SchedulerException {
// create job
JobDetail job = JobBuilder.newJob(BakeWaffles.class)
.withIdentity("waffleJob", "foodGroup")
.build();
// create trigger
SimpleTrigger trigger = TriggerBuilder.newTrigger()
.withIdentity("waffleTrigger", "foodGroup")
.withSchedule(
simpleSchedule()
.withIntervalInSeconds(3)
.repeatForever()
).build();
// schedule it
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
}
}
Output
baking waffles at: Wed Jun 17 19:08:00 CEST 2015
baking waffles at: Wed Jun 17 19:08:03 CEST 2015
baking waffles at: Wed Jun 17 19:08:06 CEST 2015
baking waffles at: Wed Jun 17 19:08:09 CEST 2015
baking waffles at: Wed Jun 17 19:08:12 CEST 2015
baking waffles at: Wed Jun 17 19:08:15 CEST 2015