Quartz.NET教程_Lesson 6: CronTrigger

課程6:克龍觸發器

CronTriggers are often more useful than SimpleTrigger, if you need a job-firing schedule that recurs based on calendar-like notions, rather than on the exactly specified intervals of SimpleTrigger.

克龍觸發器有時候會比簡單觸發器更好用,當你想要構建一個基於日曆形式的作業調度任務,而不是一個需要特定時間間隔的簡單觸發器。

With CronTrigger, you can specify firing-schedules such as “every Friday at noon”, or “every weekday and 9:30 am”, or even “every 5 minutes between 9:00 am and 10:00 am on every Monday, Wednesday and Friday”.

在使用克龍觸發器的時候,你可以詳細定義你的調度任務,比如“每個週五的中午”或者“每個工作日的上午9:30”,再或者是“每個週一,週三,週五的9:00到10:00之間,沒5分鐘執行一次”。

Even so, like SimpleTrigger, CronTrigger has a startTime which specifies when the schedule is in force, and an (optional) endTime that specifies when the schedule should be discontinued.

即便如此,和簡單觸發器一樣,克龍觸發器同樣有一個開始時間,用於定義觸發器開始執行的時間,和結束時間,用於定義調度任務什麼時候應該終止。


Cron Expressions
克龍表達式

Cron-Expressions are used to configure instances of CronTrigger. Cron-Expressions are strings that are actually made up of seven sub-expressions, that describe individual details of the schedule. These sub-expression are separated with white-space, and represent:

克龍表達式被用來對克龍觸發器的實例進行配置。克龍表達式實際上是由7個子表達式構成的用於表述調度任務詳細細節的字符串。這些表達式由空格進行分割,並且表達如下含義:

    Seconds
    Minutes
    Hours
    Day-of-Month
    Month
    Day-of-Week
    Year (optional field)

An example of a complete cron-expression is the string “0 0 12 ? * WED” - which means “every Wednesday at 12:00 pm”.

比如一個完整的克龍表達式字符串“0 0 12 ? * WED” - 表示每個星期三的下午12點。

Individual sub-expressions can contain ranges and/or lists. For example, the day of week field in the previous (which reads “WED”) example could be replaces with “MON-FRI”, “MON, WED, FRI”, or even “MON-WED,SAT”.

每個子表達式可以包含區間和/或者列表。比如“Day-of-Week”這個子表達式可以被替換成“MON-FRI”,“MON, WED, FRI”,甚至是“MON-WED,SAT”。

Wild-cards (the ‘’ character) can be used to say “every” possible value of this field. Therefore the ‘’ character in the “Month” field of the previous example simply means “every month”. A ‘*’ in the Day-Of-Week field would obviously mean “every day of the week”.

通配符(‘’符號,這個地方有問題,說的是?吧)可以被用來表述這個域裏面的每一個可能的值。然而之前例子中的月份子表達式中的?表示的是每個月。在“Day-Of-Week”中的*表示則很明顯表示的是“一週的每一天”。


All of the fields have a set of valid values that can be specified. These values should be fairly obvious - such as the numbers 0 to 59 for seconds and minutes, and the values 0 to 23 for hours. Day-of-Month can be any value 0-31, but you need to be careful about how many days are in a given month! Months can be specified as values between 0 and 11, or by using the strings JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV and DEC. Days-of-Week can be specified as vaules between 1 and 7 (1 = Sunday) or by using the strings SUN, MON, TUE, WED, THU, FRI and SAT.

所有的區域都有一些有效值得集合可以被用來詳細定義。這些值應該看上去很直白 - 比如數字0-59表示秒和分鐘,值0-23表示小時。Day-of-Month/一個月中的某天可以是0-31中的任意值,但是你得注意每個月得有多少天啊!月份可以被詳細設置爲0-11之間,或者通過這些字符串來進行具體的表示:JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV and DEC。一週中的某一天可以用1-7之前的任意值進行表示,或者通過使用下面的字符串表示:SUN, MON, TUE, WED, THU, FRI and SAT.


The ‘/’ character can be used to specify increments to values. For example, if you put ‘0/15’ in the Minutes field, it means ‘every 15 minutes, starting at minute zero’. If you used ‘3/20’ in the Minutes field, it would mean ‘every 20 minutes during the hour, starting at minute three’ - or in other words it is the same as specifying ‘3,23,43’ in the Minutes field.

