之间有区别吗?和 * 在 cron 表达式中?奇怪的例子

Is there a difference between ? and * in cron expressions? Strange example

我的系统中有以下 cron 表达式:

0 0 0/1 1/1 * ? *

你知道吗?我不知道这是什么意思。写这篇文章的人在接下来的 2 周内正在休假,所以我必须亲自去了解一下。可以找到文档 here

根据 documentation 我们有:

* * * * * * *
| | | | | | | 
| | | | | | +-- Year              (range: 1970-2099)
| | | | | +---- Day of the Week   (range: 1-7 or SUN-SAT)
| | | | +------ Month of the Year (range: 0-11 or JAN-DEC)
| | | +-------- Day of the Month  (range: 1-31)
| | +---------- Hour              (range: 0-23)
| +------------ Minute            (range: 0-59)
+-------------- Second            (range: 0-59)

好吧,让我告诉你我的想法:我相信这个表达的意思是:

start when:
    seconds:        0
    minutes:        0
    hours:          0
    dayOfMonth      1
    monthOfYear     any
    dayOfWeek       any
    year            any

run every:
    1               hour
    1               dayOfWeek
when:
    dayOfWeek same as on first execution

然而,可用的 cron 表达式监视器表示它仅表示每小时一次。 作为前辈写的Java Dev,他一定知道写这样的表达的任何原因,而不是:

0 0 * * * * *

我们使用org.springframework.scheduling.quartz.QuartzJobBean.

简短摘要

嗯,我想我的问题是:0 0 0/1 1/1 * ? *0 0 * * * * * 有什么区别?

编辑:

The documentation 可以在这里找到。

0/1 表示在 0 小时开始并每隔 1 小时重复
1/1 是每月的第一天开始并在每个 1

执行

因此此模式 每小时执行一次 cron,从每月的第一天开始并每天重复。

there is a requirement to use ? in one of dayOfWeek or dayOfMonth:
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). – xenteros 7 mins ago

然后,0 0 * * * ? *(而不是 0 0 * * * *? 是必需的,如您评论的那样)将是相同的表达式,忽略秒和分钟并取其他元素的每个值,将每小时和每天执行一次。


根据您的信息:

0 0 0/1 1/1 * ? *
| |  |   |  | | | 
| |  |   |  | | +-- Year              (range: 1970-2099)
| |  |   |  | +---- Day of the Week   (range: 1-7 or SUN-SAT)
| |  |   |  +------ Month of the Year (range: 0-11 or JAN-DEC)
| |  |   +--------- Day of the Month  (range: 1-31)
| |  +------------- Hour              (range: 0-23)
| +---------------- Minute            (range: 0-59)
+------------------ Second            (range: 0-59)

this explanation的特殊字符:

*(“所有值”)

used to select all values within a field. For example, “” in the minute field means *“every minute”.

?(“无具体数值”)

useful when you need to specify something in one of the two fields in which the character is allowed, but not the other. For example, if I want my trigger to fire on a particular day of the month (say, the 10th), but don’t care what day of the week that happens to be, I would put “10” in the day-of-month field, and “?” in the day-of-week field.

/

used to specify increments. For example, “0/15” in the seconds field means “the seconds 0, 15, 30, and 45”. And “5/15” in the seconds field means “the seconds 5, 20, 35, and 50”. You can also specify ‘/’ after the ‘’ character - in this case ‘’ is equivalent to having ‘0’ before the ‘/’. ‘1/3’ in the day-of-month field means “fire every 3 days starting on the first day of the month”.


*?

之间的区别

要解释?*在表达式上的区别,首先看一下这个table:

Field Name      Mandatory   Allowed Values      Allowed Special Characters
Seconds         YES         0-59                , - * /
Minutes         YES         0-59                , - * /
Hours           YES         0-23                , - * /
Day of month    YES         1-31                , - * ? / L W   //allowed '?'
Month           YES         1-12 or JAN-DEC     , - * /
Day of week     YES         1-7 or SUN-SAT      , - * ? / L #   //allowed '?'
Year            NO          empty, 1970-2099    , - * /

如你所见,? 仅在 Day of month 中允许,而 Day of week 在两个字段之一中是强制性的,并且会告诉 Quartz 该值尚未定义,因此,使用另一个字段(如果将 ? 放入 Day of month,则使用的值将是 Day of week)。

0 0 * * * ? *0 0 0/1 1/1 * ? *

之间没有实际区别

分析不同的标记:
0/1* 表示小时 - 第一个表示每天从 0 小时开始并每小时重复一次,第二个表示:每小时重复一次
1/1* 表示天 - 第一个表示从该月的第一天开始并每天重复,第二个表示每天。

有人使用复杂表达式的原因可能是通过测试,表达式被评估为这种形式,但没有人进行简化它的工作,或者可能以前的 cron 版本工作不同。

不是答案,只是对@joc 正确答案的更新。

至于现在,QuartzScheduler specifically 指出 ? 可以应用于以下两个位置之一:day_of_monthday_of_week

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).

除了上面的 link 之外,还有很多示例可以很好地指导您提出自己的想法。


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

您可以使用此网站作为快速生成和理解 cron 表达式语法的方法,而无需对 cron 格式语法了解太多:http://www.cronmaker.com

该网站使用 Quartz cron 格式。例如,您的表达式表示下一个预定日期是每小时: