spring-boot-starter-parent version 2.7.3

@Slf4j
@Configuration
@EnableScheduling
@RequiredArgsConstructor
public class ImplSchedulingConfigurer implements SchedulingConfigurer {

    private final ScheduledTaskService scheduledTaskService;

    private final JobService<Object> jobService;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        // 查询可执行任务
        List<ScheduledTaskEntity> scheduledTaskList = scheduledTaskService.list();
        for (ScheduledTaskEntity taskDO : scheduledTaskList) {
            Long scheduledTaskId = taskDO.getId();
            taskRegistrar.addTriggerTask(
                    () -> {
                        ScheduledTaskEntity task = scheduledTaskService.getById(scheduledTaskId);
                        if (StatusEnum.DISABLE == task.getStatus()) {
                            return;
                        }
                        try {
                            log.debug(taskDO.getTaskName() + "开始执行...");
                            long start = System.currentTimeMillis();
                            Object result = jobService.executeTask(taskDO.getTaskName());
                            long end = System.currentTimeMillis();
                            log.debug(taskDO.getTaskName() + "执行结束,执行结果:{},执行时间:{}", result, end - start);
                        } catch (Exception e) {
                            e.printStackTrace();
                            log.error(taskDO.getTaskName() + " 执行异常 " + e.getMessage());
                        }
                    },
                    triggerContext -> {

                        // 数据库里查询
                        String cronExpression = scheduledTaskService.lambdaQuery()
                                .select(ScheduledTaskEntity::getCronExpression)
                                .eq(ScheduledTaskEntity::getTaskName, taskDO.getTaskName())
                                .one()
                                .getCronExpression();
                        return new CronTrigger(cronExpression).nextExecutionTime(triggerContext);                   }
            );
        }
    }

    @Bean
    public TaskScheduler scheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(4);
        scheduler.setWaitForTasksToCompleteOnShutdown(true);
        scheduler.setAwaitTerminationSeconds(60);
        scheduler.setThreadNamePrefix("scheduler-pool-%d");
        return scheduler;
    }
}

After running the code for a long time, the task will automatically stop running, and the final phenomenon is that one thread in the thread pool only runs one specific task.

jstack screenshot SpringBoot Timed task execution exception

Comment From: philwebb

This is unlikely to be a bug in Spring Boot itself since we do little more than auto-configure a few scheduler beans. It might be a Spring Framework bug or it might be user error, it's very hard to tell from the limited information you've provided. You should also be aware that Spring Boot 2.7 has reached end of OSS support.

If you'd like us to spend some time investigating, please take the time to provide a complete minimal sample (something that we can unzip or git clone, build, and deploy) that reproduces the problem with a supported version of Spring Boot.

Comment From: jdAction

timed_task_demo.zip

Comment From: jdAction

I am running in Docker and a task will stop around 26 days later

Comment From: wilkinsona

Thanks for the sample, but it's using Spring Boot 2.7.x which, as Phil explained above, is not longer supported. Please upgrade to Spring Boot 3.1.x or later. The sample is also using a MySQL database hosted at 10.0.42.57 which we do not have. Please minimize it so that it contains nothing more than what is required to reproduce the problem.

Comment From: jdAction

Using version 5.7 of MySQ, there is an SQL file imported into the resources folder and executed in MySQL. The Spring Boot version will not be upgraded for now. Let's take a look at the specific reason why it occurred. Later, I will check the version upgrade history to see if this issue has been fixed

Comment From: philwebb

The Spring Boot version will not be upgraded for now. Let's take a look at the specific reason why it occurred

I'm sorry, but we're not going to be able to spend the significant time it's likely to take to diagnose your issue. There are too many moving parts in your sample application and little to suggest that your problem lies in the Spring Boot codebase. If you're able to reproduce the problem with a minimal sample on a supported version of Spring Boot without MySQL, IBatis or AspectJ then we'd be happy to take another look.