‘/’符號可以被用來詳細定義值得增長。比如,你在分鐘區域使用‘0/15’,表示的是‘從0分開始,每15分鐘執行一次’。如果在分鐘區域用‘3/20’,則可以表示‘從3分開始,一小時中每20分鐘執行一次’ - 或者換另一句話說,在分鐘區域用詳細定義的方法‘3,23,43’來表示。


The ‘?’ character is allowed for the day-of-month and day-of-week fields. It is used to specify “no specific value”. This is useful when you need to specify something in one of the two fields, but not the other. See the examples below (and CronTrigger API documentation) for clarification.

‘?’符號可以允許用來描述一個月中的一天,或者一週中的一天。它表示的含義是“沒有一個確定的值”。如果你希望在這兩個區域中的一個進行設置的時候,這種方式還是很有用的。看下面的例子(和克龍觸發器的接口文檔),以便更詳細的弄清楚細節。


The ‘L’ character is allowed for the day-of-month and day-of-week fields. This character is short-hand for “last”, but it has different meaning in each of the two fields. For example, the value “L” in the day-of-month field means “the last day of the month” - day 31 for January, day 28 for February on non-leap years. If used in the day-of-week field by itself, it simply means “7” or “SAT”. But if used in the day-of-week field after another value, it means “the last xxx day of the month” - for example “6L” or “FRIL” both mean “the last friday of the month”. When using the ‘L’ option, it is important not to specify lists, or ranges of values, as you’ll get confusing results.

‘L’符號可以允許用來表述“一個月中的一天”和“一週中的一天”兩個區域。這個符號是單詞“最後一次”的縮寫,但是在這兩個不同的子表達式區域,他們有不同的含義。比如,在一個月中的一天,這個區域的‘L’符號,用來表示“這個月最後一天” - 非閏年的一月份的31號,二月份的28號。如果你在一週的一天使用這個值,則表示的是“7”或者“SAT”。但是如果你在一週的一天這個區域使用‘L’接在某個值後面,表示的則是“這個月最後一個周*(週一,週二等)”比如,“6L”或者“FRIL”都表示“該月最後一個星期五”。當你在使用“L”符號的時候,你別把詳細列表定義死了,或者值的區間定義死,要不然估計你會被結果弄混。


The ‘W’ is used to specify the weekday (Monday-Friday) nearest the given day. As an example, if you were to specify “15W” as the value for the day-of-month field, the meaning is: “the nearest weekday to the 15th of the month”.

‘W’符號用來表示離給定日期最近的那個工作日。比如說,你詳細定義一個月中的一天區域的值爲“15W”,表示的是“這個月離15號最近的那個工作日”。


The ‘#’ is used to specify “the nth” XXX weekday of the month. For example, the value of “6#3” or “FRI#3” in the day-of-week field means “the third Friday of the month”.
Example Cron Expressions

‘#’符號用來詳細表示這個月的“第N個” 周*。比如下面例子,一週中的一天區域中的“6#3”或者“FRI#3”表示“這個月第三個週五”。

Here are a few more examples of expressions and their meanings - you can find even more in the API documentation for CronTrigger:

下面是一些關於相關表達式的例子 - 你可以在克龍觸發器的接口文檔中找到更多的實例:

CronTrigger Example 1 - an expression to create a trigger that simply fires every 5 minutes

"0 0/5 * * * ?"

CronTrigger Example 2 - an expression to create a trigger that fires every 5 minutes, at 10 seconds after the minute (i.e. 10:00:10 am, 10:05:10 am, etc.).

"10 0/5 * * * ?"

CronTrigger Example 3 - an expression to create a trigger that fires at 10:30, 11:30, 12:30, and 13:30, on every Wednesday and Friday.

"0 30 10-13 ? * WED,FRI"

CronTrigger Example 4 - an expression to create a trigger that fires every half hour between the hours of 8 am and 10 am on the 5th and 20th of every month. Note that the trigger will NOT fire at 10:00 am, just at 8:00, 8:30, 9:00 and 9:30

"0 0/30 8-9 5,20 * ?"

Note that some scheduling requirements are too complicated to express with a single trigger - such as “every 5 minutes between 9:00 am and 10:00 am, and every 20 minutes between 1:00 pm and 10:00 pm”. The solution in this scenario is to simply create two triggers, and register both of them to run the same job.

要注意有一些調度的任務要求太過於複雜以至於不能用一個單獨的觸發器進行描述 - 比如“上午9:00到10:00之間每5分鐘執行一次,同時下午1:00到10:00之間每20分鐘執行一次”。這個場景下的解決方案其實很簡單,就是構建兩個觸發器,然後在同一個作業中進行執行。


