Adding Trigger Listener using TriggerListener

In this post of Quartz, I will explain how to add listeners to listen for trigger events.

We can use the listeners to listen for trigger events and add custom logic at these points.

We need to create a class that implements org.quartz.TriggerListener interface or extends org.quartz.listeners.TriggerListenerSupport class and override interested events.

For our example we will create a class that implements org.quartz.TriggerListener interface as shown below


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

public class CustomTriggerListener implements TriggerListener {
    @Override
    public String getName() {
        return this.getClass().getName();
    }

    @Override
    public void triggerComplete(Trigger trigger, JobExecutionContext jobExecutionContext, CompletedExecutionInstruction completedExecutionInstruction) {
        System.out.println("Trigger Complete: " + trigger.getKey());
    }

    @Override
    public void triggerFired(Trigger trigger, JobExecutionContext jobExecutionContext) {
        System.out.println("Trigger Fired: " + trigger.getKey());
    }

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

    @Override
    public boolean vetoJobExecution(Trigger trigger, JobExecutionContext jobExecutionContext) {
        System.out.println("Trigger Veto Job Execution: " + trigger.getKey());
        return false;
    }
}

Next we register the listener to the scheduler using the ListenerManager as shown below in the complete main code


1  import java.io.IOException;
2  
3  import org.quartz.JobBuilder;
4  import org.quartz.JobDetail;
5  import org.quartz.Scheduler;
6  import org.quartz.SchedulerException;
7  import org.quartz.SchedulerFactory;
8  import org.quartz.SimpleScheduleBuilder;
9  import org.quartz.Trigger;
10 import org.quartz.TriggerBuilder;
11 import org.quartz.impl.StdSchedulerFactory;
12 
13 public class QuartzDemo4 {
14  public static void main(String[] args) throws SchedulerException, IOException, InterruptedException {
15      SchedulerFactory schedulerFactory = new StdSchedulerFactory();
16         Scheduler schedular = schedulerFactory.getScheduler();
17         
18         JobBuilder jobBuilder = JobBuilder.newJob(HelloJob1.class);
19         jobBuilder.withDescription("Job Description").withIdentity("jobDetail1", "group1");
20         
21         JobDetail jobDetail = jobBuilder.build();
22         
23         SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.repeatSecondlyForTotalCount(10);
24         
25         TriggerBuilder triggerBuilder = TriggerBuilder.newTrigger();
26         triggerBuilder.usingJobData("firstName", "Alice");
27         triggerBuilder.usingJobData("lastName", "Pierce");
28         triggerBuilder.withSchedule(simpleScheduleBuilder).withDescription("Trigger Description").withIdentity("trigger1", "group1").startNow();
29         
30         Trigger trigger = triggerBuilder.build();
31         
32         schedular.scheduleJob(jobDetail, trigger);
33         
34         schedular.getListenerManager().addTriggerListener(new CustomTriggerListener());
35         
36         schedular.start();
37         
38         Thread.sleep(60000);
39         
40         schedular.shutdown(true);
41  }
42 }

At line 34 we create an instance of CustomTriggerListener and register it with the scheduler using the ListenerManager.

ListenerManager is an interface that manages the various listeners registerd with the scheduler.

This will register the CustomTriggerListener to listen for events of all triggers (if they are more than one).

Output

log4j:WARN No appenders could be found for logger (org.quartz.impl.StdSchedulerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Trigger Fired: group1.trigger1
Trigger Veto Job Execution: group1.trigger1
Hello Alice Pierce Job is executing.
Trigger Complete: group1.trigger1
Trigger Fired: group1.trigger1
Trigger Veto Job Execution: group1.trigger1
Hello Alice Pierce Job is executing.
Trigger Complete: group1.trigger1
Trigger Fired: group1.trigger1
Trigger Veto Job Execution: group1.trigger1
Hello Alice Pierce Job is executing.
Trigger Complete: group1.trigger1
Trigger Fired: group1.trigger1
Trigger Veto Job Execution: group1.trigger1
Hello Alice Pierce Job is executing.
Trigger Complete: group1.trigger1
Trigger Fired: group1.trigger1
Trigger Veto Job Execution: group1.trigger1
Hello Alice Pierce Job is executing.
Trigger Complete: group1.trigger1
Trigger Fired: group1.trigger1
Trigger Veto Job Execution: group1.trigger1
Hello Alice Pierce Job is executing.
Trigger Complete: group1.trigger1
Trigger Fired: group1.trigger1
Trigger Veto Job Execution: group1.trigger1
Hello Alice Pierce Job is executing.
Trigger Complete: group1.trigger1
Trigger Fired: group1.trigger1
Trigger Veto Job Execution: group1.trigger1
Hello Alice Pierce Job is executing.
Trigger Complete: group1.trigger1
Trigger Fired: group1.trigger1
Trigger Veto Job Execution: group1.trigger1
Hello Alice Pierce Job is executing.
Trigger Complete: group1.trigger1
Trigger Fired: group1.trigger1
Trigger Veto Job Execution: group1.trigger1
Hello Alice Pierce Job is executing.
Trigger Complete: group1.trigger1

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s