Quartz.NET教程_Lesson 7: TriggerListeners and JobListeners

課程7:觸發器監聽器和作業監聽器

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).
觸發器相關的事件包括:觸發器開始執行,觸發器的未執行(在觸發器一章中曾經討論過),和觸發器的完成(作業的執行成功的被觸發器觸發完成)。

The ITriggerListener Interface

public interface ITriggerListener
{
     string Name { get; }

     void TriggerFired(ITrigger trigger, IJobExecutionContext context);

     bool VetoJobExecution(ITrigger trigger, IJobExecutionContext context);

     void TriggerMisfired(ITrigger trigger);

     void TriggerComplete(ITrigger trigger, IJobExecutionContext 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.
作業相關的事件包括:一個用來表示作業開始執行的提示,一個用來表示作業已經執行完成的提示。

The IJobListener Interface

public interface IJobListener
{
    string Name { get; }

    void JobToBeExecuted(IJobExecutionContext context);

    void JobExecutionVetoed(IJobExecutionContext context);

    void JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException);
} 

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.
如果你要創建一個監聽器,你只需要創建一個對象並實現ITriggerListener或者IJobListener接口即可。監聽器將會在運行的過程中註冊到調度器中,同時監聽器必須被命名

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.
爲了使用的方便,在實現這些接口的時候,你的類應該也拓展JobListenerSupport或者TriggerListenerSupport類,並將你需要監聽的事件重寫。

Listeners are registered with the scheduler’s ListenerManager along with a Matcher that describes which Jobs/Triggers the listener wants to receive events for.
監聽器被註冊在調度器的ListenerManager中,同時會有一個匹配器用來描述監聽器來監聽哪個具體的作業/觸發器。

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.
監聽器會在調度器運行的時候被註冊到調度器,而不是存儲在作業和觸發器中的JobStore中。因爲監聽器通常是是調度器和你自己應用程序的連接點。因此,每一次你的應用程序執行的時候,監聽器需要被重新註冊到調度器當中。

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.NET框架的用戶,通常不會使用監聽器,但是當你需要在你的應用程序中提示事件的時候會很上手,而不需要作業自己去做事件提示的工作。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章