Spring 基于注释的调度程序,即“@Scheduled”不是根据 cron 表达式连续 运行

Spring Annotation based scheduler i.e '@Scheduled' is not running continuously as per cron expression

我使用了 Spring 基于注释的调度程序 @Scheduler 并将其设置为 运行 每 30 秒。我的示例代码是这样的。

这是使用调度程序注释的示例服务代码。

 @Service
    public class SchedulerJob
    {
        @Scheduled(cron="*/30 * * * * ?")
        public void demoServiceMethod()
        {
            System.out.println("Job running every 30 seconds");
        }
    }

上面的代码工作正常,每 30 秒 运行ning 一次,但有时不是 运行宁。 当我重新启动服务器时,它又恢复了。

任何人都可以用@Scheduler 注释告诉我这种行为的原因。

提前致谢。

设置 Spring 配置,注释 @EnableScheduling

@Configuration
@EnableScheduling
public class RootConfig { .. }

一个类似 cron 的表达式,扩展了通常的 UNIX 定义以包括在秒以及分钟、小时、月中的某天、月份和星期几上触发。例如“0 * * * * MON-FRI”表示工作日每分钟一次。

cron 有 60 秒的粒度,这不是最好的工具。如果您 运行 经常如此,cron 不会下降到亚分钟分辨率,您将需要找到另一种方法。

尝试使用表达式 initialDelayString 定义在第一次执行之前延迟的毫秒数,并且 fixedDelayString 在最后一次调用结束和开始之间以固定的毫秒数执行带注释的方法下一个。

@Scheduled(initialDelayString = "${initialDelayString}", fixedDelayString = "${fixedDelayString}")
public void demoServiceMethod()