当前位置:   article > 正文

Spring异步任务@Async的默认线程池执行器是如何初始化的

Spring异步任务@Async的默认线程池执行器是如何初始化的

Spring异步任务@Async的默认线程池执行器,是从哪里来?是如何初始化的?

结论先行

异步任务@Async的默认线程池执行器是通过TaskExecutionAutoConfiguration#applicationTaskExecutor自动注入的。

异步任务的线程池执行器是如何初始化的?

从异步调用拦截器作为切入点,看看异步调用拦截器的调用流程是咋样的?

AsyncExecutionInterceptor#invoke

org.springframework.aop.interceptor.AsyncExecutionInterceptor

异步任务的执行调用入口
AsyncExecutionInterceptor#invoke —> AsyncExecutionAspectSupport#determineAsyncExecutor
AsyncExecutionInterceptor.invoke()

AsyncExecutionAspectSupport#determineAsyncExecutor

AsyncExecutionAspectSupport#determineAsyncExecutor —> 先调用getExecutorQualifier(method),再调用this.defaultExecutor.get()
AsyncExecutionAspectSupport.determineAsyncExecutor()

AnnotationAsyncExecutionInterceptor#getExecutorQualifier

getExecutorQualifier方法在AnnotationAsyncExecutionInterceptor#getExecutorQualifier被覆盖重写
AnnotationAsyncExecutionInterceptor.getExecutorQualifier()
AnnotatedElementUtils.findMergedAnnotation(method, Async.class)会返回@Async注解实例。
AnnotatedElementUtils.findMergedAnnotation(method, Async.class)是如何查找@Async注解?会起另一篇文章说明。

defaultExecutor是如何初始化的?

接下来,看看defaultExecutor是如何初始化的?
defaultExecutor初始化 —> getDefaultExecutor(this.beanFactory)
AsyncExecutionAspectSupport()

AsyncExecutionAspectSupport#getDefaultExecutor

AsyncExecutionAspectSupport#getDefaultExecutor —> 先调用beanFactory.getBean(TaskExecutor.class),再调用beanFactory.getBean(DEFAULT_TASK_EXECUTOR_BEAN_NAME, Executor.class)
AsyncExecutionAspectSupport.getDefaultExecutor()
AsyncExecutionInterceptor#getDefaultExecutor覆盖重写了本方法
debug调试发现super.getDefaultExecutor(beanFactory)已返回默认的执行器实例,说明是从beanFactory里获取的。
【猜想】可能是在哪个AutoConfiguration里自动注入的?
AsyncExecutionInterceptor.getDefaultExecutor()
查看beanFactory里注入的bean实例列表,发现如下线索。
TaskExecutionAutoConfiguration

applicationTaskExecutor -> {BeanDefinitionHolder@17669} "Bean definition with name 'applicationTaskExecutor' and aliases [taskExecutor]: Root bean: class [null]; scope=singleton; abstract=false; lazyInit=true; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration; factoryMethodName=applicationTaskExecutor; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/task/TaskExecutionAutoConfiguration.class]"
  • 1

DefaultExecutor是否从TaskExecutionAutoConfiguration实例化而来

实例内存地址id是否一样?

TaskExecutionAutoConfiguration#applicationTaskExecutor

org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration

通过debug验证猜想,根据实例内存地址id,就是这里生成的。
TaskExecutionAutoConfiguration#taskExecutorBuilder
TaskExecutionAutoConfiguration.taskExecutorBuilder()
TaskExecutionAutoConfiguration#applicationTaskExecutor
TaskExecutionAutoConfiguration.applicationTaskExecutor()
实例内存地址id都是ThreadPoolTaskExecutor@11620
org.springframework.beans.factory.support.SimpleInstantiationStrategy#instantiate
SimpleInstantiationStrategy.instantiate()

AsyncExecutionInterceptor#getDefaultExecutor

org.springframework.aop.interceptor.AsyncExecutionInterceptor#getDefaultExecutor
AsyncExecutionInterceptor.getDefaultExecutor()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/682773
推荐阅读
相关标签
  

闽ICP备14008679号