SpringBoot源码分析之@Scheduled

Springboot写上注解@Scheduled就可以实现定时任务,

这里对其源码做一点分析

@Service
public class MyScheduled {

    @Scheduled(cron="${time.cron}")
    void paoapaoScheduled() {
        System.out.println("Execute at " + System.currentTimeMillis());
    }
}

配置文件100秒一次

time.cron=*/100 * * * * *

 

重要的类ScheduledAnnotationBeanPostProcessor

@Nullable
	private BeanFactory beanFactory;
private void finishRegistration() {
		if (this.scheduler != null) {
			this.registrar.setScheduler(this.scheduler);
		}

		if (this.beanFactory instanceof ListableBeanFactory) {
			Map beans =
					((ListableBeanFactory) this.beanFactory).getBeansOfType(SchedulingConfigurer.class);
			List configurers = new ArrayList<>(beans.values());
			AnnotationAwareOrderComparator.sort(configurers);
			for (SchedulingConfigurer configurer : configurers) {
				configurer.configureTasks(this.registrar);
			}
		}

		if (this.registrar.hasTasks() && this.registrar.getScheduler() == null) {
			Assert.state(this.beanFactory != null, "BeanFactory must be set to find scheduler by type");
			try {
				// Search for TaskScheduler bean...
				this.registrar.setTaskScheduler(resolveSchedulerBean(this.beanFactory, TaskScheduler.class, false));
			}
			catch (NoUniqueBeanDefinitionException ex) {
				logger.trace("Could not find unique TaskScheduler bean", ex);
				try {
					this.registrar.setTaskScheduler(resolveSchedulerBean(this.beanFactory, TaskScheduler.class, true));
				}
				catch (NoSuchBeanDefinitionException ex2) {
					if (logger.isInfoEnabled()) {
						logger.info("More than one TaskScheduler bean exists within the context, and " +
								"none is named 'taskScheduler'. Mark one of them as primary or name it 'taskScheduler' " +
								"(possibly as an alias); or implement the SchedulingConfigurer interface and call " +
								"ScheduledTaskRegistrar#setScheduler explicitly within the configureTasks() callback: " +
								ex.getBeanNamesFound());
					}
				}
			}
			catch (NoSuchBeanDefinitionException ex) {
				logger.trace("Could not find default TaskScheduler bean", ex);
				// Search for ScheduledExecutorService bean next...
				try {
					this.registrar.setScheduler(resolveSchedulerBean(this.beanFactory, ScheduledExecutorService.class, false));
				}
				catch (NoUniqueBeanDefinitionException ex2) {
					logger.trace("Could not find unique ScheduledExecutorService bean", ex2);
					try {
						this.registrar.setScheduler(resolveSchedulerBean(this.beanFactory, ScheduledExecutorService.class, true));
					}
					catch (NoSuchBeanDefinitionException ex3) {
						if (logger.isInfoEnabled()) {
							logger.info("More than one ScheduledExecutorService bean exists within the context, and " +
									"none is named 'taskScheduler'. Mark one of them as primary or name it 'taskScheduler' " +
									"(possibly as an alias); or implement the SchedulingConfigurer interface and call " +
									"ScheduledTaskRegistrar#setScheduler explicitly within the configureTasks() callback: " +
									ex2.getBeanNamesFound());
						}
					}
				}
				catch (NoSuchBeanDefinitionException ex2) {
					logger.trace("Could not find default ScheduledExecutorService bean", ex2);
					// Giving up -> falling back to default scheduler within the registrar...
					logger.info("No TaskScheduler/ScheduledExecutorService bean found for scheduled processing");
				}
			}
		}

		this.registrar.afterPropertiesSet();
	}

bean 

SpringBoot源码分析之@Scheduled_第1张图片

找到自定义的MyScheduled

SpringBoot源码分析之@Scheduled_第2张图片

SpringBoot源码分析之@Scheduled_第3张图片

这里先逆向找到可见代码,再往上找到他的赋值地方

if (this.registrar.hasTasks() && this.registrar.getScheduler() == null) {
			Assert.state(this.beanFactory != null, "BeanFactory must be set to find scheduler by type");
			try {
				// Search for TaskScheduler bean...
				this.registrar.setTaskScheduler(resolveSchedulerBean(this.beanFactory, TaskScheduler.class, false));
			}
			catch (NoUniqueBeanDefinitionException ex) {
				logger.trace("Could not find unique TaskScheduler bean", ex);
				try {
					this.registrar.setTaskScheduler(resolveSchedulerBean(this.beanFactory, TaskScheduler.class, true));
				}

SpringBoot源码分析之@Scheduled_第4张图片

 


 

重要类ScheduledThreadPoolExecutor

参考:https://www.jianshu.com/p/502f9952c09b

 

ScheduledThreadPoolExecutor继承了ThreadPoolExecutor,也就是说ScheduledThreadPoolExecutor拥有execute()和submit()提交异步任务的基础功能,ScheduledThreadPoolExecutor类实现了ScheduledExecutorService,该接口定义了ScheduledThreadPoolExecutor能够延时执行任务和周期执行任务的功能。

ScheduledThreadPoolExecutor也两个重要的内部类:DelayedWorkQueueScheduledFutureTask。可以看出DelayedWorkQueue实现了BlockingQueue接口,也就是一个阻塞队列,ScheduledFutureTask则是继承了FutureTask类,也表示该类用于返回异步任务的结果。

SpringBoot源码分析之@Scheduled_第5张图片

/**
     * @throws RejectedExecutionException {@inheritDoc}
     * @throws NullPointerException       {@inheritDoc}
     */
    public ScheduledFuture schedule(Runnable command,
                                       long delay,
                                       TimeUnit unit) {
        if (command == null || unit == null)
            throw new NullPointerException();
        RunnableScheduledFuture t = decorateTask(command,
            new ScheduledFutureTask(command, null,
                                          triggerTime(delay, unit)));
        delayedExecute(t);
        return t;
    }

到了schedule这里:

SpringBoot源码分析之@Scheduled_第6张图片

SpringBoot源码分析之@Scheduled_第7张图片

SpringBoot源码分析之@Scheduled_第8张图片

增加定时任务ScheduledTaskRegistrar

protected void scheduleTasks() {
		if (this.taskScheduler == null) {
			this.localExecutor = Executors.newSingleThreadScheduledExecutor();
			this.taskScheduler = new ConcurrentTaskScheduler(this.localExecutor);
		}
		if (this.triggerTasks != null) {
			for (TriggerTask task : this.triggerTasks) {
				addScheduledTask(scheduleTriggerTask(task));
			}
		}
		if (this.cronTasks != null) {
			for (CronTask task : this.cronTasks) {
				addScheduledTask(scheduleCronTask(task));
			}
		}
		if (this.fixedRateTasks != null) {
			for (IntervalTask task : this.fixedRateTasks) {
				addScheduledTask(scheduleFixedRateTask(task));
			}
		}
		if (this.fixedDelayTasks != null) {
			for (IntervalTask task : this.fixedDelayTasks) {
				addScheduledTask(scheduleFixedDelayTask(task));
			}
		}
	}

遍历arrayList 

SpringBoot源码分析之@Scheduled_第9张图片

 

跳出refresh

SpringBoot源码分析之@Scheduled_第10张图片

 

SpringBoot源码分析之@Scheduled_第11张图片

Spring如何添加钩子函数

这里没有注册钩子函数自然是null

SpringBoot源码分析之@Scheduled_第12张图片

进Runtime.getRuntime().addShutdownHook(this.shutdownHook);

SpringBoot源码分析之@Scheduled_第13张图片

SpringBoot源码分析之@Scheduled_第14张图片

SpringBoot源码分析之@Scheduled_第15张图片

终于完了 

SpringBoot源码分析之@Scheduled_第16张图片

============================

触发定时

SpringBoot源码分析之@Scheduled_第17张图片

倒着看就知道怎么调用的,实际上是反射执行上述方法的

invoke方法用来在运行时动态地调用某个实例的方法

SpringBoot源码分析之@Scheduled_第18张图片

这里实行了Runnable 接口: 

SpringBoot源码分析之@Scheduled_第19张图片

SpringBoot源码分析之@Scheduled_第20张图片

SpringBoot源码分析之@Scheduled_第21张图片

反射,线程,线程池 底层还是这些技术!需要好好研究这块代码,加深理解基本原理。

 

扩展信息:SpringBoot中定时任务默认是串行执行 如何设置并行

SpringBoot 使用@Scheduled注解配置串行、并行定时任务

Spring Boot 中实现定时任务的两种方式

你可能感兴趣的:(SpringBoot)