在cron更改后,Sprng Boot @Scheduled任务停止工作

时间:2018-04-06 08:34:11

标签: spring-boot cron

Spring Boot here。我有一个预定的后台任务,我每小时都会开始工作:

@Component
public class TokenReaper {
    @Scheduled(cron = "0 0 * * * *")
    public void fire() {
        // Doesn't matter what it does...
    }
}

我实际上需要它现在每天早上8:26运行 ,所以那时只有一天(奇怪,我知道!),所以我将cron表达式更改为:

@Component
public class TokenReaper {
    @Scheduled(cron = "0 26 8 * * *")
    public void fire() {
        // Doesn't matter what it does...
    }
}

进行此更改后,任务在上午8:26停止运行,并且由于日志,我无法确定它实际运行的时间或者它实际上是否正在运行!任何人都可以看到我的新cron表达式是否以某种方式格式错误或正确设置为每天早上8:26运行?!

2 个答案:

答案 0 :(得分:2)

您需要在?表达式中添加cron

@Scheduled(cron = "0 26 8 * * *")更改为:

@Scheduled(cron = "0 26 8 * * ?")

答案 1 :(得分:0)

尝试一下

@Scheduled(cron = "0 26 8 * * ?")

Cron表达式由六个字段表示:

second, minute, hour, day of month, month, day(s) of week

示例模式

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