简体   繁体   English

Spring Boot @Schedular 在首次执行后未运行

[英]Spring Boot @Schedular not running after first execution

I have written this scheduled task in my SpringBoot app:我在我的 SpringBoot 应用程序中编写了这个计划任务:

@Component
public class TestTaskScheduler {

    @Scheduled(fixedRate = 1000)
    public void test() {
        System.out.println("Run again and again and again");
    }
}

This runs first time, but not subsequently.这是第一次运行,但不是随后运行。

To be more specific, if you don't provide thread pool size configuration for scheduler, scheduled tasks run on the same thread.更具体地说,如果您不为调度程序提供线程池大小配置,则计划任务在同一线程上运行。 So if there is one scheduled task that say, is resource/time consuming, other scheduled tasks wont run.因此,如果有一个计划任务是资源/时间消耗,其他计划任务将不会运行。

It turned out that I was running another "heavy" scheduled task in the same application (it was initially created for testing my business logic and then I forgot to remove that).结果我在同一个应用程序中运行了另一个“繁重”的计划任务(它最初是为了测试我的业务逻辑而创建的,然后我忘了删除它)。 When I removed the other task, the issue got resolved.当我删除其他任务时,问题得到了解决。 (It seems that another task was using most of the resources so the current task was waiting for long time to be scheduled again.) (似乎另一个任务正在使用大部分资源,因此当前任务正在等待很长时间才能再次安排。)

Try add @Scheduled(fixedDelay = 1000L * 60 * 60).尝试添加@Scheduled(fixedDelay = 1000L * 60 * 60)。

This means that it will run every 60 minutes.这意味着它将每 60 分钟运行一次。 You can determine the working time by changing the last digit.您可以通过更改最后一位来确定工作时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM