您的位置:首页 > 其它

Lesson 7: TriggerListeners and JobListeners

2017-12-28 15:26 375 查看
http://www.quartz-scheduler.org/documentation/quartz-2.2.x/tutorials/tutorial-lesson-07.html

Listeners are objects that you create to perform actions based on events occurring within the scheduler. As you can probably guess, TriggerListeners receive events related to triggers, and JobListeners receive
events related to jobs.

Trigger-related events include: trigger firings, trigger mis-firings (discussed in the “Triggers” section of this document), and trigger completions (the jobs fired off by the trigger is finished).

监听器顾名思义是当发生某些事件是,发生对应行为的一系列对象。你可能已经猜到了,TriggerListener监听的是trigger相关的事件,JobListener监听的是job相关的事件。trigger相关的事件包括触发器哑火、触发器结束。

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);
}

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

job监听事件包括任务开始执行的通知事件和任务执行结束的通知事件。

The org.quartz.JobListener Interfacepublic interface JobListener {

public String getName();

public void jobToBeExecuted(JobExecutionContext context);

public void jobExecutionVetoed(JobExecutionContext context);

public void jobWasExecuted(JobExecutionContext context,
JobExecutionException jobException);

}


Using 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 during run time, and must be given a name (or rather, they must advertise
their own name via their getName() method).

For your convenience, tather than implementing those 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 which Jobs/Triggers the listener wants to receive events for.

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.

要创建自己的监听器,只需要实现定义的接口即可。在运行时会将监听器注册到scheduler,监听器必须制定一个name(通过实现getName方法)。

为了方便起见,也可以直接继承JobListenerSupport or TriggerListenerSupport,然后重写你的方法即可。 

listener的初始化是由ListenerManager完成的。同时还需要一个匹配所监听的job/trigger的匹配器,如下:

Adding a JobListener that is interested in a particular job:

为job添加listener

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:

为一个group的job添加listener
scheduler.getListenerManager().addJobListener(myJobListener, jobGroupEquals("myJobGroup"));


Adding a JobListener that is interested in all jobs of two particular groups:

为两个group的job添加listener:

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

为所有job添加listener

Adding a JobListener that is interested in all jobs:
scheduler.getListenerManager().addJobListener(myJobListener, allJobs());


…Registering TriggerListeners works in just the same way.

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.

triggerListener同理。

Quartz使用listener的并不多,但如果有类似的事件通知需求,而job本身没有这个功能,监听器是很有用的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: