spring quartz cronExpression配置实例及说明

转载地址:http://faceye.com/haipeng/article/40288083252175550125217f5dd90009.html

在quartz跟spring进行完美整合以后, 越来越多后台工作任务,交给了quartz

在quartz中,一项比较关键的配置是cronExpression的配置

它直接决定着任务的执行频率以及执行规则.

看看对cronExpression的配置说明:

 0-59 , - * /
 0-59 , - * /
小时 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年(可选) 留空, 1970-2099 , - * /

看看官方文档对此是怎么说的:

“*”字符被用来指定所有的值。如:”*“在分钟的字段域里表示“每分钟”。

“?”字符只在日期域和星期域中使用。它被用来指定“非明确的值”。当你需要通过在这两个域中的一个来指定一些东西的时候,它是有用的。看下面的例子你就会明白。 月份中的日期和星期中的日期这两个元素时互斥的一起应该通过设置一个问号(?)来表明不想设置那个字段

“-”字符被用来指定一个范围。如:“10-12”在小时域意味着“10点、11点、12点”。

“,”字符被用来指定另外的值。如:“MON,WED,FRI”在星期域里表示”星期一、星期三、星期五”.

The'/'characterisusedtospecifyincrements.Forexample"0/15"inthesecondsfieldmeans"theseconds0,15,30,and45".And"5/15"inthesecondsfieldmeans"theseconds5,20,35,and50".Specifying'*'beforethe'/'isequivalenttospecifying0isthevaluetostartwith.Essentially,foreachfieldintheexpression,thereisasetofnumbersthatcanbeturnedonoroff.Forsecondsandminutes,thenumbersrangefrom0to59.Forhours0to23,fordaysofthemonth0to31,andformonths1to12.The"/"charactersimplyhelpsyouturnonevery"nth"valueinthegivenset.Thus"7/6"inthemonthfieldonlyturnsonmonth"7",itdoesNOTmeanevery6thmonth,pleasenotethatsubtlety.

The'L'characterisallowedfortheday-of-monthandday-of-weekfields.Thischaracterisshort-handfor"last",butithasdifferentmeaningineachofthetwofields.Forexample,thevalue"L"intheday-of-monthfieldmeans"thelastdayofthemonth"-day31forJanuary,day28forFebruaryonnon-leapyears.Ifusedintheday-of-weekfieldbyitself,itsimplymeans"7"or"SAT".Butifusedintheday-of-weekfieldafteranothervalue,itmeans"thelastxxxdayofthemonth"-forexample"6L"means"thelastfridayofthemonth".Whenusingthe'L'option,itisimportantnottospecifylists,orrangesofvalues,asyou'llgetconfusingresults.

L是‘last’的省略写法可以表示day-of-month和day-of-week域,但在两个字段中的意思不同,例如day-of-month域中表示一个月的最后一天,

如果在day-of-week域表示‘7’或者‘SAT’,如果在day-of-week域中前面加上数字,它表示一个月的最后几天,例如‘6L’就表示一个月的最后一个

星期五,

The'W'characterisallowedfortheday-of-monthfield.Thischaracterisusedtospecifytheweekday(Monday-Friday)nearestthegivenday.Asanexample,ifyouweretospecify"15W"asthevaluefortheday-of-monthfield,themeaningis:"thenearestweekdaytothe15thofthemonth".Soifthe15thisaSaturday,thetriggerwillfireonFridaythe14th.Ifthe15thisaSunday,thetriggerwillfireonMondaythe16th.Ifthe15thisaTuesday,thenitwillfireonTuesdaythe15th.Howeverifyouspecify"1W"asthevalueforday-of-month,andthe1stisaSaturday,thetriggerwillfireonMondaythe3rd,asitwillnot'jump'overtheboundaryofamonth'sdays.The'W'charactercanonlybespecifiedwhentheday-of-monthisasingleday,notarangeorlistofdays

.

The'L'and'W'characterscanalsobecombinedfortheday-of-monthexpressiontoyield'LW',whichtranslatesto"lastweekdayofthemonth".

The'#'characterisallowedfortheday-of-weekfield.Thischaracterisusedtospecify"thenth"XXXdayofthemonth.Forexample,thevalueof"6#3"intheday-of-weekfieldmeansthethirdFridayofthemonth(day6=Fridayand"#3"=the3rdoneinthemonth).Otherexamples:"2#1"=thefirstMondayofthemonthand"4#5"=thefifthWednesdayofthemonth.Notethatifyouspecify"#5"andthereisnot5ofthegivenday-of-weekinthemonth,thennofiringwilloccurthatmonth.

The 'C' character is allowed for the day-of-month and day-of-week fields. This character is short-hand for "calendar". This means values are calculated against the associated calendar, if any. If no calendar is associated, then it is equivalent to having an all-inclusive calendar. A value of "5C" in the day-of-month field means "the first day included by the calendar on or after the 5th". A value of "1C" in the day-of-week field means "the first day included by the calendar on or after sunday".

表达式意义     

"0012**?"每天中午12点触发

"01510?**"每天上午10:15触发

"01510**?"每天上午10:15触发

"01510**?*"每天上午10:15触发

"01510**?2005"2005年的每天上午10:15触发

"0*14**?"在每天下午2点到下午2:59期间的每1分钟触发

"00/514**?"在每天下午2点到下午2:55期间的每5分钟触发

"00/514,18**?"在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发

"00-514**?"在每天下午2点到下午2:05期间的每1分钟触发

"010,4414?3WED"每年三月的星期三的下午2:10和2:44触发

"01510?*MON-FRI"周一至周五的上午10:15触发

"0151015*?"每月15日上午10:15触发

"01510L*?"每月最后一日的上午10:15触发

"01510?*6L"每月的最后一个星期五上午10:15触发

"01510?*6L2002-2005"2002年至2005年的每月的最后一个星期五上午10:15触发

"01510?*6#3"每月的第三个星期五上午10:15触发

每天早上6点

06***

每两个小时

0*/2***

晚上11点到早上8点之间每两个小时,早上八点

023-7/2,8***

每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点

0114*1-3

1月1日早上4点

0411*

相关推荐