Quartz Scheduler Developer Guide : Working with TriggerListeners and JobListeners : Creating Your Own Listeners
Creating Your Own Listeners
To create a listener, simply create an object that implements the org.quartz.TriggerListener and/or org.quartz.JobListener interface.
Listeners are then registered with the scheduler at run time, and must be given a name (or rather, they must advertise their own name via their getName() method).
For your convenience, rather than implementing these interfaces, your class could also extend the class JobListenerSupport or TriggerListenerSupport and simply override the events you're interested in.
Listeners are registered with the scheduler's ListenerManager along with a Matcher that describes the Jobs/Triggers for which the listener wants to receive events.
Note:  
Listeners are registered with the scheduler during run time, and are NOT stored in the JobStore along with the jobs and triggers. This is because listeners are typically an integration point with your application. Hence, each time your application runs, the listeners need to be re-registered with the scheduler.
Listeners are not used by most users of Quartz, but are handy when application requirements create the need for the notification of events, without the Job itself having to explicitly notify the application.
Code Samples for Adding Listeners
The following code samples illustrate ways to add JobListeners with interest in different types of jobs. Adding TriggerListeners works in a the same way.
Adding a JobListener that is interested in a particular job:
scheduler.getListenerManager().addJobListener(myJobListener,
KeyMatcher.jobKeyEquals(new JobKey("myJobName", "myJobGroup")));
You may want to use static imports for the matcher and key classes, which will make your defining the matchers cleaner:
import static org.quartz.JobKey.*;
import static org.quartz.impl.matchers.KeyMatcher.*;
import static org.quartz.impl.matchers.GroupMatcher.*;
import static org.quartz.impl.matchers.AndMatcher.*;
import static org.quartz.impl.matchers.OrMatcher.*;
import static org.quartz.impl.matchers.EverythingMatcher.*;
...etc.
Which turns the above example into this:
scheduler.getListenerManager().addJobListener(myJobListener,
jobKeyEquals(jobKey("myJobName", "myJobGroup")));
Adding a JobListener that is interested in all jobs of a particular group
scheduler.getListenerManager().addJobListener(myJobListener,
jobGroupEquals("myJobGroup"));
Adding a JobListener that is interested in all jobs of two particular groups
scheduler.getListenerManager().addJobListener(myJobListener,
or(jobGroupEquals("myJobGroup"), jobGroupEquals("yourGroup")));
Adding a JobListener that is interested in all jobs
scheduler.getListenerManager().addJobListener(myJobListener,
allJobs());
Copyright © 2010-2016 Software AG, Darmstadt, Germany.

Product Logo |   Feedback