Springboot @Scheduled实现原理

文章目录

  • 实现原理
    • 1.开启计划任务
      • ScheduledAnnotationBeanPostProcessor
    • 2.创建任务并放入到任务列表
      • 依次加载所有的实现Scheduled注解的类方法
      • 将对应类型的定时器放入相应的定时任务列表中
    • 3.执行定时任务
      • 任务列表提交到线程池,任务开始定时执行
  • 定时任务线程池执行原理
  • 注意事项


实现原理

1.开启计划任务

@EnableScheduling注解用于开启计划任务。

   @Target({ElementType.TYPE})
   @Retention(RetentionPolicy.RUNTIME)
   @Import({SchedulingConfiguration.class})
   @Documented
   public @interface EnableScheduling {
   }
@Configuration
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public class SchedulingConfiguration {
 
	@Bean(name = TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)
	@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
	public ScheduledAnnotationBeanPostProcessor scheduledAnnotationProcessor() {
		return new ScheduledAnnotationBeanPostProcessor();
	}
 
}

通过@Import引入了SchedulingConfiguration这个配置类,使这个配置类生效。向spring容器中注入了一个bean:ScheduledAnnotationBeanPostProcessor。

ScheduledAnnotationBeanPostProcessor

  • ScheduledAnnotationBeanPostProcessor主要实现了两个接口

    • DestructionAwareBeanPostProcessor
    • ApplicationListener
  • ScheduledAnnotationBeanPostProcessor间接继承了BeanPostProcessor类

2.创建任务并放入到任务列表

spring在初始化bean后,通过ScheduledAnnotationBeanPostProcessor的postProcessAfterInitialization方法拦截到所有的用到@Scheduled注解的方法,并解析相应的的注解参数,放入“定时任务列表”等待后续处理
@Scheduled注解配置可参考文章:@Scheduled注解详解

依次加载所有的实现Scheduled注解的类方法

ScheduledAnnotationBeanPostProcessor的postProcessAfterInitialization方法

@Override
public Object postProcessAfterInitialization(final Object bean, String beanName) {
   Class<?> targetClass = AopProxyUtils.ultimateTargetClass(bean);
   if (!this.nonAnnotatedClasses.contains(targetClass)) {
      Map<Method, Set<Scheduled>> annotatedMethods = MethodIntrospector.selectMethods(targetClass,
            (MethodIntrospector.MetadataLookup<Set<Scheduled>>) method -> {
               Set<Scheduled> scheduledMethods = AnnotatedElementUtils.getMergedRepeatableAnnotations(
                     method, Scheduled.class, Schedules.class);
               return (!scheduledMethods.isEmpty() ? scheduledMethods : null);
            });
      if (annotatedMethods.isEmpty()) {
         this.nonAnnotatedClasses.add(targetClass);
         if (logger.isTraceEnabled()) {
            logger.trace("No @Scheduled annotations found on bean class: " + bean.getClass());
         }
      }
      else {
         // Non-empty set of methods
         annotatedMethods.forEach((method, scheduledMethods) ->
               scheduledMethods.forEach(scheduled -> processScheduled(scheduled, method, bean)));
         if (logger.isDebugEnabled()) {
            logger.debug(annotatedMethods.size() + " @Scheduled methods processed on bean '" + beanName +
                  "': " + annotatedMethods);
         }
      }
   }
   return bean;
}

遍历了每个bean的方法,并找出使用了@Scheduled的方法,调用processScheduled进行处理。

将对应类型的定时器放入相应的定时任务列表中

ScheduledAnnotationBeanPostProcessor的processScheduled方法

