CronTrigger是比SimpleTrigger更為有用的.如果你想啟動一個工作任務,像日歷進程一樣.
這個時候CronTrigger是比SimpleTrigger更為精確的.
比如你可以指定: "每個星期五的下午","每個周末的上午9:30","每周一,周三的上午9點到10點,每5分鐘一次"
這都需要Cron Excepressions 來描述.
Cron-Excepressions是用來配置CronTrigger實例的.它有7個精確的子表達式,其中年是可選的.順序如下:
- Seconds
- Minutes
- Hours
- Day-of-Month
- Month
- Day-of-Week
- Year (optional field)
讓我們來看看這個表達式的意思:
"0 0 12 ? * WED"
表示每個星期三的中午12點
個別的子表達式是允許包含集合的,比如"MON, WED, FRI", 或 "MON-WED,SAT"
中間用,號隔開.
其中 * 號是代碼 "每" 的意思,就是此屬性的每一個可能值.
在這里表示every Wednesday.
?號用在 day-of-month 和 day-of-week 屬性上.
L代表Last,W代表Weekend
#號指定每月的第幾個星期幾.如:FRI#3 意思是每月的第三個星期五.
這有一些例子大家參考一下:
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 ? * 6L" Fire at 10:15am on the last Friday 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
使用此類也是很簡單的,比如你可以用如下代碼來替換SimpleTrigger部分:
CronTrigger cronTrigger = new CronTrigger("myTrigger","myGroup","你的表達式");
對于此部分大家沒必要全部記住,需要的時候去查API就行了.