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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章