首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Quartz.net官方开发指南 第四课:关于Triggers更多内容

Quartz.net官方开发指南 第四课:关于Triggers更多内容

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

同Job一样,trigger非常容易使用,但它有一些可选项需要注意和理解,同时,trigger有不同的类型,要按照需求进行选择。

Calendars——日历

Quartz Calendar对象在trigger被存储到scheduler时与trigger相关联。Calendar对于在trigger触发日程中的采用批量世间非常有用。例如:你想要创建一个在每个工作日上午9:30触发一个触发器,那么就添加一个排除所有节假日的日历。

Calendar可以是任何实现Calendar接口的序列化对象。看起来如下;

using System;

using Quartz.Impl.Calendar; 

namespace Quartz

{

     /// <summary> 

     ///  An interface to be implemented by objects that define spaces of time during 

    /// which an associated <see cref="Trigger" /> may fire. 

    /// </summary>

    /// <remarks>

    /// Calendars do not  define actual fire times, but rather are used to limit a 

    /// <see cref="Trigger" />  from firing on its normal schedule if necessary. Most 

    /// Calendars include all  times by default and allow the user to specify times to

    /// exclude. As such, it  is often useful to think of Calendars as being used to

    /// <i>exclude</i> a block of time, as opposed to <i>include</i> 

    /// a block of time. (i.e. the  schedule &quot;fire every five minutes except on Sundays&quot; could be 

    /// implemented with a <see cref="SimpleTrigger" /> and a <see cref="WeeklyCalendar" /> which excludes Sundays)

    /// </remarks>

     /// <author>James House</author>

     /// <author>Juergen Donnerstag</author>

     public interface ICalendar

     {

         /// <summary> 

         /// Gets or sets a description for the <see cref="ICalendar" /> instance - may be

         /// useful for remembering/displaying the purpose of the calendar, though

         /// the description has no meaning to Quartz.

         /// </summary>

         string Description { get; set; }

 

         /// <summary>

         /// Set a new base calendar or remove the existing one.

         /// Get the base calendar.

         /// </summary>

         ICalendar CalendarBase { set; get; }

 

          /// <summary>

         /// Determine whether the given time  is 'included' by the

         /// Calendar.

         /// </summary>

         bool IsTimeIncluded(DateTime time);

 

         /// <summary>

         /// Determine the next time that is 'included' by the

         /// Calendar after the given time.

         /// </summary>

         DateTime GetNextIncludedTime(DateTime time);

     }

}

  注意,这些方法的参数都是DateTime型,你可以猜想出,它们的时间戳是毫秒的格式。这意味日历能够排除毫秒精度的时间。最可能的是,你可能对排除整天的时间感兴趣。为了提供方便,Quartz提供了一个Quartz.Impl.Calendar.HolidayCalendar,这个类可以排除整天的时间。

Calendars必须被实例化,然后通过AddCalendar (..)方法注册到scheduler中。如果使用HolidayCalendar,在实例化之后,你可以使用它的AddExcludedDate (DateTime excludedDate))方法来定义你想要从日程表中排除的时间。同一个calendar实例可以被用于多个trigger中,如下:

Using Calendars

ICalendar cronCalendar = new CronCalendar("0/5 * * * * ?");

ICalendar holidayCalendar = new HolidayCalendar();       

sched.AddCalendar("cronCalendar", cronCalendar, true, true);

sched.AddCalendar("holidayCalendar", holidayCalendar, true, true);

JobDetail job = new JobDetail("job_" + count, schedId, typeof (SimpleRecoveryJob));

SimpleTrigger trigger = new SimpleTrigger("trig_" + count, schedId, 20, 5000L);

trigger.AddTriggerListener(new DummyTriggerListener().Name);

trigger.StartTime = DateTime.Now.AddMilliseconds(1000L);

sched.ScheduleJob(job, trigger);

传入SimpleTrigger构造函数的参数的细节将在下章中介绍。但是,任何在日历中被排除的时间所要进行的触发都被取消。

Misfire Instructions——未触发指令

Trigger的另一个重要属性就是它的“misfire instruction(未触发指令)”。如果因为scheduler被关闭而导致持久的触发器“错过”了触发时间,这时,未触发就发生了。不同类型的触发器有不同的未触发指令。缺省情况下,他们会使用一个“智能策略”指令——根据触发器类型和配置的不同产生不同动作。当scheduler开始时,它查找所有未触发的持久triggers,然后按照每个触发器所配置的未触发指令来更新它们。开始工程中使用Quartz的时,应熟悉定义在各个类型触发器上的未触发指令。关于未触发指令信息的详细说明将在每种特定的类型触发器的指南课程中给出。可以通过MisfireInstruction属性来为给定的触发器实例配置未触发指令。

TriggerUtils - Triggers Made EasyTriggerUtils——使Triggers变得容易)

TriggerUtils类包含了创建触发器以及日期的便捷方法。使用这个类可以轻松地使触发器在每分钟,小时,日,星期,月等触发。使用这个类也可以产生距离触发最近的妙、分或者小时,这对设定触发开始时间非常有用。

TriggerListeners

最后,如同job一样,triggers可以注册监听器,实现TriggerListener接口的对象将可以收到触发器被触发的通知。

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

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

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

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

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