Quartz.NET教程_Lesson 5: SimpleTrigger

課程5:簡單觸發器


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.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.

你也許會想起來所有的觸發器都有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();

碼字不易,與君共勉!:

碼字不易,與君共勉!

發佈了31 篇原創文章 · 獲贊 39 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章