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=""

    <name>QUARTZ - ${project.artifactId}</name>



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 {

    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")

        // create trigger
        SimpleTrigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("waffleTrigger", "foodGroup")

        // schedule it
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.scheduleJob(job, trigger);


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



You may also like...