Insight spring @Scheduled 解析过程
2016-07-31 15:20
363 查看
接着上一篇<task:annotation-driven>解析,Insight @Scheduled 解析。
1、@Scheduled注解方法的加载
2、@Scheduled注解方法的处理
根据Scheduled属性构造不同类型的Task(triggerTasks、cronTasks、fixedRateTasks、fixedDelayTasks),然后注册到ScheduledTaskRegistrar,最后由其激活Task。
3、激活Task
registrar.afterPropertiesSet();
4、finally,调度任务就乖乖的定时execute啦。
附ScheduledExecutorService自带Example:
1、@Scheduled注解方法的加载
// bean 初始化后触发callback,ScheduledAnnotationBeanPostProcessor完成@Scheduled 的处理 // 参考InitializingBean's afterPropertiesSet public Object postProcessAfterInitialization(final Object bean, String beanName) { // ... ReflectionUtils.doWithMethods(targetClass, new MethodCallback() { public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { Scheduled scheduled = AnnotationUtils.getAnnotation(method, Scheduled.class); if (scheduled != null) { processScheduled(scheduled, method, bean); annotatedMethods.add(method); } } }); // ... return bean; }
2、@Scheduled注解方法的处理
根据Scheduled属性构造不同类型的Task(triggerTasks、cronTasks、fixedRateTasks、fixedDelayTasks),然后注册到ScheduledTaskRegistrar,最后由其激活Task。
3、激活Task
registrar.afterPropertiesSet();
public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean { public void afterPropertiesSet() { scheduleTasks(); } /** * Schedule all registered tasks against the underlying task scheduler. */ protected void scheduleTasks() { long now = System.currentTimeMillis(); if (this.taskScheduler == null) { // Executors 熟悉吧,JDK自带的并发框架来了。 this.localExecutor = Executors.newSingleThreadScheduledExecutor(); // Executor包装类,实现不同类型task的分发实现,如下述的CronTask this.taskScheduler = new ConcurrentTaskScheduler(this.localExecutor); } // ... if (this.cronTasks != null) { // cron 表达式类型的Task处理 for (CronTask task : this.cronTasks) { // 构造ReschedulingRunnable(task, trigger)真正实现task 调度 this.scheduledFutures.add(this.taskScheduler.schedule(task.getRunnable(), task.getTrigger())); } } // ... } }// ReschedulingRunnable见名知义,reuse-schedule class ReschedulingRunnable extends DelegatingErrorHandlingRunnable implements ScheduledFuture
4、finally,调度任务就乖乖的定时execute啦。
附ScheduledExecutorService自带Example:
// ScheduledExecutorService Usage Example static class BeeperControl { private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); public void beepForAnHour() { final Runnable beeper = new Runnable() { public void run() { System.out.println("beep"); } }; // beep every ten seconds final ScheduledFuture<?> beeperHandle = scheduler.scheduleAtFixedRate(beeper, 10, 10, TimeUnit.SECONDS); // after an hour cancel execution of task scheduler.schedule(new Runnable() { public void run() { beeperHandle.cancel(true); } }, 60 * 60, TimeUnit.SECONDS); } }
相关文章推荐
- Insight MyBatis-Spring 解析过程,调用执行
- Spring 3.2 源码解析 -- XML bean 元素到 BeanDefinition 解析过程
- spring中xml标签加载解析的一般过程
- Spring4.3.x 浅析xml配置的解析过程(5)——解析自定义命名空间的标签
- Spring4.3.x 浅析xml配置的解析过程(10)——解析aop命名空间之aspectj-autoproxy标签
- Spring4.3.x 浅析xml配置的解析过程(9)——解析aop命名空间之config标签
- spring IOC源码之解析xml中各个元素的过程
- Spring Boot启动过程完全解析(一)
- Spring4.3.x 浅析xml配置的解析过程(3)——使用DocumentLoader创建Document对象
- Spring4.3.x 浅析xml配置的解析过程(2)——使用ResourceLoader创建Resource对象
- Spring源码解析二:IOC容器初始化过程详解
- spring源码(3)之解析配置文件的过程
- Spring boot 启动过程解析 logback
- Spring4.3.x 浅析xml配置的解析过程(4)——解析bean标签及其所有子标签
- Spring启动后扫描解析注解的过程
- Spring之依赖解析过程
- springBoot中@Scheduled执行原理解析
- Insight spring @Transactional 属性解析应用
- Spring4.3.x 浅析xml配置的解析过程(7)——解析context命名空间之annotation-config标签
- [转] Spring4.3.x 浅析xml配置的解析过程(6)——解析context命名空间之property-placeholder和property-override标签