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 triggersJobStore 中。 JobsTrigger 只存储了与他们相关的监听器的名字。因此,每次应用运行的时候,都需要向 scheduler 重新注册监听器。
 
向 Scheduler中加入一个JobListener
scheduler. AddGlobalJobListener (myJobListener);
或者
scheduler.AddJobListener(myJobListener);
Quartz 的大多数用户不使用监听器,但是当应用需要创建事件通知而 Job 本身不能显式通知应用,则使用监听器非常方便。





本文转自 张善友 51CTO博客,原文链接:http://blog.51cto.com/shanyou/73992,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值