protected void processScheduled(Scheduled scheduled, Method method, Object bean) {
   try {
      Assert.isTrue(method.getParameterCount() == 0,
            "Only no-arg methods may be annotated with @Scheduled");
 
      Method invocableMethod = AopUtils.selectInvocableMethod(method, bean.getClass());
      Runnable runnable = new ScheduledMethodRunnable(bean, invocableMethod);
      boolean processedSchedule = false;
      String errorMessage =
            "Exactly one of the 'cron', 'fixedDelay(String)', or 'fixedRate(String)' attributes is required";
 
      Set<ScheduledTask> tasks = new LinkedHashSet<>(4);
 
      // Determine initial delay
      long initialDelay = scheduled.initialDelay();
      String initialDelayString = scheduled.initialDelayString();
      if (StringUtils.hasText(initialDelayString)) {
         Assert.isTrue(initialDelay < 0, "Specify 'initialDelay' or 'initialDelayString', not both");
         if (this.embeddedValueResolver != null) {
            initialDelayString = this.embeddedValueResolver.resolveStringValue(initialDelayString);
         }
         if (StringUtils.hasLength(initialDelayString)) {
            try {
               initialDelay = parseDelayAsLong(initialDelayString);
            }
            catch (RuntimeException ex) {
               throw new IllegalArgumentException(
                     "Invalid initialDelayString value \"" + initialDelayString + "\" - cannot parse into long");
            }
         }
      }
 
      // Check cron expression
      String cron = scheduled.cron();
      if (StringUtils.hasText(cron)) {
         String zone = scheduled.zone();
         if (this.embeddedValueResolver != null) {
            cron = this.embeddedValueResolver.resolveStringValue(cron);
            zone = this.embeddedValueResolver.resolveStringValue(zone);
         }
         if (StringUtils.hasLength(cron)) {
            Assert.isTrue(initialDelay == -1, "'initialDelay' not supported for cron triggers");
            processedSchedule = true;
            TimeZone timeZone;
            if (StringUtils.hasText(zone)) {
               timeZone = StringUtils.parseTimeZoneString(zone);
            }
            else {
               timeZone = TimeZone.getDefault();
            }
            tasks.add(this.registrar.scheduleCronTask(new CronTask(runnable, new CronTrigger(cron, timeZone))));
         }
      }
 
      // At this point we don't need to differentiate between initial delay set or not anymore
      if (initialDelay < 0) {
         initialDelay = 0;
      }
 
      // Check fixed delay
      long fixedDelay = scheduled.fixedDelay();
      if (fixedDelay >= 0) {
         Assert.isTrue(!processedSchedule, errorMessage);
         processedSchedule = true;
         tasks.add(this.registrar.scheduleFixedDelayTask(new FixedDelayTask(runnable, fixedDelay, initialDelay)));
      }
      String fixedDelayString = scheduled.fixedDelayString();
      if (StringUtils.hasText(fixedDelayString)) {
         if (this.embeddedValueResolver != null) {
            fixedDelayString = this.embeddedValueResolver.resolveStringValue(fixedDelayString);
         }
         if (StringUtils.hasLength(fixedDelayString)) {
            Assert.isTrue(!processedSchedule, errorMessage);
            processedSchedule = true;
            try {
               fixedDelay = parseDelayAsLong(fixedDelayString);
            }
            catch (RuntimeException ex) {
               throw new IllegalArgumentException(
                     "Invalid fixedDelayString value \"" + fixedDelayString + "\" - cannot parse into long");
            }
            tasks.add(this.registrar.scheduleFixedDelayTask(new FixedDelayTask(runnable, fixedDelay, initialDelay)));
         }
      }
 
      // Check fixed rate
      long fixedRate = scheduled.fixedRate();
      if (fixedRate >= 0) {
         Assert.isTrue(!processedSchedule, errorMessage);
         processedSchedule = true;
         tasks.add(this.registrar.scheduleFixedRateTask(new FixedRateTask(runnable, fixedRate, initialDelay)));
      }
      String fixedRateString = scheduled.fixedRateString();
      if (StringUtils.hasText(fixedRateString)) {
         if (this.embeddedValueResolver != null) {
            fixedRateString = this.embeddedValueResolver.resolveStringValue(fixedRateString);
         }
         if (StringUtils.hasLength(fixedRateString)) {
            Assert.isTrue(!processedSchedule, errorMessage);
            processedSchedule = true;
            try {
               fixedRate = parseDelayAsLong(fixedRateString);
            }
            catch (RuntimeException ex) {
               throw new IllegalArgumentException(
                     "Invalid fixedRateString value \"" + fixedRateString + "\" - cannot parse into long");
            }
            tasks.add(this.registrar.scheduleFixedRateTask(new FixedRateTask(runnable, fixedRate, initialDelay)));
         }
      }
 
      // Check whether we had any attribute set
      Assert.isTrue(processedSchedule, errorMessage);
 
      // Finally register the scheduled tasks
      synchronized (this.scheduledTasks) {
         Set<ScheduledTask> registeredTasks = this.scheduledTasks.get(bean);
         if (registeredTasks == null) {
            registeredTasks = new LinkedHashSet<>(4);
            this.scheduledTasks.put(bean, registeredTasks);
         }
         registeredTasks.addAll(tasks);
      }
   }
   catch (IllegalArgumentException ex) {
      throw new IllegalStateException(
            "Encountered invalid @Scheduled method '" + method.getName() + "': " + ex.getMessage());
   }
}

