本文介绍了Spring Scheduling-每天午夜Cron表达都不起作用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试在Spring中安排一项任务,该任务每天午夜运行.我遵循了Spring的官方指南,并制作了如下的Scheduler类:

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

@Component
    public class OverduePaymentScheduler {

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

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

        }
    }

但是,如果时钟是凌晨12点,则该任务不会运行.我在链接.

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.

如果我将cron表达式更改为"*/10 * * * * *"(每十秒钟运行一次),则调度程序可以很好地执行.

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

那我在做什么错了?

推荐答案

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

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

这篇关于Spring Scheduling-每天午夜Cron表达都不起作用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 19:48