springboot 调度任务源码分析

1. 启动程序,首先根据@EnableScheduling注解,找到对应的EnableScheduling类

springboot 调度任务源码分析
2. 再根据@Import的value值注解找到SchedulingConfiguration配置类,准备注册ScheduledAnnotationBeanPostProcessor

的bean到spring容器

springboot 调度任务源码分析

3. 初始化ScheduledAnnotationBeanPostProcessor处理器,完成ScheduledAnnotationBeanPostProcessor注册

springboot 调度任务源码分析

4. 以上完成后,执行ScheduledAnnotationBeanPostProcessor类下的postProcessAfterInitialization(),对调度任务类下的带@Scheduled注解的方法进行解析

springboot 调度任务源码分析

5.  再调用方法processScheduled()进行处理

springboot 调度任务源码分析

6. 注册调度任务

springboot 调度任务源码分析

7. 再依次注册TaskSchedulerBuilder、ThreadPoolTaskScheduler

springboot 调度任务源码分析

8. 注册ThreadPoolTaskScheduler的过程中会依次调用到TaskSchedulerBuilder的build()、configure()方法,并返回一个线程池大小为1,线程名前缀为scheduling-的ThreadPoolTaskScheduler对象

springboot 调度任务源码分析

9. 初始化执行器

springboot 调度任务源码分析

10. 在ScheduledAnnotationBeanPostProcessor类中执行ApplicationListener监听回调方法onApplicationEvent()

springboot 调度任务源码分析

11. 跳转到ScheduledAnnotationBeanPostProcessor类下的finishRegistration()方法,准备进行任务调度设置

springboot 调度任务源码分析

12. 设置前先调用resolveSchedulerBean()方法进行解析

springboot 调度任务源码分析

13. 解析完成后,再完成任务调度设置,跟踪到此完成