`
duyangsss
  • 浏览: 124139 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Cron表达式详解

阅读更多

 

Cron 表达式格式 

Quartz的cron表达式与UNIX的cron表达式很相似,只有在个别地方有一些细微区别。

其中一个不同就是Quartz所提供的表达式精确到秒,而UNIX的cron表达式只提供分钟的精度设置。因为当我们使用Quartz执行计划任务经常会使用到秒级别(如:每45秒执行某一个任务一次),因此就是这些不同。

使用UNIX中,某一个工作将被一个所储存的cron表达式所触发执行,这个表达式需要设置六个位置。

With UNIX cron, the job (or command) that is to be executed is stored with the cron expression, in the sixth position. Quartz uses the cron expression to store the firing schedule. The CronTrigger, which references the cron expression, is associated with the job at schedule time.

Another difference between the UNIX cron expression format and Quartz is the number of supported fields in the expression. Where UNIX gives five (minute, hour, day, month, and dayofweek), Quartz provides seven. Table 5.1 lists the seven cron expression fields Quartz supports.

 

Table 5.1. Quartz Cron Expressions Support up to Seven Fields

Name

Required

Allowed Values

Special Characters

Seconds

Yes

059

, - * /

Minutes

Yes

059

, - * /

Hours

Yes

23

, - * /

Day of Month

Yes

131

, - * ? / L W C

Month

Yes

112 or JAN-DEC

, - * /

Day of Week

Yes

17 or SUN-SAT

, - * ? / L C #

Year

No

Blank or 19702099

, - * /

The names of months and days of the week are not case sensitive. FRI is the same as fri.

 

The fields are separated by a space, just as with UNIX cron. Arguably, the simplest expression we could write would look something like this:

* * * ? * *

 

This expression would fire the scheduled job every second, for every minute, for every hour of every day.

Understanding the Special Characters

As with UNIX cron, Quartz cron expressions support special characters that can be used to create more complicated execution schedules. However, Quartz supports many more than the standard UNIX cron expression.

The * Character

Using the asterisk (*) in a field indicates that you want to include all legal values for that field. For example, using this character in the month field means to fire the trigger for every month.

Example expression:

0 * 17 * * ?

 

Meaning: Fire the trigger every minute, every day starting at 5 PM until 5:59 PM. It stops at 5:59 PM because the value 17 is in the hour field, and at 6 PM, the hour becomes 18 and doesn't agree with this trigger until the next day at 5 PM.

Use the * character when you want the trigger to fire for every valid value of the field.

The ? Character

The question mark (?) character can be used only in the dayofmonth and dayofweek fields, but not at the same time. You can think of the ? character as "I don't care what value is in this field." This is different from the asterisk, which indicates every value for the field. The ? character says that no value was specified for this field.

The reasons a value can't be specified for both fields are tough to explain and even tougher to understand. Basically, if a value was specified for each, the meaning would become ambiguous: Consider if an expression had the value 11 in a field for the day of the month and a value of WED in the field for the day of the week. Should that trigger fire only on the 11th of the month if it falls on a Wednesday? Or should it fire on both the 11th and every Wednesday? The ambiguity is removed by not allowing a value in both fields at the same time.

Just remember that if you specify a value in one of the two fields, you must put a ? in the other.

Example expression:

0 10,44 14 ? 3 WED

 

Meaning: Fire at 2:10 PM and 2:44 PM every Wednesday in the month of March.

The , Character

The comma (,) character is used to specify a list of additional values within a given field. For example, using the value 0,15,30,45 in the second field means to fire the trigger every 15 seconds.

Example expression:

0 0,15,30,45 * * * ?

 

Meaning: Fire the trigger on every quarter-hour.

The / Character

The slash (/) character is used to schedule increments. We just used the comma to increment every 15 minutes, but we could have also written it like 0/15.

Example expression:

0/15 0/30 * * * ?

 

Meaning: Fire the trigger every 15 seconds on the hour and half-hour.

You can't increment beyond the fields range. For example, you can't specify 30/20 in the second field and expect the scheduler to fire correctly.

The Character

The hyphen (-) character is used to specify a range. For example, 3-8 in the hour field means "the hours 3, 4, 5, 6, 7, and 8." The fields will not wrap, so values such as 50-10 are not allowed.

Example expression:

0 45 3-8 ? * *

 

Meaning: Fire the trigger on 45 past the hours 3 AM through 8 AM.

The L Character

The L character represents the last allowed value for the field. It is supported by the dayofmonth and dayofweek fields only. When used in the dayofmonth field, it represents the last day of the month for the value specified in the month field. For example, when the month field has JAN specified, using L in the dayofmonth field would cause the trigger to fire on January 31. If SEP was specified as the month, then L would mean to fire on September 30. In order words, it means to fire the trigger on the last day of whatever month is specified.

The expression 0 0 8 L * ? means to fire the trigger at 8:00 AM the last day of every month. The * character in the month field gives us the "every month" part.

When the L character is used in the dayofweek field, it indicates the last day of the week, which is Saturday (or, numerically, 7). So if you needed to fire the trigger on the last Saturday of every month at 11:59 PM, you could use the expression 0 59 23 ? * L.

When used in the dayofweek field, you can use a numerical value in conjunction with the L character to represent the last X day of the month. For example, the expression 0 0 12 ? * 2L says to fire the trigger on the last Monday of every month.

 

 

Don't Use Range or List Options with the L Character

Although you can use a day of the week (17) value in conjunction with the L character, you're not allowed you to use a range of values or a list with it. This will produce unpredicted results.


The W Character

The W character stands for weekday (MonFri) and can be used only in the dayofmonth field. It is used to specify the weekday that is nearest to the given day. Most business processes are based on the work week, so the W character can be very important. For example, a value of 15W in the dayofmonth field means "the nearest weekday to the 15th of the month." If the 15th was on a Saturday, the trigger would fire on Friday the 14th because it's closer to the 15th than Monday, which would be the 17th in this example. The W character can be specified only when the day of the month is a single day, not a range or list of days.

The # Character

The # character can be used only in the dayofweek field. It's used to specify the nth XXX day of the month. For example, if you specified the value 6#3 in the dayofweek field, it would mean the third Friday of the month (6 = Friday and #3 means the third one in the month). Another example of 2#1 means the first Monday of the month (2 = Monday and #1 means the first one of the month). Note that if you specify #5 and there is no 5 of the given day of the week in the month, no firing will occur that month.

分享到:
评论

相关推荐

    cron表达式详解

    cron,cron表达式,cron表达式详解

    CRON表达式详解(英文版)

    cron表达式是一组根据特定规则由数字、空格和符号组成的字符串,从而表示时间信息。 与正则表达式类似,它是表示一些信息的字符串。

    Quartz的cron表达式

    Quartz的cron表达式。 Spring 定时任务@Scheduled cron表达式详解

    Spring集成Quartz定时任务框架介绍和Cron表达式详解

    NULL 博文链接:https://269937035.iteye.com/blog/2182979

    Cron表达式详解-附件资源

    Cron表达式详解-附件资源

    详解Cron表达式-原来是那么简单

    Cron表达式是一个具有时间含义的字符串,字符串以5~6个空格隔开,分为6~7个域,格式为X X X X X X X。其中X是一个域的占位符。最后一个代表年份的域非必须,可省略。单个域有多个取值时,使用半角逗号,隔开取值。

    Spring @Scheduler使用cron表达式时的执行问题详解

    Spring给程序猿们带来了许多便利。下面这篇文章主要给大家介绍了关于Spring @Scheduler使用cron表达式时的执行问题的相关资料,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧

    elastic-job相关资料下载

    elastic-job-lite-console-3.0.0.M1-SNAPSHOT.tar.gz netcat-win32-1.12.zip ZooInspector.zip zookeeper-3.4.9.tar.gz cron表达式详解.doc

    详解cron表达式

    Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义。接下来通过本文给大家详细介绍cron表达式内容,感兴趣的朋友一起看看吧

    Go定时器cron的使用详解

    cron表达式是一个好东西,这个东西不仅Java的quartZ能用到,Go语言中也可以用到。我没有用过Linux的cron,但网上说Linux也是可以用crontab -e 命令来配置定时任务。Go语言和Java中都是可以精确到秒的,但是Linux中...

    详解Javascript判断Crontab表达式是否合法

    主要介绍了详解Javascript判断Crontab表达式是否合法的相关资料,需要的朋友可以参考下

    Spring Job 配置详解

    Spring Job 配置详解,Spring中Quartz的Cron配置说明,一个Cron-表达式是一个由六至七个字段组成由空格分隔的字符串,其中6个字段是必须的而一个是可选的

    Quartz之CronExpression详解

    详细介绍CronExpression表达式设定定时任务的规则

    基于 ADF 框架开发的 Quartz 作业调度程序。

    1. 输入:支持 cron表达式; 2. 支持定时调用服务; 3. 支持定时调用数据库脚本(存储过程等); 4. 界面可以查看当前运行的后台作业; 5. 支持持久化存储; 6. 支持分布式配置; 7. 界面支持暂停,恢复,等一系列...

    Spring Quartz2 动态任务的实例详解

    此处使用的是Quartz中SimpleScheduleBuilder类,非CronScheduleBuilder,CronScheduleBuilder是Cron表达式的。具体请自行百度。 实现代码:  /** * 新增任务 * @param scheduleJob * @throws Exception */ @...

    Python定时任务APScheduler的实例实例详解

    描述一个任务何时被触发,按日期或按时间间隔或按 cronjob 表达式三种方式触发 任务存储器 job stores:存放任务,可以放内存(默认)或数据库 注:调度器之间不能共享任务存储器 执行器 executors:用于执行任务...

    入门学习Linux常用必会60个命令实例详解doc/txt

    入门学习Linux常用必会60个命令实例详解 Linux必学的60个命令 Linux提供了大量的命令,利用它可以有效地完成大量的工作,如磁盘操作、文件存取、目录操作、进程管理、文件权限设定等。所以,在Linux系统上工作离不...

Global site tag (gtag.js) - Google Analytics