Quartz.net官方开发指南 第七课 : TriggerListeners和JobListeners

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

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

/// <summary>
 /// The interface to be implemented by classes that want to be informed when a
 /// <see cref="Trigger" /> fires. In general, applications that use a
 /// <see cref="IScheduler" /> will not have use for this mechanism.
 /// </summary>
 /// <seealso cref="IScheduler" />
 /// <seealso cref="Trigger" />
 /// <seealso cref="IJobListener" />
 /// <seealso cref="JobExecutionContext" />
 /// <author>James House</author>
 public interface ITriggerListener
 {
 /// <summary>
 /// Get the name of the <see cref="ITriggerListener" />.
 /// </summary>
 string Name { get; }
 /// <summary>
 /// Called by the <see cref="IScheduler" /> when a <see cref="Trigger" />
 /// has fired, and it's associated <see cref="JobDetail" />
 /// is about to be executed.
 /// <p>
 /// It is called before the <see cref="VetoJobExecution" /> method of this
 /// interface.
 /// </p>
 /// </summary>
 /// <param name="trigger">The <see cref="Trigger" /> that has fired.</param>
 /// <param name="context">
 /// The <see cref="JobExecutionContext" /> that will be passed to the <see cref="IJob" />'s<see cref="IJob.Execute" /> method.
 /// </param>
 void TriggerFired(Trigger trigger, JobExecutionContext context);
 /// <summary>
 /// Called by the <see cref="IScheduler" /> when a <see cref="Trigger" />
 /// has fired, and it's associated <see cref="JobDetail" />
 /// is about to be executed.
 /// <p>
 /// It is called after the <see cref="TriggerFired" /> method of this
 /// interface.
 /// </p>
 /// </summary>
 /// <param name="trigger">The <see cref="Trigger" /> that has fired.</param>
 /// <param name="context">
 /// The <see cref="JobExecutionContext" /> that will be passed to
 /// the <see cref="IJob" />'s<see cref="IJob.Execute" /> method.
 /// </param>
 bool VetoJobExecution(Trigger trigger, JobExecutionContext context);
 /// <summary>
 /// Called by the <see cref="IScheduler" /> when a <see cref="Trigger" />
 /// has misfired.
 /// <p>
 /// Consideration should be given to how much time is spent in this method,
 /// as it will affect all triggers that are misfiring. If you have lots
 /// of triggers misfiring at once, it could be an issue it this method
 /// does a lot.
 /// </p>
 /// </summary>
 /// <param name="trigger">The <see cref="Trigger" /> that has misfired.</param>
 void TriggerMisfired(Trigger trigger);
 /// <summary>
 /// Called by the <see cref="IScheduler" /> when a <see cref="Trigger" />
 /// has fired, it's associated <see cref="JobDetail" />
 /// has been executed, and it's <see cref="Trigger.Triggered" /> method has been
 /// called.
 /// </summary>
 /// <param name="trigger">The <see cref="Trigger" /> that was fired.</param>
 /// <param name="context">
 /// The <see cref="JobExecutionContext" /> that was passed to the
 /// <see cref="IJob" />'s<see cref="IJob.Execute" /> method.
 /// </param>
 /// <param name="triggerInstructionCode">
 /// The result of the call on the <see cref="Trigger" />'s<see cref="Trigger.Triggered" /> method.
 /// </param>
 void TriggerComplete(Trigger trigger, JobExecutionContext context, SchedulerInstruction triggerInstructionCode);
}

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

The.JobListener Interface

