Quartz Scheduler(2.2.1)

TriggerListeners and JobListeners

Listeners are objects that you create to perform actions based on events occurring within the scheduler. As indicated by their names, TriggerListeners receive events related to triggers, and JobListeners receive events related to jobs.

TriggerListener

Trigger-related events include: trigger firings, trigger misfirings, and trigger completions (the jobs fired off by the trigger is finished).

The org.quartz.TriggerListener Interface

public interface TriggerListener { 
    public String getName(); 
    public void triggerFired(Trigger trigger, JobExecutionContext context); 
    public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context); 
    public void triggerMisfired(Trigger trigger); 
    public void triggerComplete(Trigger trigger, JobExecutionContext context, int triggerInstructionCode); 
}

JobListener

Job-related events include: a notification that the job is about to be executed, and a notification when the job has completed execution.

The org.quartz.JobListener Interface

public interface JobListener { 
    public String getName(); 
    public void jobToBeExecuted(JobExecutionContext context); 
    public void jobExecutionVetoed(JobExecutionContext context); 
    public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException); 
}

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.

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.

scheduler.getListenerManager().addJobListener(myJobListener, KeyMatcher.jobKeyEquals(new JobKey("myJobName", "myJobGroup")));
原文地址:https://www.cnblogs.com/huey/p/5108822.html