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就行了.