Cron is a UNIX tool that has been around for a long time, so its scheduling capabilities are powerful and proven. The CronTrigger class is based on the scheduling capabilities of cron.

CronTrigger uses “cron expressions”, which are able to create firing schedules such as: “At 8:00am every Monday through Friday” or “At 1:30am every last Friday of the month.”

CronTrigger is 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 during January”.

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:

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:00 pm”.

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 replaced with “MON-FRI”, “MON,WED,FRI”, or even “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 therefore obviously mean “every day of the 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 1-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 values 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 15th minute of the hour, starting at minute zero'. If you used '3/20' in the Minutes field, it would mean 'every 20th minute of the hour, starting at minute three' - or in other words it is the same as specifying '3,23,43' in the Minutes field. Note the subtlety that “/35” does not* mean “every 35 minutes” - it means “every 35th minute of the hour, starting at minute zero” - or in other words the same as specifying '0,35'.

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 Javadoc) 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”. You can also specify an offset from the last day of the month, such as “L-3” which would mean the third-to-last day of the calendar month. When using the 'L' option, it is important not to specify lists, or ranges of values, as you'll get confusing/unexpected 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”.

Format

The fields are as follows:

 Field Name   Mandatory   Allowed Values   Allowed Special Characters 
 Seconds YES  0-59  , - *
 Minutes YES  0-59  , - *
 Hours YES  0-23  , - *
 Day of month YES  1-31  , - * ? / L W
 Month YES  1-12 or JAN-DEC   , - *
 Day of week YES  1-7 or SUN-SAT  , - * ? / L #
 Year NO  empty, 1970-2099   , - *

So cron expressions can be as simple as this: * * * * ? *, or more complex, like this: 0/5 14,18,3-39,52 * ? JAN,MAR,SEP MON-FRI 2002-2010

Special characters

1. * (all values) - used to select all values within a field. For example, “*” in the minute field means “ every minute”.

2. ? (“no specific value”) - useful when you need to specify something in one of the two fields in which the character is allowed, but not the other. For example, if you want your trigger to fire on a particular day of the month (say, the 10th), but don't care what day of the week that happens to be, you would put “10” in the day-of-month field, and “?” in the day-of-week field. See the examples below for clarification.

3. - - used to specify ranges. For example, “10-12” in the hour field means “the hours 10, 11 and 12.”

4. , - used to specify additional values. For example, “MON,WED,FRI” in the day-of-week field means “the days Monday, Wednesday, and Friday”.

5. / - used to specify increments. For example, “0/15” in the seconds field means “the seconds 0, 15, 30, and 45". And “5/15” in the seconds field means “the seconds 5, 20, 35, and 50,” You can also specify '/' after the '' character - in this case '' is equivalent to having '0' before the '/'. '1/3' in the day-of-month field means “fire every 3 days starting on the first day of the month”.

6. L (“last”) - has different meaning in each of the two fields in which it is allowed. 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” means “the last Friday of the month”. You can also specify an offset from the last day of the month, such as “L-3” which would mean the third-to-last day of the calendar month. When using the 'L' option, it is important not to specify lists, or ranges of values, as you'll get confusing/unexpected results.

7. >W (“weekday”) - 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”. So if the 15th is a Saturday, the trigger will fire on Friday the 14th. If the 15th is a Sunday, the trigger will fire on Monday the 16th. If the 15th is a Tuesday, then it will fire on Tuesday the 15th. However if you specify “1W” as the value for day-of-month, and the 1st is a Saturday, the trigger will fire on Monday the 3rd, as it will not 'jump' over the boundary of a month's days. The 'W' character can only be specified when the day-of-month is a single day, not a range or list of days.

8. # - used to specify “the nth” XXX day of the month. For example, the value of “6#3” in the day-of-week field means “the third Friday of the month” (day 6 = Friday and “#3” = the 3rd one in the month). Other examples: “2#1” = the first Monday of the month and “4#5” = the fifth Wednesday of the month. Note that if you specify “#5” and there is not 5 of the given day-of-week in the month, then no firing will occur that month.

Examples

Here are some full examples:

