Java 每天 1:01:am 的 Spring cron 表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26147044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring cron expression for every day 1:01:am
提问by d-man
I'm trying to have my code execute on a fixed schedule, based on a Spring cron expression. I would like the code to be executed every day at 1:01:am. I tried the following expression, but this didn't fire up for me. What's wrong with the syntax here?
我正在尝试根据 Spring cron 表达式按固定计划执行我的代码。我希望代码每天在 1:01:am 执行。我尝试了以下表达式,但这并没有让我兴奋。这里的语法有什么问题?
@Scheduled(cron = "0 1 1 ? * *")
public void resetCache() {
// ...
}
回答by gipinani
Try with:
尝试:
@Scheduled(cron = "0 1 1 * * ?")
Below you can find the example patterns from the spring forum:
您可以在下面找到来自 spring 论坛的示例模式:
* "0 0 * * * *" = the top of every hour of every day.
* "*/10 * * * * *" = every ten seconds.
* "0 0 8-10 * * *" = 8, 9 and 10 o'clock of every day.
* "0 0 8,10 * * *" = 8 and 10 o'clock of every day.
* "0 0/30 8-10 * * *" = 8:00, 8:30, 9:00, 9:30 and 10 o'clock every day.
* "0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays
* "0 0 0 25 12 ?" = every Christmas Day at midnight
Cron expression is represented by six fields:
Cron 表达式由六个字段表示:
second, minute, hour, day of month, month, day(s) of week
(*)
means match any
(*)
表示匹配任何
*/X
means "every X"
*/X
意思是“每个 X”
?
("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 I 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.
?
(“无特定值”) - 当您需要在允许字符的两个字段之一中指定某些内容时很有用,而另一个字段则不允许。例如,如果我希望我的触发器在一个月的某一天(比如 10 号)触发,但我不在乎那一天是一周中的哪一天,我会在当天输入“10”-月字段和“?” 在星期字段中。
PS: In order to make it work, remember to enable it in your application context: https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/scheduling.html#scheduling-annotation-support
PS:为了使其工作,请记住在您的应用程序上下文中启用它:https: //docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/scheduling.html#scheduling-注释支持
回答by user3298173
You can use annotate your method with @Scheduled(cron ="0 1 1 * * ?")
.
您可以使用@Scheduled(cron ="0 1 1 * * ?")
.
0 - is for seconds
0 - 是秒
1- 1 minute
1-1 分钟
1 - hour of the day.
1 - 一天中的一个小时。
回答by Maleen Abewardana
Something missing from gipinani's answer
gipinani的回答中缺少一些东西
@Scheduled(cron = "0 1 1,13 * * ?", zone = "CST")
This will execute at 1.01 and 13.01. It can be used when you need to run the job without a pattern multiple times a day.
这将在 1.01 和 13.01 执行。当您需要每天多次在没有模式的情况下运行作业时,可以使用它。
And the zone attribute is very useful, when you do deployments in remote servers. This was introduced with spring 4.
当您在远程服务器上进行部署时,区域属性非常有用。这是在 spring 4 中引入的。
回答by Bahadir Tasdemir
For my scheduler, I am using it to fire at 6 am every day and my cron notation is:
对于我的调度程序,我使用它在每天早上 6 点启动,我的 cron 符号是:
0 0 6 * * *
If you want 1:01:am then set it to
如果你想要 1:01:am 然后将它设置为
0 1 1 * * *
Complete code for the scheduler
调度程序的完整代码
@Scheduled(cron="0 1 1 * * *")
public void doScheduledWork() {
//complete scheduled work
}
** VERY IMPORTANT
**非常重要
To be sure about the firing time correctness of your scheduler, you have to set zone value like this (I am in Istanbul):
为了确保调度程序的触发时间正确性,您必须像这样设置区域值(我在伊斯坦布尔):
@Scheduled(cron="0 1 1 * * *", zone="Europe/Istanbul")
public void doScheduledWork() {
//complete scheduled work
}
You can find the complete time zone values from here.
您可以从这里找到完整的时区值。
Note: My Spring framework version is: 4.0.7.RELEASE
注意:我的 Spring 框架版本是:4.0.7.RELEASE
回答by Irlan Cidade
One thing i've noticed is: spring CronTrigger is not cron. You may end up with 7 parameters in a valid cron expression (wich you can validate on cronmaker.com) and then spring not accept it. Most of cases you just delete the last parameter and everything works fine.
我注意到的一件事是:spring CronTrigger 不是 cron。您可能会在有效的 cron 表达式中得到 7 个参数(您可以在 cronmaker.com 上进行验证),然后 spring 不接受它。大多数情况下,您只需删除最后一个参数,一切正常。
回答by MACCXpace
Spring cron expression for every day 1:01:am
每天 1:01:am 的 Spring cron 表达式
@Scheduled(cron = "0 1 1 ? * *")
@Scheduled(cron = "0 1 1 ? * *")
for more information check this information:
有关更多信息,请查看以下信息:
https://docs.oracle.com/cd/E12058_01/doc/doc.1014/e12030/cron_expressions.htm
https://docs.oracle.com/cd/E12058_01/doc/doc.1014/e12030/cron_expressions.htm