How-To: Using Job Listeners

Creating a JobListener

Implement the JobListener interface.

   
package foo;

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

public class MyJobListener implements JobListener {

    private String name;

    public MyJobListener(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void jobToBeExecuted(JobExecutionContext context) {
        // do something with the event
    }

    public void jobWasExecuted(JobExecutionContext context,
            JobExecutionException jobException) {
        // do something with the event
    }

    public void jobExecutionVetoed(JobExecutionContext context) {
        // do something with the event
    }
}

OR -

Extend JobListenerSupport.

   
package foo;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.listeners.JobListenerSupport;

public class MyOtherJobListener extends JobListenerSupport {

    private String name;

    public MyOtherJobListener(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
        @Override
    public void jobWasExecuted(JobExecutionContext context,
            JobExecutionException jobException) {
        // do something with the event
    }
}

Registering A JobListener With The Scheduler To Listen To All Jobs

   
scheduler.getListenerManager().addJobListener(myJobListener, allJobs());

Registering A JobListener With The Scheduler To Listen To A Specific Job

   
scheduler.getListenerManager().addJobListener(myJobListener, jobKeyEquals(jobKey("myJobName", "myJobGroup")));

Registering A JobListener With The Scheduler To Listen To All Jobs In a Group

   
scheduler.getListenerManager().addJobListener(myJobListener, jobGroupEquals("myJobGroup"));