简体   繁体   中英

Spring scheduled task stops working for a longer period of time

We have 10 scheduled tasks configured to run every 20 seconds with the following annotation (they are stopped at night, because depended systems reboot at 4 in the morning):

@Scheduled(cron = "*/20 * 0-3,5-23 * * *")
public void pollingMethod1() {
    ...
}
@Scheduled(cron = "*/20 * 0-3,5-23 * * *")
public void pollingMethod2() {
    ...
}
...

We have a thread pool taks scheduler configured with pool size 10:

@Configuration
public class SchedulerConfig implements SchedulingConfigurer {
    private final int POOL_SIZE = 10;

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();

        threadPoolTaskScheduler.setPoolSize(POOL_SIZE);
        threadPoolTaskScheduler.setThreadNamePrefix("polling-pool-");
        threadPoolTaskScheduler.initialize();
        threadPoolTaskScheduler.setAwaitTerminationSeconds(120);
        threadPoolTaskScheduler.setWaitForTasksToCompleteOnShutdown(true);

        scheduledTaskRegistrar.setTaskScheduler(threadPoolTaskScheduler);
    }
}

Sometimes during the day one of the tasks does not get executed for one or two hours and then continues to be executed regularly. Is it possible that one the tasks is blocking other tasks? And how can I find out if this is the case?

Suppost pollingMethod1 takes more than 20 seconds, then it might be executed by different threads at the same time, which causes other task be blocked. If this is allowed, you should increase POOL_SIZE .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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