CronTriggers Tutorial(同样适用于spring的quartz定时程序) 定时格式

本文介绍了CronTrigger类的使用方法,这是一种基于Cron表达式的定时任务调度工具。文章详细解释了Cron表达式的各个组成部分及其特殊字符的意义,通过实例演示了如何配置复杂的定时任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Introduction

corn 是一个unix工具,它拥有非常强大的定时执行能力。CronTrigger类就是基于cron的定时功能实现的。

CronTrigger 使用“cron 表达式”可以创建日程如“每周一到周五的早上8:00,做.....”或“每个月的最后一个周五的凌晨1:30,做.....”

Cron表达式是非常强大的,但是非常容易搞混。这篇文章的目的就是搞清楚如何创建如你所愿的cron表达式,让你可以不用去论坛苦苦寻找解决方法。

Format

一个cron表达式是一个由6到7个用空格分开的字段组成的字符串。这些字段如下:

字段名 是否必须? 允许的值允许的特殊字符 
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 , - * /

cron表达式可以简单的写成:* * * * ? *

或者更复杂一些,如:0 0/5 14,18,3-39,52 ? JAN,MAR,SEP MON-FRI 2002-2010 (注意:这是一条表达式)

Special characters

 

  •  * ("all values") - 用来选择所有的值,例如,minute field 分钟字段中的 "*" 意味着“每一分钟”

 

  • ? ("no specific value") -例,我希望我的触发器在10月的特殊一天中被触发而不去关心这一天是发生在星期几,设置Day of month字段为10,然后在day-of-week字段设置?。

 

  • - - “-”是用于指定范围的。例如,hour字段中设置“10-12”意味着“10,11,12点”

 

  • , - 用来指定额外的值。例如,在day-of-week字段设置“mon,wed,fri”意思是“星期一,星期三,星期五”

 

  • / - 用来指定增加量。例如,seconds字段中的“0/15”意味着“第0或15或30或45秒”,day-or-month字段中的“1/3”表示每个月的第一天开始触发,并且每三天触发一次。

 

  • L ("last") - 在两个字段中的意思是不一样的。例如,在day-of-month字段中意味着“the last day of the month”即“day 31 for January, day 28 for February on non-leap years”。如果用在day-of-week字段中,就是“7”或者“sat”的意思,但是如果跟到一个给定值之后,则是“x月的最后一个周几”的意思,比如,“6L”就是“某个月的最后一个周五”。When using the 'L' option, it is important not to specify lists, or ranges of values, as you'll get confusing results.
  • 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.

    The 'L' and 'W' characters can also be combined in the day-of-month field to yield 'LW', which translates to "last weekday of the month".

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

    The legal characters and the names of months and days of the week are not case sensitive. MON is the same as mon.

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 ? * 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
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

  • 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).
  • Be careful when setting fire times between mid-night and 1:00 AM - "daylight savings" can cause a skip or a repeat depending on whether the time moves back or jumps forward.
 原文地址: http://wiki.opensymphony.com/display/QRTZ1/CronTriggers+Tutorial
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值