同Job一样,trigger非常容易使用,但它有一些可选项需要注意和理解,同时,trigger有不同的类型,要按照需求进行选择。 <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Calendars——日历
Quartz Calendar对象在trigger被存储到scheduler时与trigger相关联。Calendar对于在trigger触发日程中的采用批量世间非常有用。例如:你想要创建一个在每个工作日上午9:30触发一个触发器,那么就添加一个排除所有节假日的日历。
Calendar可以是任何实现Calendar接口的序列化对象。看起来如下;
usingSystem;

usingQuartz.Impl.Calendar;

namespaceQuartz



{


/**////<summary>

///Aninterfacetobeimplementedbyobjectsthatdefinespacesoftimeduring

///whichanassociated<seecref="Trigger"/>mayfire.

///</summary>

///<remarks>

///Calendarsdonotdefineactualfiretimes,butratherareusedtolimita

///<seecref="Trigger"/>fromfiringonitsnormalscheduleifnecessary.Most

///Calendarsincludealltimesbydefaultandallowtheusertospecifytimesto

///exclude.Assuch,itisoftenusefultothinkofCalendarsasbeingusedto

///<i>exclude</i>ablockoftime,asopposedto<i>include</i>

///ablockoftime.(i.e.theschedule"fireeveryfiveminutesexceptonSundays"couldbe

///implementedwitha<seecref="SimpleTrigger"/>anda<seecref="WeeklyCalendar"/>whichexcludesSundays)

///</remarks>

///<author>JamesHouse</author>

///<author>JuergenDonnerstag</author>

publicinterfaceICalendar



{


/**////<summary>

///Getsorsetsadescriptionforthe<seecref="ICalendar"/>instance-maybe

///usefulforremembering/displayingthepurposeofthecalendar,though

///thedescriptionhasnomeaningtoQuartz.

///</summary>


stringDescription
{get;set;}




/**////<summary>

///Setanewbasecalendarorremovetheexistingone.

///Getthebasecalendar.

///</summary>


ICalendarCalendarBase
{set;get;}




/**////<summary>

///Determinewhetherthegiventimeis'included'bythe

///Calendar.

///</summary>

boolIsTimeIncluded(DateTimetime);




/**////<summary>

///Determinethenexttimethatis'included'bythe

///Calendarafterthegiventime.

///</summary>

DateTimeGetNextIncludedTime(DateTimetime);

}

}

注意,这些方法的参数都是DateTime型,你可以猜想出,它们的时间戳是毫秒的格式。这意味日历能够排除毫秒精度的时间。最可能的是,你可能对排除整天的时间感兴趣。为了提供方便,Quartz提供了一个Quartz.Impl.Calendar.HolidayCalendar,这个类可以排除整天的时间。
Calendars必须被实例化,然后通过AddCalendar (..)方法注册到scheduler中。如果使用HolidayCalendar,在实例化之后,你可以使用它的AddExcludedDate (DateTime excludedDate))方法来定义你想要从日程表中排除的时间。同一个calendar实例可以被用于多个trigger中,如下:
Using Calendars
ICalendarcronCalendar=newCronCalendar("0/5****?");

ICalendarholidayCalendar=newHolidayCalendar();

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

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

JobDetailjob=newJobDetail("job_"+count,schedId,typeof(SimpleRecoveryJob));

SimpleTriggertrigger=newSimpleTrigger("trig_"+count,schedId,20,5000L);

trigger.AddTriggerListener(newDummyTriggerListener().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接口的对象将可以收到触发器被触发的通知。
自由、创新、研究、探索……