Quartz.NET笔记(七) TriggerListeners and JobListeners

Listeners are objects that you create to perform actions based on events occuring 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).

 

监听器是在scheduler事件发生时能够执行动作的对象。可以看出,TriggerListeners接收与triggers相关的事件,而JobListeners则接收与Job相关的事件。

Trigger相关的事件包括:trigger触发、trigger未触发,以及trigger完成(由trigger触发的任务被完成)。

 

The ITriggerListener Interface

 1 public interface ITriggerListener
 2 {
 3      string Name { get; }
 4 
 5      void TriggerFired(ITrigger trigger, IJobExecutionContext context);
 6 
 7      bool VetoJobExecution(ITrigger trigger, IJobExecutionContext context);
 8 
 9      void TriggerMisfired(ITrigger trigger);
10 
11      void TriggerComplete(ITrigger trigger, IJobExecutionContext context, int triggerInstructionCode);
12 }

 

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

与任务相关的事件包括:即将被执行的任务的通知和任务已经执行完毕的通知。

 

The IJobListener Interface

 1 public interface IJobListener
 2 {
 3     string Name { get; }
 4 
 5     void JobToBeExecuted(IJobExecutionContext context);
 6 
 7     void JobExecutionVetoed(IJobExecutionContext context);
 8 
 9     void JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException);
10 } 

 

Using Your Own Listeners

To create a listener, simply create an object the implements either the ITriggerListener and/or IJobListener 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 Name property.

For your convenience, rather 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.

 

创建监听器很简单,创建一个实现Quartz.ITriggerListener或(和)Quartz.IJobListener的接口。监听器然后在执行的时候注册到scheduler中,而且必须给定一个名字(或者,它们必须通过他们的Name属性来介绍自己)。监听器可以被注册为全局的或者非全局全局监听器接收所有triggers/jobs产生的事件,而非全局监听器只接受那些通过TriggerListenerNames属性 或 JobListenerNames()方法显式指定监听器名的triggers/jobs所产生的事件。

正如上面所说的那样,监听器在运行时向scheduler注册,并且不被存储在jobs triggersJobStore中。JobsTrigger只存储了与他们相关的监听器的名字。因此,每次应用运行的时候,都需要向scheduler重新注册监听器。

 

Adding a JobListener that is interested in a particular job:

scheduler.ListenerManager.AddJobListener(myJobListener, KeyMatcher<JobKey>.KeyEquals(new JobKey("myJobName", "myJobGroup")));

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

scheduler.ListenerManager.AddJobListener(myJobListener, GroupMatcher<JobKey>.GroupEquals("myJobGroup"));

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

scheduler.ListenerManager.AddJobListener(myJobListener,
    OrMatcher<JobKey>.Or(GroupMatcher<JobKey>.GroupEquals("myJobGroup"), GroupMatcher<JobKey>.GroupEquals("yourGroup")));

Adding a JobListener that is interested in all jobs:

scheduler.ListenerManager.AddJobListener(myJobListener, GroupMatcher<JobKey>.AnyGroup());

Listeners are not used by most users of Quartz.NET, but are handy when application requirements create the need for the notification of events, without the Job itself explicitly notifying the application.

 

Quartz的大多数用户不使用监听器,但是当应用需要创建事件通知而Job本身不能显式通知应用,则使用监听器非常方便。

posted @ 2016-01-26 11:31  我在赫尔辛基火车站  阅读(429)  评论(0编辑  收藏  举报