`
duyangsss
  • 浏览: 127451 次
  • 性别: 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表达式是用于定义定时任务执行时间的一种格式,广泛应用于Linux系统中的计划任务。本文将深入解读cron表达式的各个组成部分及其使用方法。 #### cron表达式的基本结构 一个完整的cron...

    CRON表达式详解(英文版)

    CRON表达式是一种用于在Unix和Linux系统以及许多其他支持它的平台上设置定时任务的强大工具。它基于一组预定义的规则,用字符串的形式来表达时间的周期性安排。这个表达式通常由五个或六个字段组成,有时甚至包括一...

    可直接使用-cron表达式前端生成工具.zip

    ### cron表达式详解 cron表达式是由7个子字段组成的字符串,每个字段代表不同的时间单位,从左到右分别是: 1. **秒(Seconds)**:0-59,可以设置分号(;)进行多个值的分隔。 2. **分钟(Minutes)**:0-59。 3....

    Quartz_cron表达式

    ### Quartz Cron 表达式详解 #### 一、概述 Quartz是一款强大的开源作业调度框架,广泛应用于Java应用程序中,用于实现定时任务的功能。Quartz支持多种触发器类型,其中最为灵活且功能强大的是CronTrigger,它允许...

    Cron表达式选择器JS插件

    ### Cron表达式详解 Cron表达式由6或7个子表达式组成,每个子表达式代表不同的时间单位:秒、分钟、小时、日期、月份中的日期、月份和星期几(在某些实现中还包括年份)。这些子表达式通过空格分隔,并可以包含数字...

    Quartz的cron表达式

    Quartz 的 cron 表达式详解 Quartz 的 cron 表达式是一个强大的调度工具,用于设置定时任务的执行时间。它由七个字段组成,分别是秒、分钟、小时、天(月)、月、天(星期)和年份。每个字段可以设置特定的值、范围...

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

    本文将深入探讨如何在Spring项目中集成Quartz,并详细解析Cron表达式,以便更好地理解和运用定时任务。 首先,集成Quartz到Spring项目中通常涉及以下步骤: 1. **引入依赖**:在项目的pom.xml或build.gradle文件中...

    Cron表达式

    ### Cron表达式详解 #### 一、Cron表达式概述 Cron表达式是一种用于配置定时任务执行时间的标准格式,在很多系统中都有广泛的应用,比如Quartz调度器、Linux操作系统等。通过Cron表达式,我们可以灵活地设定任务...

    Quartz Cron表达式

    ### Quartz Cron表达式详解 #### 一、CronTrigger与SimpleTrigger对比 在Quartz调度器中,`CronTrigger`是一种非常强大的触发机制,它能够根据复杂的日历规则来调度任务,相比于`SimpleTrigger`而言更为灵活。如果...

    Cron表达式前台源码

    **Cron表达式详解** Cron表达式是一种广泛用于任务调度的语法,它允许我们设置时间规则来执行特定的任务。这种表达式源自Unix系统中的cron daemon,现在在各种编程语言和平台中都有应用,包括Java的Quartz框架、...

    cron表达式(quartz中时间表达式)

    ### cron表达式详解 cron表达式是由7个子表达式组成的字符串,每个子表达式之间用空格分隔,代表不同的时间单位: 1. **秒(Seconds)**:0-59的整数,可包含逗号分隔的多个值或范围。 2. **分钟(Minutes)**:0-...

    Quartz的Cron表达式

    ### Quartz的Cron表达式详解 #### 一、Cron表达式概述 Cron表达式是一种用于配置定时任务执行频率的格式化字符串。在Quartz调度器中,CronTrigger类型的触发器支持Cron表达式来定义触发规则。Cron表达式由六个或七...

    Cron表达式详解-附件资源

    Cron表达式详解-附件资源

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

    Cron表达式是一种广泛用于定时任务调度的工具,它基于字符串的形式定义了任务的执行时间。这个表达式由5到6个(有时是7个)空格分隔的字段组成,每个字段代表一个时间维度,从秒到年。具体格式为{秒}{分钟}{小时}{...

    crontab:解析Cron表达式,撰写Cron表达式字符串和计算执行日期

    Cron表达式详解** Cron表达式由5个或6个字段组成,每个字段代表一个时间维度,从左到右依次是秒、分钟、小时、日期、月份和星期。每个字段可以是一个具体的值,一个范围,一个列表,或者一个通配符(星号*),表示...

    quartz/Cron/Crontab表达式在线生成工具

    【Cron表达式详解】 Cron表达式是一种强大的时间调度机制,广泛应用于各种定时任务的配置,例如在Java中的Quartz框架。它是由七个子表达式组成的字符串,每个子表达式都代表一个时间维度(秒、分钟、小时、日期、...

    quartz的cron表达式

    ### quartz的cron表达式知识点详解 #### 一、cron表达式概述 cron表达式是一种用于配置定时任务执行规则的语言,广泛应用于Quartz等调度框架中。通过cron表达式,我们可以非常灵活地定义任务的执行时间,包括精确到...

    cron4s:Scala的跨平台CRON表达式解析

    CRON表达式详解 CRON表达式由六个或七个字段组成,每个字段代表不同的时间单位,从左到右依次为: - 秒(0-59) - 分钟(0-59) - 小时(0-23) - 日(1-31) - 月份中的月日(1-31) - 月份(1-12) - 星期(0-7,...

    cron测试工具

    **cron表达式详解:** cron表达式由一系列由空格分隔的字段组成,每个字段代表不同的时间单位。例如,`* * * * * *` 表示每秒执行一次。每个字段可以是单个数值、范围(如1-5)、列表(1,3,5)或者通配符(*,代表...

Global site tag (gtag.js) - Google Analytics