Quartz TriggerListener Example

In this tutorial, we will show you how to create and register a Quartz TriggerListener, to keep track when a trigger is fired, misfired, vetoed or completed.

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 TriggerListener

You can create a TriggerListener either by implementing the TriggerListener or by extending the TriggerListenerSupport class. When implementing the TriggerListener you must override all the methods defined. If extending the TriggerListenerSupport class you can only override the methods you need.

package com.memorynotfound.quartz;

import org.quartz.JobExecutionContext;
import org.quartz.Trigger;
import org.quartz.TriggerListener;

public class GlobalTriggerListener implements TriggerListener {

    private static final String TRIGGER_LISTENER_NAME = "GlobalTriggerListener";

    @Override
    public String getName() {
        return TRIGGER_LISTENER_NAME;
    }

    @Override
    public void triggerFired(Trigger trigger, JobExecutionContext context) {
        String triggerName = context.getJobDetail().getKey().toString();
        System.out.println("triggerFired");
        System.out.println("trigger : " + triggerName + " is fired");
    }

    @Override
    public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
        boolean veto = false;
        System.out.println("Veto Job Excecution trigger: " + veto);
        return veto;
    }

    @Override
    public void triggerMisfired(Trigger trigger) {
        System.out.println(getName() + " trigger: " + trigger.getKey() + " misfired at " + trigger.getStartTime());
    }

    @Override
    public void triggerComplete(Trigger trigger, JobExecutionContext context, Trigger.CompletedExecutionInstruction triggerInstructionCode) {
        System.out.println(getName() + " trigger: " + trigger.getKey() + " completed at " + trigger.getStartTime());
    }
}

Schedule Quartz CronTrigger Example

You can add listeners by getting the list manager like: scheduler.getListManager() and then addTriggerListener. You can register a global trigger listener or a trigger listener that will only listen to certain trigger key.

package com.memorynotfound.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.KeyMatcher;

import static org.quartz.SimpleScheduleBuilder.*;
import static org.quartz.impl.matchers.EverythingMatcher.allJobs;

public class SimpleTriggerProgram {

    public static void main(String... args) throws SchedulerException {

        // create job
        JobDetail waffleJob = JobBuilder.newJob(BakeWaffles.class)
                .withIdentity("waffleJob", "foodJobGroup")
                .build();

        // create trigger
        TriggerKey triggerKey = new TriggerKey("waffleTrigger", "foodGroup");
        SimpleTrigger trigger = TriggerBuilder.newTrigger()
                .withIdentity(triggerKey)
                .withSchedule(
                        simpleSchedule()
                                .withIntervalInSeconds(60)
                                .repeatForever()
                ).build();

        // schedule it
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();

        // add a listener for every trigger
        scheduler.getListenerManager().addTriggerListener(new GlobalTriggerListener());

        // add a listener only for a certain triggerKey
        scheduler.getListenerManager().addTriggerListener(new GlobalTriggerListener(),
                KeyMatcher.keyEquals(triggerKey));
        scheduler.start();
        scheduler.scheduleJob(waffleJob, trigger);

    }
}

Output

triggerFired
trigger : foodJobGroup.waffleJob is fired
Veto Job Excecution trigger: false
baking waffles in: Wed Jun 17 20:12:54 CEST 2015
GlobalTriggerListener trigger: foodGroup.waffleTrigger completed at Wed Jun 17 20:12:54 CEST 2015

triggerFired
trigger : foodJobGroup.waffleJob is fired
Veto Job Excecution trigger: false
baking waffles in: Wed Jun 17 20:13:54 CEST 2015
GlobalTriggerListener trigger: foodGroup.waffleTrigger completed at Wed Jun 17 20:12:54 CEST 2015

References

Download

Download it ! – Quartz-trigger-listener.zip

You may also like...