Expression
Meaning
 0 0 12 * * ?
 Fire at 12pm (noon) every day
 0 15 10 ? * *
 Fire at 10:15am every day
 0 15 10 * * ?
 Fire at 10:15am every day
 0 15 10 * * ? *
 Fire at 10:15am every day
 0 15 10 * * ? 2005
 Fire at 10:15am every day during the year 2005
 0 * 14 * * ?
 Fire every minute starting at 2pm and ending at 2:59pm, every day
 0 0/5 14 * * ?
 Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day
 0 0/5 14,18 * * ?
 Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day
 0 0-5 14 * * ?
 Fire every minute starting at 2pm and ending at 2:05pm, every day
 0 10,44 14 ? 3 WED
 Fire at 2:10pm and at 2:44pm every Wednesday in the month of March.
 0 15 10 ? * MON-FRI
 >Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday
 0 15 10 15 * ?
 >Fire at 10:15am on the 15th day of every month
 0 15 10 L * ?
 >Fire at 10:15am on the last day of every month
 0 15 10 L-2 * ?
 >Fire at 10:15am on the 2nd-to-last day of every month
 0 15 10 ? * 6L
 >Fire at 10:15am on the last Friday of every month
 0 15 10 ? * 6L 2002-2005 
 >Fire at 10:15am on every last Friday of every month during the years 2002, 2003, 2004 and 2005
 0 15 10 ? * 6#3
 >Fire at 10:15am on the third Friday of every month
 0 0 12 1/5 * ?
 >Fire at 12pm (noon) every 5 days every month, starting on the first day of the month.
 0 11 11 11 11 ?
 Fire every November 11th at 11:11am.

Pay attention to the effects of '?' and '*' in the day-of-week and day-of-month fields.

Notes

1. Support for specifying both a day-of-week and a day-of-month value is not complete (you must currently use the '?' character in one of these fields).

