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() {
// ...
}
6 个解决方案
#1
337
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-注释支持
#2
46
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
#3
20
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小时。
#4
6
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.
当您在远程服务器中进行部署时,zone属性非常有用。这是在春季4推出的。
#5
6
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上验证),然后不接受它。大多数情况下,您只需删除最后一个参数,一切正常。
#6
1
Spring cron expression for every day 1:01:am
每天1:01:am的春天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
#1
337
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-注释支持
#2
46
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
#3
20
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小时。
#4
6
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.
当您在远程服务器中进行部署时,zone属性非常有用。这是在春季4推出的。
#5
6
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上验证),然后不接受它。大多数情况下,您只需删除最后一个参数,一切正常。
#6
1
Spring cron expression for every day 1:01:am
每天1:01:am的春天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