Building CronTriggers
構建克龍觸發器

CronTrigger instances are built using TriggerBuilder (for the trigger’s main properties) and WithCronSchedule extension method (for the CronTrigger-specific properties).

克龍觸發器的實例通過觸發器構建者(用於提供觸發器需要的主要屬性)和WithCronSchedule拓展方法(用於定義克龍觸發器特有的屬性)進行構建。

You can also use CronScheduleBuilder’s static methods to create schedules.
你也可以通過克龍觸發器構建者類的靜態方法進行調度任務的構建。

Build a trigger that will fire every other minute, between 8am and 5pm, every day:
構建的一個觸發器,讓其每天的上午8點到下午5點之間,每2分鐘執行一次:

trigger = TriggerBuilder.Create()
    .WithIdentity("trigger3", "group1")
    .WithCronSchedule("0 0/2 8-17 * * ?")
    .ForJob("myJob", "group1")
    .Build();

Build a trigger that will fire daily at 10:42 am:
構建一個每天上午10:42執行的觸發器:

// we use CronScheduleBuilder's static helper methods here
trigger = TriggerBuilder.Create()
    .WithIdentity("trigger3", "group1")
    .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(10, 42))
    .ForJob(myJobKey)
    .Build();

or -

trigger = TriggerBuilder.Create()
    .WithIdentity("trigger3", "group1")
    .WithCronSchedule("0 42 10 * * ?")
    .ForJob("myJob", "group1")
    .Build();

Build a trigger that will fire on Wednesdays at 10:42 am, in a TimeZone other than the system’s default:

構建一個觸發器在特定時區的週三上午10:42執行,而不是系統默認的時區:

trigger = TriggerBuilder.Create()
    .WithIdentity("trigger3", "group1")
    .WithSchedule(CronScheduleBuilder
        .WeeklyOnDayAndHourAndMinute(DayOfWeek.Wednesday, 10, 42)
        .InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Central America Standard Time")))
    .ForJob(myJobKey)
    .Build();

or -

trigger = TriggerBuilder.Create()
    .WithIdentity("trigger3", "group1")
    .WithCronSchedule("0 42 10 ? * WED", x => x
        .InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Central America Standard Time")))
    .ForJob(myJobKey)
    .Build();

CronTrigger Misfire Instructions
克龍觸發器的異常處理方案

The following instructions can be used to inform Quartz what it should do when a misfire occurs for CronTrigger. (Misfire situations were introduced in the More About Triggers section of this tutorial). These instructions are defined in as constants (and API documentation has description for their behavior). The instructions include:
下面的應急處理方案可以用來告知Quartz框架,當克龍觸發器出現異常時,應該怎麼處理(應急處理方案之前就介紹過了)。這些應急處理方案,可以以被定義爲常量(接口文檔中對每一種方案的行爲都進行了描述)。這些應急方案包括:

MisfireInstruction.IgnoreMisfirePolicy
MisfireInstruction.CronTrigger.DoNothing
MisfireInstruction.CronTrigger.FireOnceNow

All triggers have the MisfireInstrution.SmartPolicy instruction available for use, and this instruction is also the default for all trigger types. The ‘smart policy’ instruction is interpreted by CronTrigger as MisfireInstruction.CronTrigger.FireOnceNow. The API documentation for the CronTrigger.UpdateAfterMisfire() method explains the exact details of this behavior.
所有的觸發器都有MisfireInstrution.SmartPolicy方案,這也是所有觸發器默認的應急處理方案。這個“機智策略”方案會被克龍觸發器解析成MisfireInstruction.CronTrigger.FireOnceNow.接口文檔中CronTrigger.UpdateAfterMisfire()方法對這個行爲的細節進行了詳細的解釋。

When building CronTriggers, you specify the misfire instruction as part of the cron schedule (via WithCronSchedule extension method):
當構建克龍觸發器是,你可以定義應急處理方法爲整個克龍調度任務的一部分(通過WithCronSchedule拓展方法):

trigger = TriggerBuilder.Create()
    .WithIdentity("trigger3", "group1")
    .WithCronSchedule("0 0/2 8-17 * * ?", x => x
        .WithMisfireHandlingInstructionFireAndProceed())
    .ForJob("myJob", "group1")
    .Build();

希望我的文字能給你帶來幫助:

碼字不易,與君共勉!

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