2. Be careful when setting fire times between the hours of the morning when “daylight savings” changes occur in your locale (for US locales, this would typically be the hour before and after 2:00 AM - because the time shift can cause a skip or a repeat depending on whether the time moves back or jumps forward. You may find this Wikipedia entry helpful in determining the specifics to your locale: https://secure.wikimedia.org/wikipedia/en/wiki/Daylight_saving_time_around_the_world

Building CronTriggers

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

Build a trigger that will fire every other minute, between 8am and 5pm, every day

trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger3", "group1")
.withSchedule(CronScheduleBuilder.cronSchedule("0 0/2 8-17 * * ?"))
.forJob("myJob", "group1")
.build();

Build a trigger that will fire daily at 10:42 am

trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger3", "group1")
.withSchedule(DateBuilder.dailyAtHourAndMinute(10, 42))
.forJob(myJobKey)
.build();

or -

trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger3", "group1")
.withSchedule(CronScheduleBuilder.cronSchedule("0 42 10 * * ?"))
.forJob(myJobKey)
.build();

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

trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger3", "group1")
.withSchedule(DateBuilder.weeklyOnDayAndHourAndMinute(DateBuilder.WEDNESDAY, 10, 42))
.forJob(myJobKey)
.inTimeZone(TimeZone.getTimeZone("America/Los_Angeles"))
.build();

or -

trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger3", "group1")
.withSchedule(CronScheduleBuilder.cronSchedule("0 42 10 ? * WED"))
.inTimeZone(TimeZone.getTimeZone("America/Los_Angeles"))
.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 as constants on CronTrigger itself (including Javadoc describing their behavior). The instructions include:

MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY
MISFIRE_INSTRUCTION_DO_NOTHING
MISFIRE_INSTRUCTION_FIRE_NOW

All triggers also have the MISFIRE_INSTRUCTION_SMART_POLICY instruction available for use, and this instruction is also the default for all trigger types. The 'smart policy' instruction is interpreted by CronTrigger as MISFIRE_INSTRUCTION_FIRE_NOW. The Javadoc for the CronTrigger.updateAfterMisfire() method explains the exact details of this behavior.

When building CronTriggers, you specify the misfire instruction as part of the simple schedule (via CronSchedulerBuilder):

trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger3", "group1")
.withSchedule(CronScheduleBuilder.cronSchedule("0 0/2 8-17 * * ?")
.withMisfireHandlingInstructionFireAndProceed())
.forJob("myJob", "group1")
.build();

Quartz Scheduler(2.2.1) - Usage of CronTriggers的更多相关文章

  1. Quartz Scheduler(2.2.1) - Usage of JobDataMap

    The JobDataMap can be used to hold any amount of (serializable) data objects which you wish to have ...

  2. Quartz Scheduler(2.2.1) - Usage of Calendars

    Quartz Calendar objects (not java.util.Calendar objects) can be associated with triggers at the time ...

  3. Quartz Scheduler(2.2.1) - Usage of SimpleTrigger

    SimpleTrigger should meet your scheduling needs if you need to have a job execute exactly once at a ...

  4. Table of Contents - Quartz Scheduler

    Getting Started Hello World Integration with Spring Quartz Scheduler Developer Guide Usage of JobDat ...

  5. spring集成quartz scheduler

    创建项目 有两种创建quart配置作业 1.使用MethodInvokingJobDetailFactoryBean  Quartz Scheduler 配置作业(MethodInvokingJobD ...

  6. Quartz Scheduler(2.2.1) - hello world

    简单示例 1. maven 依赖 <dependencies> <dependency> <groupId>org.quartz-scheduler</gro ...

  7. Quartz Scheduler(2.2.1) - Working with JobStores

    About Job Stores JobStores are responsible for keeping track of all the work data you give to the sc ...

  8. Quartz Scheduler 开发指南(1)

    Quartz Scheduler 开发指南(1) 原文地址:http://www.quartz-scheduler.org/generated/2.2.2/html/qtz-all/ 实例化调度程序( ...

  9. 最新 Spring 4.2.2 集成 Quartz Scheduler 2.2.2 任务调度示例

    参考http://blog.csdn.net/defonds/article/details/49496895 本文将演示如何通过 Spring 使用 Quartz Scheduler 进行任务调度. ...

随机推荐

  1. oracle 的rowid和rownum

    rowid就是唯一标志记录物理位置的一个id,对于rownum来说它是oracle系统顺序分配为从查询返回的行的编号,返回的第一行分配的是1,第二行是2,依此类推,这个伪字段可以用于限制查询返回的总行 ...

  2. UVA 11354 Bond(MST + LCA)

    n<=50000, m<=100000的无向图,对于Q<=50000个询问,每次求q->p的瓶颈路. 其实求瓶颈路数组maxcost[u][v]有用邻接矩阵prim的方法.但是 ...

  3. URAL 2065 Different Sums (找规律)

    题意:构造一个数列,使得它们的区间和的种类最少,其中数列中不同的数的数目不少于k. 析:我们考虑0这个特殊的数字,然后0越多,那么总和种类最少,再就是正负交替,那么增加0的数量. 代码如下: #pra ...

  4. HDU 3666 THE MATRIX PROBLEM (差分约束)

    题意:给定一个最大400*400的矩阵,每次操作可以将某一行或某一列乘上一个数,问能否通过这样的操作使得矩阵内的每个数都在[L,R]的区间内. 析:再把题意说明白一点就是是否存在ai,bj,使得l&l ...

  5. Objc基础学习记录1

    1.'-'系在实例方法前头 2.'+'类方法class method 相反; 3.void表示没有返回值; 4.&x 和c语言一样,代表的是x的在内存上的地址; 5.*y指向内存存储空间内的数 ...

  6. 使用apt-get autoremove造成的系统无法开机

    由于误操作(apt-get autoremove xxx)删除了一些lib文件貌似,之后,系统直接重启,然后就无法进入系统,后使用引导盘对系统进行修复,思路如下: 1.挂载已经有的分区,挂载为可读可写 ...

  7. rqnoj-396-SY学语文-dp

    纯动态规划. 注意初始化为-INF #include<stdio.h> #include<algorithm> #include<iostream> #includ ...

  8. MongoDB学习笔记(一) MongoDB介绍及安装

    转自:http://database.51cto.com/art/201103/247882.htm http://baike.baidu.com/link?url=b6B3dVSCnQauCX-Ep ...

  9. centos6.5下Python IDE开发环境搭建

    自由不是想做什么就做什么,而是想不做什么就不做什么.        ---摘抄于2016/11/30晚 之前学习了一段时间的Python,但所有部署都在windows上.正赶上最近在学习liux,以后 ...

  10. C# 利用范型与扩展方法重构代码

    在一些C#代码中常常可以看到 //An Simple Example By Ray Linn class CarCollection :ICollection { IList list; public ...