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这个精确时间点执行,然后每隔十秒钟执行一次,多执行5次。
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.
这种的描述方式,你会发现简单触发器中包含的内容都顺理成章:一个开始时间,结束时间,重复次数,重复的间隔。所有这些属性都是你希望其严格执行的方式,只有结束时间这一个属性有一些小的细节,你需要格外注意一下。
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.RepeatIndefinitely 常量。重复的间隔属性必须是一个时间段。0,或者一个正的时间段值。注意如果一个重复间隔的值被设置成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.
如果你还没有学会使用DateTime类,你会发现,通过借助你想创建的开始时间,计算触发器的触发次数是一种更为有效的方式。
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).
结束时间属性(如果是一个精确的值)会覆盖重复次数的属性。在你想要创建一个触发器,希望他每十秒钟执行一次,一直到最后的一个给定时间的情况下会很有帮助,这样就不必去通过你设定的开始时间和结束时间算出时间间隔次数了。你可以简单的通过设定结束时间,然后将重复次数属性设置为RepeatIndefinitely(你甚至可以详细定义一个触发器在结束时间到来之前,比实际执行次数大的多的数量)即可。
SimpleTrigger instances are built using TriggerBuilder (for the trigger’s main properties) and WithSimpleSchedule extension method (for the SimpleTrigger-specific properties).
简单触发器实例是通过TriggerBuilder方法(触发器的主要属性都由其创建)和WithSimpleSchedule拓展方法完成的(简单触发器的具体属性由其定义)。
Build a trigger for a specific moment in time, with no repeats: 构造一个特定时间,没有重复的触发器:
// trigger builder creates simple trigger by default, actually an ITrigger is returned ISimpleTrigger trigger = (ISimpleTrigger) TriggerBuilder.Create() .WithIdentity("trigger1", "group1") .StartAt(myStartTime) // some Date .ForJob("job1", "group1") // identify job with name, group strings .Build(); Build a trigger for a specific moment in time, then repeating every ten seconds ten times: trigger = TriggerBuilder.Create() .WithIdentity("trigger3", "group1") .StartAt(myTimeToStartFiring) // if a start time is not given (if this line were omitted), "now" is implied .WithSimpleSchedule(x => x .WithIntervalInSeconds(10) .WithRepeatCount(10)) // note that 10 repeats will give a total of 11 firings .ForJob(myJob) // identify job with handle to its JobDetail itself .Build(); Build a trigger that will fire once, five minutes in the future: trigger = (ISimpleTrigger) TriggerBuilder.Create() .WithIdentity("trigger5", "group1") .StartAt(DateBuilder.FutureDate(5, IntervalUnit.Minute)) // use DateBuilder to create a date in the future .ForJob(myJobKey) // identify job with its JobKey .Build();Build a trigger that will fire now, then repeat every five minutes, until the hour 22:00: 构建一个立刻执行的触发器,然后每5分钟执行一次,22:00结束执行:
trigger = TriggerBuilder.Create() .WithIdentity("trigger7", "group1") .WithSimpleSchedule(x => x .WithIntervalInMinutes(5) .RepeatForever()) .EndAt(DateBuilder.DateOf(22, 0, 0)) .Build();Build a trigger that will fire at the top of the next hour, then repeat every 2 hours, forever: 构建一个触发器在下一个整点执行,然后每2小时一次,永久执行:
trigger = TriggerBuilder.Create() .WithIdentity("trigger8") // because group is not specified, "trigger8" will be in the default group .StartAt(DateBuilder.EvenHourDate(null)) // get the next even-hour (minutes and seconds zero ("00:00")) .WithSimpleSchedule(x => x .WithIntervalInHours(2) .RepeatForever()) // note that in this example, 'forJob(..)' is not called // - which is valid if the trigger is passed to the scheduler along with the job .Build(); 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:
简单触发器有一些不同的应急方案用来告知Quartz.NET框架,当某种异常发生时该怎么处理(应急处理方案在之前的详解触发器一章中已经介绍过了)。
Misfire Instruction Constants for SimpleTrigger 简单触发器应急处理方案的常量
MisfireInstruction.IgnoreMisfirePolicy MisfirePolicy.SimpleTrigger.FireNow MisfirePolicy.SimpleTrigger.RescheduleNowWithExistingRepeatCount MisfirePolicy.SimpleTrigger.RescheduleNowWithRemainingRepeatCount MisfirePolicy.SimpleTrigger.RescheduleNextWithRemainingCount MisfirePolicy.SimpleTrigger.RescheduleNextWithExistingCountYou 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.
你也许会想起来所有的触发器都有MisfirePolicy.SmartPolicy可以使用,并且这个属性也是所有触发器的默认应急处理属性。
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.
如果这个“机智策略”应对属性被使用了,简单触发器会动态的在配置和给定的简单触发器实例基础上,在这些不同的方案中选择一个合适的结果。SimpleTrigger.UpdateAfterMisfire()方法的文档说明清晰的揭示出了其动态行为的详细处理。
When building SimpleTriggers, you specify the misfire instruction as part of the simple schedule (via SimpleSchedulerBuilder):
当简单触发器正在构建的过程中,你可以将异常应对属性作为一个简单调度任务的一部分进行处理(通过SimpleSchedulerBuilder/简单调度器构造者实现):
trigger = TriggerBuilder.Create() .WithIdentity("trigger7", "group1") .WithSimpleSchedule(x => x .WithIntervalInMinutes(5) .RepeatForever() .WithMisfireHandlingInstructionNextWithExistingCount()) .Build();码字不易,与君共勉!: