Quartz.net官方开发指南 第四课:关于Triggers更多内容
2007-08-19 21:34:00
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://shanyou.blog.51cto.com/363653/74127 |
同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 "fire every five minutes except on Sundays" 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 Easy(TriggerUtils——使Triggers变得容易)
TriggerUtils类包含了创建触发器以及日期的便捷方法。使用这个类可以轻松地使触发器在每分钟,小时,日,星期,月等触发。使用这个类也可以产生距离触发最近的妙、分或者小时,这对设定触发开始时间非常有用。
TriggerListeners
最后,如同job一样,triggers可以注册监听器,实现TriggerListener接口的对象将可以收到触发器被触发的通知。
自由、创新、研究、探索…… 本文出自 “张善友” 博客,请务必保留此出处http://shanyou.blog.51cto.com/363653/74127 本文出自 51CTO.COM技术博客 |








}
张善友
博客统计信息
热门文章
最新评论
友情链接