Quartz.NET笔记(五) SimpleTrigger
SimpleTrigger should meet your scheduling needs if you need to have a job execute exactly once at a specific moment in time, or at a specific moment in time followed by repeats at a specific interval. Or plainer english, if you want the trigger to fire at exactly 11:23:54 AM on January 13, 2005, and then fire five more times, every ten seconds.
如果需要让任务只在某个时刻执行一次,或者,在某个时刻开始,然后按照某个时间间隔重复执行,简单地说,如果你想让触发器在2005年1月13日上午11:23:54秒执行,然后每个隔10秒钟重复执行一次,并且这样重复5次。那么SimpleTrigger 就可以满足你的要求。
With this description, you may not find it surprising to find that the properties of a SimpleTrigger include: a start-time, and end-time, a repeat count, and a repeat interval. All of these properties are exactly what you'd expect them to be, with only a couple special notes related to the end-time property.
通过这样的描述,你可能很惊奇地发现SimpleTrigger包括这些属性:开始时间,结束时间,重复次数,重复间隔。所有这属性都是你期望它所应具备的,只有end-time属性有一些条目与之关联。
The repeat count can be zero, a positive integer, or the constant value SimpleTrigger.RepeatIndefinitely. The repeat interval property must be TimeSpan.Zero, or a positive TimeSpan value. Note that a repeat interval of zero will cause 'repeat count' firings of the trigger to happen concurrently (or as close to concurrently as the scheduler can manage).
重复次数可能是0,正数或者一个常量值SimpleTrigger.REPEAT_INDEFINITELY。重复间隔时间属性可能是0,正的long型,这个数字以毫秒为单位。注意:如果指定的重复间隔时间是0,那么会导致触发器按照‘重复数量’定义的次数并发触发(或者接近并发)。
If you're not already familiar with the DateTime class, you may find it helpful for computing your trigger fire-times, depending on the startTimeUtc (or endTimeUtc) that you're trying to create.
The EndTimeUtc property (if it is specified) over-rides the repeat count property. This can be useful if you wish to create a trigger such as one that fires every 10 seconds until a given moment in time - rather than having to compute the number of times it would repeat between the start-time and the end-time, you can simply specify the end-time and then use a repeat count of RepeatIndefinitely (you could even specify a repeat count of some huge number that is sure to be more than the number of times the trigger will actually fire before the end-time arrives).
EndTime(如果这个属性被设置)属性会覆盖重复次数属性,这对创建一个每隔10秒就触发一次直到某个时间结束的触发器非常有用,这就可以不计算开始时间和结束时间之间的重复数量。也可以指定一个结束时间,然后使用REPEAT_INDEFINITELY作为重复数量。(甚至可以指定一个大于结束时间之前实际重复次数的整数作为重复次数)。一句话,EndTime属性控制权高于重复次数属性。
SimpleTrigger instances are built using TriggerBuilder (for the trigger's main properties) and WithSimpleSchedule extension method (for the SimpleTrigger-specific properties).
Build a trigger for a specific moment in time, with no repeats:
1 // trigger builder creates simple trigger by default, actually an ITrigger is returned。【trigger builder默认创建simple trigger,返回ITrigger类型】 2 ISimpleTrigger trigger = (ISimpleTrigger) TriggerBuilder.Create() 3 .WithIdentity("trigger1", "group1") 4 .StartAt(myStartTime) // some Date 5 .ForJob("job1", "group1") // identify job with name, group strings 6 .Build();
Build a trigger for a specific moment in time, then repeating every ten seconds ten times:(指定开始时间,10秒触发一次,重复10次(实际11次))
1 trigger = TriggerBuilder.Create() 2 .WithIdentity("trigger3", "group1") 3 .StartAt(myTimeToStartFiring) // if a start time is not given (if this line were omitted), "now" is implied【如果不指定开始时间,默认为当前时间】 4 .WithSimpleSchedule(x => x 5 .WithIntervalInSeconds(10) 6 .WithRepeatCount(10)) // note that 10 repeats will give a total of 11 firings 7 .ForJob(myJob) // identify job with handle to its JobDetail itself 8 .Build();
Build a trigger that will fire once, five minutes in the future:(5分钟后触发一次)
1 trigger = (ISimpleTrigger) TriggerBuilder.Create() 2 .WithIdentity("trigger5", "group1") 3 .StartAt(DateBuilder.FutureDate(5, IntervalUnit.Minute)) // use DateBuilder to create a date in the future 4 .ForJob(myJobKey) // identify job with its JobKey 5 .Build();
Build a trigger that will fire now, then repeat every five minutes, until the hour 22:00:(立即触发,5分钟重复一次,直到22:00结束)
1 trigger = TriggerBuilder.Create() 2 .WithIdentity("trigger7", "group1") 3 .WithSimpleSchedule(x => x 4 .WithIntervalInMinutes(5) 5 .RepeatForever()) 6 .EndAt(DateBuilder.DateOf(22, 0, 0)) 7 .Build();
Build a trigger that will fire at the top of the next hour, then repeat every 2 hours, forever:
1 trigger = TriggerBuilder.Create() 2 .WithIdentity("trigger8") // because group is not specified, "trigger8" will be in the default group 3 .StartAt(DateBuilder.EvenHourDate(null)) // get the next even-hour (minutes and seconds zero ("00:00")) 4 .WithSimpleSchedule(x => x 5 .WithIntervalInHours(2) 6 .RepeatForever()) 7 // note that in this example, 'forJob(..)' is not called 8 // - which is valid if the trigger is passed to the scheduler along with the job 9 .Build(); 10 11 scheduler.scheduleJob(trigger, job);
Spend some time looking at all of the available methods in the language defined by TriggerBuilder and its extension method WithSimpleSchedule so that you can be familiar with options available to you that may not have been demonstrated in the examples above.
花一些时间看看TriggerBuilder 和它的扩展方法WithSimpleSchedule 中定义的所有方法,你将会熟悉例子中没有演示到的可用选择
SimpleTrigger Misfire Instructions
SimpleTrigger has several instructions that can be used to inform Quartz.NET what it should do when a misfire occurs. (Misfire situations were introduced in the More About Triggers section of this tutorial). These instructions are defined as constants on MisfirePolicy.SimpleTrigger (including API documentation describing their behavior). The instructions include:
“未触发”发生时,SimpleTrigger有几个指令可以用来通知Quartz进行相关处理。(“未触发”在上节课中介绍过了)。这些指令以常量形式定义在SimpleTrigger本身,这些指令如下:
Misfire Instruction Constants for SimpleTrigger
- MisfireInstruction.IgnoreMisfirePolicy
- MisfirePolicy.SimpleTrigger.FireNow
- MisfirePolicy.SimpleTrigger.RescheduleNowWithExistingRepeatCount
- MisfirePolicy.SimpleTrigger.RescheduleNowWithRemainingRepeatCount
- MisfirePolicy.SimpleTrigger.RescheduleNextWithRemainingCount
- MisfirePolicy.SimpleTrigger.RescheduleNextWithExistingCount
You should recall from the earlier lessons that all triggers have the MisfirePolicy.SmartPolicy instruction available for use, and this instruction is also the default for all trigger types.
回顾前面的课程你可以知道,每个触发器都有一个Trigger.MISFIRE_INSTRUCTION_SMART_POLICY指令可用,并且,这个指令对于每个类型的触发器都是缺省的。
If the 'smart policy' instruction is used, SimpleTrigger dynamically chooses between its various MISFIRE instructions, based on the configuration and state of the given SimpleTrigger instance. The documentation for the SimpleTrigger.UpdateAfterMisfire() method explains the exact details of this dynamic behavior.
When building SimpleTriggers, you specify the misfire instruction as part of the simple schedule (via SimpleSchedulerBuilder):
1 trigger = TriggerBuilder.Create() 2 .WithIdentity("trigger7", "group1") 3 .WithSimpleSchedule(x => x 4 .WithIntervalInMinutes(5) 5 .RepeatForever() 6 .WithMisfireHandlingInstructionNextWithExistingCount()) 7 .Build();