• Quartz.NET笔记(六) CronTrigger


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

    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.

    如果你需要像日历那样按日程来触发任务,而不是像SimpleTrigger 那样每隔特定的间隔时间触发,CronTriggers通常比SimpleTrigger更有用。

    使用CronTrigger,你可以指定诸如“每个周五中午”,或者“每个工作日的9:30”或者“从每个周一、周三、周五的上午9:00到上午10:00之间每隔五分钟”这样日程安排来触发。

    甚至,像SimpleTrigger一样,CronTrigger也有一个StartTime以指定日程从什么时候开始,也有一个(可选的)EndTime以指定何时日程不再继续。

     

    Cron Expressions  Cron 表达式

    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:

    • 1. Seconds
    • 2. Minutes
    • 3. Hours
    • 4. Day-of-Month
    • 5. Month
    • 6. Day-of-Week
    • 7. Year (optional field)

    Cron表达式被用来配置CronTrigger实例。Cron表达式是一个由7个子表达式组成的字符串。每个子表达式都描述了一个单独的日程细节。这些子表达式用空格分隔,分别表示:

      1. Seconds 秒

      2. Minutes 分钟

      3. Hours 小时

      4. Day-of-Month 月中的天

      5. Month 月

      6. Day-of-Week 周中的天

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

    一个cron表达式的例子字符串为"0 0 12 ? * WED",这表示“每周三的中午12:00”。

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

    单个子表达式可以包含范围或者列表。例如:前面例子中的周中的天这个域(这里是"WED")可以被替换为"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".

    通配符('*')可以被用来表示域中“每个”可能的值。因此在"Month"域中的*表示每个月,而在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.

    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.

    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.

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

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

    所有的域中的值都有特定的合法范围,这些值的合法范围相当明显,例如:秒和分域的合法值为0到59,小时的合法范围是0到23,Day-of-Month中值得合法凡范围是0到31,但是需要注意不同的月份中的天数不同。月份的合法值是0到11。或者用字符串JAN,FEB MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV 及DEC来表示。Days-of-Week可以用1到7来表示(1=星期日)或者用字符串SUN, MON, TUE, WED, THU, FRI 和SAT来表示.

    '/'字符用来表示值的增量,例如, 如果分钟域中放入'0/15',它表示“每隔15分钟,从0开始”,如果在份中域中使用'3/20',则表示“小时中每隔20分钟,从第3分钟开始”或者另外相同的形式就是'3,23,43'。

    '?'字符可以用在day-of-month及day-of-week域中,它用来表示“没有指定值”。这对于需要指定一个或者两个域的值而不需要对其他域进行设置来说相当有用。

    'L'字符可以在day-of-month及day-of-week中使用,这个字符是"last"的简写,但是在两个域中的意义不同。例如,在day-of-month域中的"L"表示这个月的最后一天,即,一月的31日,非闰年的二月的28日。如果它用在day-of-week中,则表示"7"或者"SAT"。但是如果在day-of-week域中,这个字符跟在别的值后面,则表示"当月的最后的周XXX"。例如:"6L" 或者 "FRIL"都表示本月的最后一个周五。当使用'L'选项时,最重要的是不要指定列表或者值范围,否则会导致混乱。

    'W' 字符用来指定距离给定日最接近的周几(在day-of-week域中指定)。例如:如果你为day-of-month域指定为"15W",则表示“距离月中15号最近的周几”。

    '#'表示表示月中的第几个周几。例如:day-of-week域中的"6#3" 或者 "FRI#3"表示“月中第三个周五”。

     

    Example Cron Expressions

    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.

    Building CronTriggers

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

    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:

    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:

    // 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:

    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:

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

    When building CronTriggers, you specify the misfire instruction as part of the cron schedule (via WithCronSchedule extension method):

    trigger = TriggerBuilder.Create()
        .WithIdentity("trigger3", "group1")
        .WithCronSchedule("0 0/2 8-17 * * ?", x => x
            .WithMisfireHandlingInstructionFireAndProceed())
        .ForJob("myJob", "group1")
        .Build();
  • 相关阅读:
    Navicat 总是断开连接
    MySQL 重连机制
    优化 一
    python之 paramiko模块 连接服务器
    变量值的修改
    Python使用APScheduler实现定时任务
    Linux命令 清空文件
    输入法 | 输入法如何打出直角引号
    Java | Java字节码
    英语 | 图片学习单词
  • 原文地址:https://www.cnblogs.com/hzz521/p/5159740.html
Copyright © 2020-2023  润新知