Spring 调度 - 每天午夜的 Cron 表达式不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45124756/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:27:28  来源:igfitidea点击:

Spring Scheduling - Cron expression for everyday at midnight not working?

springspring-bootcronspring-data-rest

提问by Charlie

I am trying to schedule a task in Spring which is to be run everyday at midnight. I followed the official guidefrom Spring and made the scheduler class as below:

我正在尝试在 Spring 中安排一项任务,该任务将在每天午夜运行。我按照Spring的官方指南制作了调度程序类,如下所示:

@Component
    public class OverduePaymentScheduler {    

        @Scheduled(cron = "0 0 0 * * *")
        public void trackOverduePayments() {

              System.out.println("Scheduled task running");

        }
    }

However the task does not run when the clock hits 12am. I got the cron expression from the documentation for quartz scheduler at this link.

但是,当时钟到达凌晨 12 点时,任务不会运行。我从这个链接的石英调度程序文档中获得了 cron 表达式。

The scheduler is executed fine if I change the cron expression to "*/10 * * * * *" which runs every ten seconds.

如果我将 cron 表达式更改为每十秒运行一次的“*/10 * * * * *”,则调度程序执行得很好。

So what am I doing wrong?

那么我做错了什么?

回答by Rzv Razvan

These are valid formats for cron expressions:

这些是 cron 表达式的有效格式:

  • 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 6,19 * * *= 6:00 AM and 7:00 PM every day.
  • 0 0/30 8-10 * * *= 8:00, 8:30, 9:00, 9:30, 10:00 and 10:30 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
  • 0 0 * * * *= 每天的每个小时的顶部。
  • */10 * * * * *= 每十秒。
  • 0 0 8-10 * * *= 每天的 8、9 和 10 点。
  • 0 0 6,19 * * *= 每天早上 6:00 和晚上 7:00。
  • 0 0/30 8-10 * * *= 每天 8:00、8:30、9:00、9:30、10:00 和 10:30。
  • 0 0 9-17 * * MON-FRI= 工作日的朝九晚五
  • 0 0 0 25 12 ?= 每个圣诞节的午夜

The pattern is:

图案是:

second, minute, hour, day, month, weekday

So your answer is:

所以你的答案是:

0 0 0 * * *

回答by Charlie

I finally got it to work with this cron expression 0 0 0 * * *but I had to set the time zone in the scheduler class like this. @Scheduled(cron = "0 0 0 * * *",zone = "Indian/Maldives")

我终于让它与这个 cron 表达式一起工作,0 0 0 * * *但我必须像这样在调度程序类中设置时区。 @Scheduled(cron = "0 0 0 * * *",zone = "Indian/Maldives")

回答by Ajit Soman

Please use below cron pattern for 12:00 AM every day:

请在每天上午 12:00 使用以下 cron 模式:

    // at 12:00 AM every day
    @Scheduled(cron="0 0 0 * * ?")

I have checked your cron pattern at this website:http://www.cronmaker.com/.

我已经在这个网站上检查了你的 cron 模式:http: //www.cronmaker.com/

It says pattern 0 0 0 * * *as invalid.

它说模式0 0 0 * * *无效。

回答by RahuL Sharma

You can use below format to satisfy your requirement:

您可以使用以下格式来满足您的要求:

0 0 23 * * *

0 0 23 * * *

Since the hours starts from 0 to 23 for Quartz configuration. You can refer this linkfor more information.

由于石英配置的小时从 0 到 23 开始。您可以参考此链接以获取更多信息。