zoukankan      html  css  js  c++  java
  • 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本身不能显式通知应用,则使用监听器非常方便。

    欢迎大家扫描下面二维码成为我的客户,为你服务和上云

  • 相关阅读:
    P2114 [NOI2014]起床困难综合症(二进制)
    P4577 [FJOI2018]领导集团问题
    P5290 [十二省联考2019]春节十二响(堆+启发式合并)
    P2048 [NOI2010]超级钢琴(RMQ+堆+贪心)
    P4890 Never·island(dp)
    P2617 Dynamic Rankings(树状数组套主席树)
    P5241 序列(滚动数组+前缀和优化dp)
    P3243 [HNOI2015]菜肴制作(拓扑排序)
    【LeetCode每天一题】Combination Sum II(组合和II)
    【LeetCode每天一题】Combination Sum(组合和)
  • 原文地址:https://www.cnblogs.com/shanyou/p/869088.html
Copyright © 2011-2022 走看看