/// <summary>
 /// The interface to be implemented by classes that want to be informed when a
 /// <see cref="JobDetail" /> executes. In general, applications that use a 
 /// <see cref="IScheduler" /> will not have use for this mechanism.
 /// </summary>
 /// <seealso cref="IScheduler" />
 /// <seealso cref="IJob" />
 /// <seealso cref="JobExecutionContext" />
 /// <seealso cref="JobExecutionException" />
 /// <seealso cref="ITriggerListener" />
 /// <author>James House</author>
 public interface IJobListener
 {
 /// <summary>
 /// Get the name of the <see cref="IJobListener" />.
 /// </summary>
 string Name { get; }
 /// <summary>
 /// Called by the <see cref="IScheduler" /> when a <see cref="JobDetail" />
 /// is about to be executed (an associated <see cref="Trigger" />
 /// has occured).
 /// <p>
 /// This method will not be invoked if the execution of the Job was vetoed
 /// by a <see cref="ITriggerListener" />.
 /// </p>
 /// </summary>
 /// <seealso cref="JobExecutionVetoed(JobExecutionContext)" />
 void JobToBeExecuted(JobExecutionContext context);
 /// <summary>
 /// Called by the <see cref="IScheduler" /> when a <see cref="JobDetail" />
 /// was about to be executed (an associated <see cref="Trigger" />
  /// has occured), but a <see cref="ITriggerListener" /> vetoed it's 
 /// execution.
 /// </summary>
 /// <seealso cref="JobToBeExecuted(JobExecutionContext)" />
 void JobExecutionVetoed(JobExecutionContext context);
 /// <summary>
 /// Called by the <see cref="IScheduler" /> after a <see cref="JobDetail" />
 /// has been executed, and be for the associated <see cref="Trigger" />'s
 /// <see cref="Trigger.Triggered" /> method has been called.
 /// </summary>
 void JobWasExecuted(JobExecutionContext context, JobExecutionException jobException);
 }

使用你自定义的监听器

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

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

Scheduler中加入一个JobListener

scheduler.AddGlobalJobListener(myJobListener);

或者

scheduler.AddJobListener(myJobListener);

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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏王磊的博客

Net处理html页面元素工具类(HtmlAgilityPack.dll)的使用

简介 本文介绍net处理html页面元素的工具类(HtmlAgilityPack.dll)的使用,用途比较多的应该是例如采集类的功能,采集到的html字符串要怎...

35460
来自专栏Android开发指南

6.网络编程

347130
来自专栏cpp

TARS TC_EpollServer数据结构分析

1.NetThread负责底层数据包的收发,连接线程池管理,使用epoll ET模式;

32910
来自专栏Jerry的SAP技术分享

Cordova插件中JavaScript代码与Java的交互细节介绍

在Cordova官网中有这么一张架构图:大家看右下角蓝色的矩形框"Custom Plugin"——自定义插件。意思就是如果您用Cordova打包Mobile应用...

10320
来自专栏跟着阿笨一起玩NET

Quartz.Net使用总结

1、Quartz区分NET3.5和NET4.0版本,故新建项目的时候,引用的DLL一定要匹配net freamwork的正确版本。

35010
来自专栏Zephery

谈谈个人网站的建立(三)—— 定时任务

Quartz 先看一下Quartz的架构图: ? </div> 一.特点: 强大的调度功能,例如支持丰富多样的调度方法,可以满足各种常规及特殊需求; 灵活的...

44580
来自专栏Android-JessYan

看到如此多的MVP+Dagger2+Retrofit+Rxjava项目,轻松拿star,心动了吗?

原文地址: http://www.jianshu.com/p/4bbecd0bb027

97030
来自专栏非著名程序员

Android:全面解析熟悉而陌生 的 Application 类使用

前言 Applicaiton类在 Android开发中非常常见,可是你真的了解Applicaiton类吗? 本文将全面解析Applicaiton类,包括特点、方...

25650
来自专栏cloudskyme

使用jquery-easyui写的CRUD插件(2)

首先定义变量 var options = jQuery.extend({},jQuery.fn.crudUIGrid.defaults, options); ...

38250
来自专栏MelonTeam专栏

一种下载管理方案的设计与实现

导语: 4G时代流量资费大幅下降,各种“WBQ”卡惊艳出世——在此背景下,下载对于移动端已不再是一种昂贵的高成本行为。同时EMMC、UFS等存储介质的发展也...

330110

扫码关注云+社区

领取腾讯云代金券