spring boot 多个定时任务为什么不执行?

ScheduledAnnotationBeanPostProcessor类 setScheduler 方法


	/**
	 * Set the {@link org.springframework.scheduling.TaskScheduler} that will invoke
	 * the scheduled methods, or a {@link java.util.concurrent.ScheduledExecutorService}
	 * to be wrapped as a TaskScheduler.
	 * 

If not specified, default scheduler resolution will apply: searching for a * unique {@link TaskScheduler} bean in the context, or for a {@link TaskScheduler} * bean named "taskScheduler" otherwise; the same lookup will also be performed for * a {@link ScheduledExecutorService} bean. If neither of the two is resolvable, * a local single-threaded default scheduler will be created within the registrar. * @see #DEFAULT_TASK_SCHEDULER_BEAN_NAME */ public void setScheduler(Object scheduler) { this.scheduler = scheduler; }

设置将调用计划方法的TaskScheduler ,或将ScheduledExecutorService包装为 TaskScheduler。
如果未指定,将应用默认调度程序解析:在上下文中搜索唯一的TaskScheduler bean,否则搜索名为“taskScheduler”的TaskScheduler bean;还将对ScheduledExecutorService bean 执行相同的查找。如果两者都无法解析,则将在注册器中创建本地单线程默认调度程序。
也可以看看:
DEFAULT_TASK_SCHEDULER_BEAN_NAME

意思就是默认是单线程去执行定时任务如果是多个定时任务就没有线程去执行

方法1 使用注解指定定时任务线程池大小

#  task:
#    scheduling:
#      pool:
#        size: 5

方法2 覆盖bean 名“taskScheduler”

@Configuration
public class ScheduleConfig {

    @Bean
    public TaskScheduler taskScheduler() {

        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(5);
        return taskScheduler;
    }
}

你可能感兴趣的:(spring,boot,spring,boot,java,spring)