解析@Scheduled注解传进来的参数(cron表达式或者延时等)并创建不同类型的任务提交到registrar中,registrar是用来注册要放到计划任务线程池中运行的任务的,它里面包含一个线程池和任务列表,这一步只是放到了列表中。

现在@Scheduled已经被解析,并转成了Runable任务对像放到了ScheduledTaskRegistrar对像中了,现在就差把它们放到线程池中启动了。

3.执行定时任务

任务列表提交到线程池,任务开始定时执行

ScheduledAnnotationBeanPostProcessor实现了ApplicationListener的接口。

当Spring容器初始化完成会触发ContextRefreshedEvent事件,调用ScheduledAnnotationBeanPostProcessor的onApplicationEvent方法,方法调用finishRegistration完成任务列表注册到线程池,任务开始定时执行。

public void onApplicationEvent(ContextRefreshedEvent event) {
    if (event.getApplicationContext() == this.applicationContext) {
        this.finishRegistration();
    }
}
private void finishRegistration() {
		if (this.scheduler != null) {
			this.registrar.setScheduler(this.scheduler);
		}
 
		if (this.beanFactory instanceof ListableBeanFactory) {
			Map<String, SchedulingConfigurer> beans =
					((ListableBeanFactory) this.beanFactory).getBeansOfType(SchedulingConfigurer.class);
			List<SchedulingConfigurer> 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(beanFactory, TaskScheduler.class, false));
			}
			catch (NoUniqueBeanDefinitionException ex) {
				logger.debug("Could not find unique TaskScheduler bean", ex);
				try {
					this.registrar.setTaskScheduler(resolveSchedulerBean(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.debug("Could not find default TaskScheduler bean", ex);
				// Search for ScheduledExecutorService bean next...
				try {
					this.registrar.setScheduler(resolveSchedulerBean(beanFactory, ScheduledExecutorService.class, false));
				}
				catch (NoUniqueBeanDefinitionException ex2) {
					logger.debug("Could not find unique ScheduledExecutorService bean", ex2);
					try {
						this.registrar.setScheduler(resolveSchedulerBean(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.debug("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();
	}
  1. 找到SchedulingConfigurer接口的所有所有实现类,调用它的configureTasks方法

    通常我们可以自定义一个类实现SchedulingConfigurer,并对registrar里面的属性赋值,最常用的做法是自定线程池注入到registrar中,因为registrar默认是单线程的线程池,也即是说@Scheduled方法是串行的

  2. 对ScheduledTaskRegistrar对像中的TaskScheduler属性赋值

    TaskScheduler这个属性即是线程池对像,如果在第一步我们已经自己指定了一个线程池,这部分代码会跳过。

  3. 调用this.registrar.afterPropertiesSet()

    这一步很重要,它的功能就是把所有任务提交到线程池中。

    @Override
    	public void afterPropertiesSet() {
    		scheduleTasks();
    	}
     
    	/**
    	 * Schedule all registered tasks against the underlying
    	 * {@linkplain #setTaskScheduler(TaskScheduler) task scheduler}.
    	 */
    	@SuppressWarnings("deprecation")
    	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));
    			}
    		}
    	}
    

    ScheduledThreadPoolExecutor的run方法

    //说明:每次执行定时任务结束后,会先设置下下次定时任务的执行时间,以此来确认下次任务的执行时间。
    public void run() {
        boolean periodic = isPeriodic();
        if (!canRunInCurrentRunState(periodic))
            cancel(false);
        else if (!periodic)
            ScheduledFutureTask.super.run();
        else if (ScheduledFutureTask.super.runAndReset()) {
            setNextRunTime();
            reExecutePeriodic(outerTask);
        }
    }
    
    1. 首先判断线程池是否为空,如果是空,就新建一个单线程的线程池
    2. 先执行corn,计算出相应的下次执行时间,放入线程池中。再执行fixedRate,计算出相应的下次执行时间,放入线程池中。
    3. 线程池中某一任务到达执行时间,开始执行,执行完成后会先设置下次定时任务的执行时间,放到线程池中。

到这一步,@Scheduled作用生效,方法将会定时执行了。

定时任务线程池执行原理

定时任务使用的一般是ScheduledThreadPoolExecutor线程池
该类型线程池解析见该文章:Java线程池解析

注意事项

  1. 任务默认是单线程执行,前一个任务执行完毕,才会继续执行下一个任务。所以上一个任务一直未完成,会导致后面的任务全部“失效”。
  2. 如果多个定时任务定义的是同一个时间,那么也是顺序执行的,会根据程序加载Scheduled方法的先后来执行。先执行cron,之后再执行fixedRate。

你可能感兴趣的:(SpringBoot,定时任务,spring,boot,java,spring)