Springboot@Scheduled注解实现原理
Springboot 通过@Scheduled实现定时任务的思路:
利⽤springboot的BeanPostProcessor接⼝特性,当⼀个类实现了BeanPostProcessor之后,spring 容器在初始化系统的每个bean的时候都会调⽤这个类实现的BeanPostProcessor的接⼝⽅法,并把bean对像和名称作为参数传给这个类对像。
BeanPostProcessor接⼝⽅法有两个:
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
当⼀个bean被初始化之前会调⽤BeanPostProcessor对像的postProcessBeforeInitialization⽅法,初始化完成后调⽤postProcessAfterInitialization⽅法,因此可以在postProcessAfterInitialization⾥⾯遍历每个bean的所有⽅法,检查它们是否有@Scheduled 注解,如果有则新建⼀个runnable,这个runnable⽤⼀个成员属性保存这个bean的对像引⽤,run执⾏的时候通过反射调⽤这个被
@scheduled标记的⽅法就可以了。
当遍历了所有的bean,⽣成了⼀系列的runnable任务之后,需要把它们丢到线程池中运⾏,这时候需要利⽤到ApplicationListener接⼝特性。
ApplicationListener接⼝只有⼀个接⼝⽅法:
void onApplicationEvent(E event);
当spring 容器把所有的bean都实例化好了,并启动完成之后会调⽤实现这个接⼝的所有对像的onApplicationEvent⽅法,这个⽅法⾥⾯可以做⼀些系统启动需要执⾏的⼀些业务;
与BeanPostProcessor不同的是⼀个对像的这个接⼝⽅法只调⽤⼀次,⽽BeanPostProcessor的是每建⼀个bean就会调⽤它的对像的⽅法⼀次,因此可以在这个⽅法⾥⾯把前⾯⽣成的计划任务丢到线程池⾥⾯运⾏。
以上是springboot的实现思路,下⾯⼀步⼀步分析源码:
我们都是知道要使@Scheduled⽣效就得先⽤@EnableScheduling来开启计划任务
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Import({SchedulingConfiguration.class})
@Documented
public @interface EnableScheduling {
}
这是@EnableScheduling的定义,这⾥⾯通过@Import引⼊了SchedulingConfiguration这个配置类,使这个配置类⽣效,我们看看SchedulingConfiguration⾥⾯做了什么事情:
@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();
}
}
这个配置类中只做了⼀件事情,就是通过@Bean向spring容器中注⼊了⼀个bean:ScheduledAnnotationBeanPostProcessor
进⼊ScheduledAnnotationBeanPostProcessor类
springboot aoppublic class ScheduledAnnotationBeanPostProcessor implements ScheduledTaskHolder,
MergedBeanDefinitionPostProcessor,
DestructionAwareBeanPostProcessor,
Ordered,
EmbeddedValueResolverAware,
BeanNameAware, BeanFactoryAware,
ApplicationContextAware,
SmartInitializingSingleton,
ApplicationListener<ContextRefreshedEvent>,
DisposableBean {
......
}
重点注意ScheduledAnnotationBeanPostProcessor实现了两个接⼝:DestructionAwareBeanPostProcessor和ApplicationListener接⼝。
ScheduledAnnotationBeanPostProcessor接⼝继承了BeanPostProcessor,按⼀开始所说的思路我们关注它对postProcessAfterInitialization接⼝⽅法的实现:
@Override
public Object postProcessAfterInitialization(final Object bean, String beanName) {
Class<?> targetClass = AopProxyUtils.ultimateTargetClass(bean);
if (!ains(targetClass)) {
Map<Method, Set<Scheduled>> annotatedMethods = MethodIntrospector.selectMethods(targetClass,
(MethodIntrospector.MetadataLookup<Set<Scheduled>>) method -> {
Set<Scheduled> scheduledMethods = MergedRepeatableAnnotations(
method, Scheduled.class, Schedules.class);
return (!scheduledMethods.isEmpty() ? scheduledMethods : null);
});
if (annotatedMethods.isEmpty()) {
if (logger.isTraceEnabled()) {
}
}
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进⾏处理,processScheduled⽅法内容:
protected void processScheduled(Scheduled scheduled, Method method, Object bean) {
try {
Assert.ParameterCount() == 0,
"Only no-arg methods may be annotated with @Scheduled");
Method invocableMethod = AopUtils.selectInvocableMethod(method, Class());
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 (beddedValueResolver != null) {
initialDelayString = solveStringValue(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 = ();
if (StringUtils.hasText(cron)) {
String zone = ();
if (beddedValueResolver != null) {
cron = solveStringValue(cron);
zone = solveStringValue(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 = Default();
}
tasks.istrar.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.istrar.scheduleFixedDelayTask(new FixedDelayTask(runnable, fixedDelay, initialDelay))); }
String fixedDelayString = scheduled.fixedDelayString();
if (StringUtils.hasText(fixedDelayString)) {
if (beddedValueResolver != null) {
fixedDelayString = solveStringValue(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.istrar.scheduleFixedDelayTask(new FixedDelayTask(runnable, fixedDelay, initialDelay))); }
}
// Check fixed rate
long fixedRate = scheduled.fixedRate();
if (fixedRate >= 0) {
Assert.isTrue(!processedSchedule, errorMessage);
processedSchedule = true;
tasks.istrar.scheduleFixedRateTask(new FixedRateTask(runnable, fixedRate, initialDelay)));
}
String fixedRateString = scheduled.fixedRateString();
if (StringUtils.hasText(fixedRateString)) {
if (beddedValueResolver != null) {
fixedRateString = solveStringValue(fixedRateString);
fixedRateString = solveStringValue(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.istrar.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 = (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 '" + Name() + "': " + ex.getMessage());
}
}
这个⽅法内容很多,但逻辑并不复杂,⼀是新建runable,⼆是解析@Scheduled注解传进来的参数(cron表达式或者延时等)并创建不同类型的任务提交到registrar中,registrar是⽤来注册要放到计划任务线程池中运⾏的任务的,它⾥⾯包含⼀个线程池和任务列表,这⼀步只是放到了列表中。
创建Runable的⽅法:
protected Runnable createRunnable(Object target, Method method) {
Assert.ParameterCount() == 0, "Only no-arg methods may be annotated with @Scheduled");
Method invocableMethod = AopUtils.selectInvocableMethod(method, Class());
return new ScheduledMethodRunnable(target, invocableMethod);
}
返回的是new 的⼀个ScheduledMethodRunnable对像
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论