您的位置:首页 > 其它

Quartz.NET教程_Lesson 7: TriggerListeners and JobListeners

2016-09-16 20:14 453 查看

课程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框架的用户,通常不会使用监听器,但是当你需要在你的应用程序中提示事件的时候会很上手,而不需要作业自己去做事件提示的工作。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息