Adding Job Listener using JobListener Interface

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

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

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

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


package package5;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;

public class CustomJobListener implements JobListener {

    @Override
    public String getName() {
        return this.getClass().getName();
    }

    @Override
    public void jobExecutionVetoed(JobExecutionContext jobExecutionContext) {
        System.out.println("Job Execution Vetoed: " + jobExecutionContext.getJobDetail().getKey());
    }

    @Override
    public void jobToBeExecuted(JobExecutionContext jobExecutionContext) {
        System.out.println("Job To Be Executed: " + jobExecutionContext.getJobDetail().getKey());
    }

    @Override
    public void jobWasExecuted(JobExecutionContext jobExecutionContext, JobExecutionException jobExecutionException) {
        System.out.println("Job Was Executed: " + jobExecutionContext.getJobDetail().getKey());
    }
}

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


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

At line 35 we create an instance of CustomJobListener 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 CustomJobListener to listen for events of all jobs (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.
Job To Be Executed: group1.jobDetail1
Hello Alice Pierce Job is executing.
Job Was Executed: group1.jobDetail1
Job To Be Executed: group1.jobDetail1
Hello Alice Pierce Job is executing.
Job Was Executed: group1.jobDetail1
Job To Be Executed: group1.jobDetail1
Hello Alice Pierce Job is executing.
Job Was Executed: group1.jobDetail1
Job To Be Executed: group1.jobDetail1
Hello Alice Pierce Job is executing.
Job Was Executed: group1.jobDetail1
Job To Be Executed: group1.jobDetail1
Hello Alice Pierce Job is executing.
Job Was Executed: group1.jobDetail1
Job To Be Executed: group1.jobDetail1
Hello Alice Pierce Job is executing.
Job Was Executed: group1.jobDetail1
Job To Be Executed: group1.jobDetail1
Hello Alice Pierce Job is executing.
Job Was Executed: group1.jobDetail1
Job To Be Executed: group1.jobDetail1
Hello Alice Pierce Job is executing.
Job Was Executed: group1.jobDetail1
Job To Be Executed: group1.jobDetail1
Hello Alice Pierce Job is executing.
Job Was Executed: group1.jobDetail1
Job To Be Executed: group1.jobDetail1
Hello Alice Pierce Job is executing.
Job Was Executed: group1.jobDetail1

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