两者之间有区别吗?和*在cron表达式?奇怪的例子 [英] Is there a difference between ? and * in cron expressions? Strange example
问题描述
我的系统中有以下cron表达式:
0 0 0/1 1/1 *? *
你知道吗?我不知道这意味着什么。写这篇文章的人是他未来两周的假期,所以我必须自己找出答案。可以在此处找到该文档。 / p>
根据文档我们有:
* * * * * * *
| | | | | | |
| | | | | | + - 年(范围:1970-2099)
| | | | | + ----每周的日子(范围:1-7或SUN-SAT)
| | | | + ------年度月份(范围:0-11或JAN-DEC)
| | | + --------每月的日子(范围:1-31)
| | + ----------小时(范围:0-23)
| + ------------分钟(范围:0-59)
+ --------------秒(范围:0-59)
好的,让我告诉你我的想法:我相信这个表达意味着:
开始时间:
秒:0
分钟:0
小时:0
dayOfMonth 1
monthOfYear任何
dayOfWe任何
年任何
每运行一次:
1小时
1 dayOfWeek
时间:
dayOfWeek与第一次执行时相同
然而,可用的cron表达式监视器表示它仅表示每小时。
作为编写高级Java Dev的人,他必须知道编写此类表达式的任何理由而不是:
0 0 * * * * *
我们使用 org.springframework .scheduling.quartz.QuartzJobBean
。
简短摘要
好吧,我认为我的问题是: 0 0 0/1 1/1 *之间有什么区别? *
和 0 0 * * * * *
?
编辑:
文档可以在这里找到。
0/1
表示从小时开始 0
并重复每个 1
小时
1/1
从这个月的第一天开始并执行每个 1
天
所以这个模式执行一次cron每个小时,从第一天开始,每天重复一次。
需要使用
?
在dayOfWeek
或dayOfMonth
之一:
指定星期和月日值的支持尚未完成(您必须在其中一个字段中使用'?
'字符) 。 - xenteros 7分钟前
然后, 0 0 * * *? *
(而不是 0 0 * * * *
,?
必须如您所评论的那样)将是相同的表达式,忽略秒和分钟并获取其他元素的每个值,将每小时和每天执行。
根据您的信息:
0 0 0/1 1/1 *? *
| | | | | | |
| | | | | | + - 年(范围:1970-2099)
| | | | | + ----每周的日子(范围:1-7或SUN-SAT)
| | | | + ------年度月份(范围:0-11或JAN-DEC)
| | | + ---------每月的一天(范围:1-31)
| | + -------------小时(范围:0-23)
| + ----------------分钟(范围:0-59)
+ ------------------秒(范围:0-59)
*
(所有值)
用于选择字段中的所有值。例如,分钟字段中的表示*每分钟。
?
(没有特定价值)
当您需要在其中一个中指定某些内容时非常有用允许角色的两个字段,但不允许另一个字段。例如,如果我希望我的触发器在该月的某个特定日期(例如,第10天)触发,但不关心恰好在一周的哪一天,我会在当天放置10 -month字段,以及星期几字段中的?。
/
用于指定增量。例如,秒字段中的0/15表示秒0,15,30和45。秒字段中的5/15表示秒5,20,35和50。你也可以在''字符后指定'/' - 在这种情况下''相当于在'/'之前有'0'。日期字段中的1/3表示从该月的第一天开始每3天触发一次。
< hr>
*
和之间的差异?
要解释表达式中?
和 *
之间的区别,首先需要看看这个表:
字段名称允许的强制允许值特殊字符
秒是0-59, - * /
分钟是0-59, - * /
小时是0-23, - * /
日期是1-31, - *? / L W //允许'?'
月是1-12或JAN-DEC, - * /
星期几是1-7或SUN-SAT, - *? / L#//允许'?'
年无空,1970-2099, - * /
如您所见?
仅允许在日期
和日期周
在两个字段中都是强制性的,并告诉Quartz这个值尚未定义,因此,使用另一个字段(如果你输入?
进入日期
,使用的值将是星期几
)。
I have the following cron expression in my system:
0 0 0/1 1/1 * ? *
and you know what? I have no idea what it means. The guy who has written it is on his holiday for the next 2 weeks so I have to find out on myself. The documentation can be found here
According to the documentation we have:
* * * * * * *
| | | | | | |
| | | | | | +-- 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)
Ok, let me tell you what I think: I believe that the expression means:
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
However available cron expression monitors says that it simply means every hour. As the one who has written that is Senior Java Dev, he must have known any reason for writing such expression instead of:
0 0 * * * * *
We use org.springframework.scheduling.quartz.QuartzJobBean
.
Short summary
Well, I think that my question is: what is the difference between 0 0 0/1 1/1 * ? *
and 0 0 * * * * *
?
Edit:
The documentation can be found here.
0/1
means start at hour 0
and repeat each 1
hour
1/1
is start first day of the month and execute each 1
day
So this pattern executes the cron once each hour, starting day one of month and repeating itself every day.
there is a requirement to use
?
in one ofdayOfWeek
ordayOfMonth
:
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
Then, 0 0 * * * ? *
(and not 0 0 * * * *
, with ?
mandatory as you commented) will be same expression, ignoring seconds and minutes and taking each value of other elements, will execute each hour and everyday.
According your information:
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)
And this explanation of the special characters:
*
("all values")
used to select all values within a field. For example, "" in the minute field means *"every minute".
?
("no specific value")
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".
differences between *
and ?
To explain difference between ?
and *
in the expressions, first of all take a look at this 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 , - * /
As you can see ?
is only allowed in Day of month
and Day of week
is mandatory in one of both fields and will tell Quartz this value has not been defined, thus, use the other field (if you put ?
into Day of month
, the value used will be Day of week
).
这篇关于两者之间有区别吗?和*在cron表达式?奇怪的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!