前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Quartz.net官方开发指南 第七课 : TriggerListeners和JobListeners

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

作者头像
张善友
发布2018-01-19 15:28:42
4790
发布2018-01-19 15:28:42
举报
文章被收录于专栏:张善友的专栏张善友的专栏

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

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

代码语言:js
复制
/// <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

代码语言:js
复制
/// <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本身不能显式通知应用,则使用监听器非常方便。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2007-08-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档