@Async原理
前言
在开发过程中,我们会遇到很多使用线程池的业务场景,例如定时任务使用的就是ScheduledThreadPoolExecutor
。而有些时候使用线程池的场景就是会将一些可以进行异步操作的业务放在线程池中去完成,例如在生成订单的时候给用户发送短信,生成订单的结果不应该被发送短信的成功与否所左右,也就是说生成订单这个主操作是不依赖于发送短信这个操作,所以我们就可以把发送短信这个操作置为异步操作。而要想完成异步操作,一般使用的一个是消息服务器MQ,一个就是线程池。今天我们就来看看在Java中常用的Spring框架中如何去使用线程池来完成异步操作,以及分析背后的原理。
在Spring4中,Spring中引入了一个新的注解@Async
,这个注解让我们在使用Spring完成异步操作变得非常方便。
在SpringBoot环境中,要使用@Async
注解,我们需要先在启动类上加上@EnableAsync
注解。这个与在SpringBoot中使用@Scheduled
注解需要在启动类中加上@EnableScheduling
是一样的道理(当然你使用古老的XML配置也是可以的,但是在SpringBoot环境中,建议的是全注解开发),具体原理下面会分析。加上@EnableAsync
注解后,如果我们想在调用一个方法的时候开启一个新的线程开始异步操作,我们只需要在这个方法上加上@Async
注解,当然前提是,这个方法所在的类必须在Spring环境中。
@EnableAsync
我们先来分析@EnableAsync
注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AsyncConfigurationSelector.class)
public @interface EnableAsync {
/**
* Indicate the 'async' annotation type to be detected at either class
* or method level.
* <p>By default, both Spring's @{@link Async} annotation and the EJB 3.1
* {@code @javax.ejb.Asynchronous} annotation will be detected.
* <p>This attribute exists so that developers can provide their own
* custom annotation type to indicate that a method (or all methods of
* a given class) should be invoked asynchronously.
*/
//默认情况下,要开启异步操作,要在相应的方法或者类上加上@Async注解或者EJB3.1规范下的@Asynchronous注解。
//这个属性使得开发人员可以自己设置开启异步操作的注解
Class<? extends Annotation> annotation() default Annotation.class;
/**
* Indicate whether subclass-based (CGLIB) proxies are to be created as opposed
* to standard Java interface-based proxies.
* <p><strong>Applicable only if the {@link #mode} is set to {@link AdviceMode#PROXY}</strong>.
* <p>The default is {@code false}.
* <p>Note that setting this attribute to {@code true} will affect <em>all</em>
* Spring-managed beans requiring proxying, not just those marked with {@code @Async}.
* For example, other beans marked with Spring's {@code @Transactional} annotation
* will be upgraded to subclass proxying at the same time. This approach has no
* negative impact in practice unless one is explicitly expecting one type of proxy
* vs. another — for example, in tests.
*/
//默认false,不建议开启,因为会把所有Spring管理的Bean都设为cglib代理
boolean proxyTargetClass() default false;
/**
* Indicate how async advice should be applied.
* <p><b>The default is {@link AdviceMode#PROXY}.</b>
* Please note that proxy mode allows for interception of calls through the proxy
* only. Local calls within the same class cannot get intercepted that way; an
* {@link Async} annotation on such a method within a local call will be ignored
* since Spring's interceptor does not even kick in for such a runtime scenario.
* For a more advanced mode of interception, consider switching this to
* {@link AdviceMode#ASPECTJ}.
*/
//默认的mode是AdviceMode.PROXY
/**
@Aspect一开始是AspectJ推出的Java注解形式,后来Spring AOP也支持使用这种形式表示切面,但实际上底层实现和AspectJ毫无关系,毕竟Spring AOP是动态代理,和静态代理是不兼容的。
AspectJ 在运行前直接将横切关注点编织到实际代码中,是静态代理。
而在Spring AOP中实现的是动态代理,默认使用JDK动态代理,由于JDK动态代理的天然限制,只能对接口做代理,所以当直接对类代理的时候使用的CGLIB代理,无论JDK动态代理还是CGLIB代理都是动态代理,而AspectJ是静态代理。
但是Spring AOP兼容了AspectJ的注解显式,但是我们要明确,我们在Spring AOP中使用的@Aspect注解与AspectJ是没有太大关系的。
由于Spring AOP是动态代理的,他存在一些缺陷,当同一类中的方法相互调用时,内部的方法如果有代理方法的话,也是不会调用代理方法的,这个时候代理方法是会失效的,原因就在于当在同一个类中进行方法调间用时,默认是```this.insideMethod()```,在Spring AOP动态代理中,依旧会调用原内部方法,而不是代理后的内部方法。AspectJ这种静态代理的方式会解决上述问题,所以当你想要使用这种特性的话,可以使用ASPECTJ方式。
但是个人还是不建议使用这种方式,想要解决同一个类中方法间调用,内部方法不走代理方法的问题,其实还是有其他的解决方法的,没有必要这个问题抛弃Spring AOP而使用静态代理。
Spring AOP和AspectJ的比较 https://www.jianshu.com/p/872d3dbdc2ca
*/
AdviceMode mode() default AdviceMode.PROXY;
/**
* Indicate the order in which the {@link AsyncAnnotationBeanPostProcessor}
* should be applied.
* <p>The default is {@link Ordered#LOWEST_PRECEDENCE} in order to run
* after all other post-processors, so that it can add an advisor to
* existing proxies rather than double-proxy.
*/
int order() default Ordered.LOWEST_PRECEDENCE;
}
我们又看到了@Import
注解,还是一样的套路。
AsyncConfigurationSelector
@Override
public String[] selectImports(AdviceMode adviceMode) {
switch (adviceMode) {
//EnableAsync的默认mode是Proxy,会注入ProxyAsyncConfiguration这个配置类
case PROXY:
return new String[] { ProxyAsyncConfiguration.class.getName() };
case ASPECTJ:
return new String[] { ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME };
default:
return null;
}
}
前面分析@EnableAsync
注解的时候已经分析过了,默认使用的是ProxyAsyncConfiguration这个配置类。
@Configuration
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public class ProxyAsyncConfiguration extends AbstractAsyncConfiguration {
//在这个类中注入了AsyncAnnotationBeanPostProcessor,很显然这是个BeanPostProcessor
@Bean(name = TaskManagementConfigUtils.ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public AsyncAnnotationBeanPostProcessor asyncAdvisor() {
//必须要有@EnableAsync
Assert.notNull(this.enableAsync, "@EnableAsync annotation metadata was not injected");
AsyncAnnotationBeanPostProcessor bpp = new AsyncAnnotationBeanPostProcessor();
//看是否有自定义的开启异步操作注解
Class<? extends Annotation> customAsyncAnnotation = this.enableAsync.getClass("annotation");
if (customAsyncAnnotation != AnnotationUtils.getDefaultValue(EnableAsync.class, "annotation")) {
bpp.setAsyncAnnotationType(customAsyncAnnotation);
}
//设置线程池配置,属性值在父类中获得
if (this.executor != null) {
bpp.setExecutor(this.executor);
}
//设置异常处理器配置,属性值在父类中获得
if (this.exceptionHandler != null) {
bpp.setExceptionHandler(this.exceptionHandler);
}
//默认不使用CGLIB代理
bpp.setProxyTargetClass(this.enableAsync.getBoolean("proxyTargetClass"));
//默认的优先级是最低
bpp.setOrder(this.enableAsync.<Integer>getNumber("order"));
return bpp;
}
}
@Configuration
public abstract class AbstractAsyncConfiguration implements ImportAware {
protected AnnotationAttributes enableAsync;
protected Executor executor;
protected AsyncUncaughtExceptionHandler exceptionHandler;
@Override
public void setImportMetadata(AnnotationMetadata importMetadata) {
this.enableAsync = AnnotationAttributes.fromMap(
importMetadata.getAnnotationAttributes(EnableAsync.class.getName(), false));
if (this.enableAsync == null) {
throw new IllegalArgumentException(
"@EnableAsync is not present on importing class " + importMetadata.getClassName());
}
}
/**
* Collect any {@link AsyncConfigurer} beans through autowiring.
*/
@Autowired(required = false)
void setConfigurers(Collection<AsyncConfigurer> configurers) {
if (CollectionUtils.isEmpty(configurers)) {
return;
}
//AsyncConfigurer用来配置线程池配置以及异常处理器,而且在Spring环境中最多只能有一个,在这里我们知道了,如果想要自己去配置线程池,只需要实现AsyncConfigurer接口,并且不可以在Spring环境中有多个实现AsyncConfigurer的类。
if (configurers.size() > 1) {
throw new IllegalStateException("Only one AsyncConfigurer may exist");
}
AsyncConfigurer configurer = configurers.iterator().next();
this.executor = configurer.getAsyncExecutor();
this.exceptionHandler = configurer.getAsyncUncaughtExceptionHandler();
}
}
AbstractAsyncConfiguration中的setConfigurers
说明了我们可以通过实现AsyncConfigurer接口来完成线程池以及异常处理器的配置,而且在Spring环境中只能配置一个实现类,否则会抛出异常。
AsyncAnnotationBeanPostProcessor
这个BeanPostBeanPostProcessor
很显然会对带有能够引发异步操作的注解(比如@Async
)的Bean进行处理。我们来具体看一下。
@SuppressWarnings("serial")
public class AsyncAnnotationBeanPostProcessor extends AbstractBeanFactoryAwareAdvisingPostProcessor {
/**
* The default name of the {@link TaskExecutor} bean to pick up: "taskExecutor".
* <p>Note that the initial lookup happens by type; this is just the fallback
* in case of multiple executor beans found in the context.
* @since 4.2
* @see AnnotationAsyncExecutionInterceptor#DEFAULT_TASK_EXECUTOR_BEAN_NAME
*/
public static final String DEFAULT_TASK_EXECUTOR_BEAN_NAME =
AnnotationAsyncExecutionInterceptor.DEFAULT_TASK_EXECUTOR_BEAN_NAME;
protected final Log logger = LogFactory.getLog(getClass());
private Class<? extends Annotation> asyncAnnotationType;
private Executor executor;
private AsyncUncaughtExceptionHandler exceptionHandler;
public AsyncAnnotationBeanPostProcessor() {
setBeforeExistingAdvisors(true);
}
/**
* Set the 'async' annotation type to be detected at either class or method
* level. By default, both the {@link Async} annotation and the EJB 3.1
* {@code javax.ejb.Asynchronous} annotation will be detected.
* <p>This setter property exists so that developers can provide their own
* (non-Spring-specific) annotation type to indicate that a method (or all
* methods of a given class) should be invoked asynchronously.
* @param asyncAnnotationType the desired annotation type
*/
public void setAsyncAnnotationType(Class<? extends Annotation> asyncAnnotationType) {
Assert.notNull(asyncAnnotationType, "'asyncAnnotationType' must not be null");
this.asyncAnnotationType = asyncAnnotationType;
}
/**
* Set the {@link Executor} to use when invoking methods asynchronously.
* <p>If not specified, default executor resolution will apply: searching for a
* unique {@link TaskExecutor} bean in the context, or for an {@link Executor}
* bean named "taskExecutor" otherwise. If neither of the two is resolvable,
* a local default executor will be created within the interceptor.
* @see AsyncAnnotationAdvisor#AsyncAnnotationAdvisor(Executor, AsyncUncaughtExceptionHandler)
* @see AnnotationAsyncExecutionInterceptor#getDefaultExecutor(BeanFactory)
* @see #DEFAULT_TASK_EXECUTOR_BEAN_NAME
*/
public void setExecutor(Executor executor) {
this.executor = executor;
}
/**
* Set the {@link AsyncUncaughtExceptionHandler} to use to handle uncaught
* exceptions thrown by asynchronous method executions.
* @since 4.1
*/
public void setExceptionHandler(AsyncUncaughtExceptionHandler exceptionHandler) {
this.exceptionHandler = exceptionHandler;
}
@Override
public void setBeanFactory(BeanFactory beanFactory) {
super.setBeanFactory(beanFactory);
AsyncAnnotationAdvisor advisor = new AsyncAnnotationAdvisor(this.executor, this.exceptionHandler);
if (this.asyncAnnotationType != null) {
advisor.setAsyncAnnotationType(this.asyncAnnotationType);
}
advisor.setBeanFactory(beanFactory);
this.advisor = advisor;
}
}
我们注意到他有重写父类的setBeanFactory
,这个方法是不是有点熟悉呢,它是BeanFactoryAware
接口中的方法,AsyncAnnotationBeanPostProcessor
的父类实现了这个接口,在我们很久之前分析过的Bean的初始化中,是有提到过这个接口的,实现了Aware
类型接口的Bean,会在初始化Bean的时候调用相应的初始化方法,具体可以查看AbstractAutowireCapableBeanFactory
#initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd)
方法。
@Override
public void setBeanFactory(BeanFactory beanFactory) {
super.setBeanFactory(beanFactory);
//创建一个Advisor
AsyncAnnotationAdvisor advisor = new AsyncAnnotationAdvisor(this.executor, this.exceptionHandler);
//如果有指定的注解,加入指定的注解
if (this.asyncAnnotationType != null) {
advisor.setAsyncAnnotationType(this.asyncAnnotationType);
}
//将当前的BeanFacotry注入到Advisor中
advisor.setBeanFactory(beanFactory);
this.advisor = advisor;
}
@SuppressWarnings("unchecked")
public AsyncAnnotationAdvisor(Executor executor, AsyncUncaughtExceptionHandler exceptionHandler) {
Set<Class<? extends Annotation>> asyncAnnotationTypes = new LinkedHashSet<Class<? extends Annotation>>(2);
//asyncAnnotationTypes添加默认的注解
asyncAnnotationTypes.add(Async.class);
try {
asyncAnnotationTypes.add((Class<? extends Annotation>)
ClassUtils.forName("javax.ejb.Asynchronous", AsyncAnnotationAdvisor.class.getClassLoader()));
}
catch (ClassNotFoundException ex) {
// If EJB 3.1 API not present, simply ignore.
}
if (exceptionHandler != null) {
this.exceptionHandler = exceptionHandler;
}
//当没有配置exceptionHandler时,使用默认的异常处理器
else {
this.exceptionHandler = new SimpleAsyncUncaughtExceptionHandler();
}
//创建一个advice,术语叫做通知,增强处理,实际上是一个MethodInterceptor,注入线程池和异常处理器属性 AnnotationAsyncExecutionInterceptor
this.advice = buildAdvice(executor, this.exceptionHandler);
//创建一个切点
this.pointcut = buildPointcut(asyncAnnotationTypes);
}
处理Bean的postProcessAfterInitialization
方法在祖先类AbstractAdvisingBeanPostProcessor
中。
从源码中可以看到。AsyncAnnotationBeanPostProcessor
是对Bean进行后置处理的BeanPostProcessor
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
if (bean instanceof AopInfrastructureBean) {
// Ignore AOP infrastructure such as scoped proxies.
return bean;
}
if (bean instanceof Advised) {
Advised advised = (Advised) bean;
if (!advised.isFrozen() && isEligible(AopUtils.getTargetClass(bean))) {
// Add our local Advisor to the existing proxy's Advisor chain...
if (this.beforeExistingAdvisors) {
advised.addAdvisor(0, this.advisor);
}
else {
advised.addAdvisor(this.advisor);
}
return bean;
}
}
//是否符合处理条件
if (isEligible(bean, beanName)) {
//如果符合条件
//创建ProxyFactory
ProxyFactory proxyFactory = prepareProxyFactory(bean, beanName);
if (!proxyFactory.isProxyTargetClass()) {
evaluateProxyInterfaces(bean.getClass(), proxyFactory);
}
proxyFactory.addAdvisor(this.advisor);
customizeProxyFactory(proxyFactory);
//创建代理类,默认使用JDK代理,创建代理类的过程之前在讲SPRING AOP中已经详细介绍过了
return proxyFactory.getProxy(getProxyClassLoader());
}
// No async proxy needed.
return bean;
}
JdkDynamicAopProxy
的invoke方法
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
MethodInvocation invocation;
Object oldProxy = null;
boolean setProxyContext = false;
TargetSource targetSource = this.advised.targetSource;
Class<?> targetClass = null;
Object target = null;
try {
//省略代码
Object retVal;
if (this.advised.exposeProxy) {
// Make invocation available if necessary.
oldProxy = AopContext.setCurrentProxy(proxy);
setProxyContext = true;
}
// May be null. Get as late as possible to minimize the time we "own" the target,
// in case it comes from a pool.
target = targetSource.getTarget();
if (target != null) {
targetClass = target.getClass();
}
// Get the interception chain for this method.
//获取当前的Advicechain 通知链
List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
// Check whether we have any advice. If we don't, we can fallback on direct
// reflective invocation of the target, and avoid creating a MethodInvocation.
if (chain.isEmpty()) {
// We can skip creating a MethodInvocation: just invoke the target directly
// Note that the final invoker must be an InvokerInterceptor so we know it does
// nothing but a reflective operation on the target, and no hot swapping or fancy proxying.
Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
retVal = AopUtils.invokeJoinpointUsingReflection(target, method, argsToUse);
}
else {
// We need to create a method invocation...
//当前的AdviceChain就包含了当时setBeanFactory方法生成的通知
invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
// Proceed to the joinpoint through the interceptor chain.
retVal = invocation.proceed();
}
// Massage return value if necessary.
//省略代码
}
return retVal;
}
finally {
if (target != null && !targetSource.isStatic()) {
// Must have come from TargetSource.
targetSource.releaseTarget(target);
}
if (setProxyContext) {
// Restore old proxy.
AopContext.setCurrentProxy(oldProxy);
}
}
}
上述方法生成的通知链会包含setBeanFactory()
方法生成的通知,执行链会用于创建ReflectiveMethodInvocation
对象,最终是调用ReflectiveMethodInvocation
#proceed()
来完成对方法的增强处理。
@Override
public Object proceed() throws Throwable {
// We start with an index of -1 and increment early.
if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
return invokeJoinpoint();
}
Object interceptorOrInterceptionAdvice =
this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
// Evaluate dynamic method matcher here: static part will already have
// been evaluated and found to match.
InterceptorAndDynamicMethodMatcher dm =
(InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice;
if (dm.methodMatcher.matches(this.method, this.targetClass, this.arguments)) {
return dm.interceptor.invoke(this);
}
else {
// Dynamic matching failed.
// Skip this interceptor and invoke the next in the chain.
return proceed();
}
}
else {
// It's an interceptor, so we just invoke it: The pointcut will have
// been evaluated statically before this object was constructed.
//执行通知的invoke方法。
return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
}
}
proceed()
方法在这里会执行最后一个分支
具体是执行AsyncExecutionInterceptor
#invoke()
@Override
public Object invoke(final MethodInvocation invocation) throws Throwable {
Class<?> targetClass = (invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null);
Method specificMethod = ClassUtils.getMostSpecificMethod(invocation.getMethod(), targetClass);
final Method userDeclaredMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
//决定到底使用哪个线程池
AsyncTaskExecutor executor = determineAsyncExecutor(userDeclaredMethod);
if (executor == null) {
throw new IllegalStateException(
"No executor specified and no default executor set on AsyncExecutionInterceptor either");
}
//构造任务
Callable<Object> task = new Callable<Object>() {
@Override
public Object call() throws Exception {
try {
Object result = invocation.proceed();
if (result instanceof Future) {
return ((Future<?>) result).get();
}
}
catch (ExecutionException ex) {
handleError(ex.getCause(), userDeclaredMethod, invocation.getArguments());
}
catch (Throwable ex) {
handleError(ex, userDeclaredMethod, invocation.getArguments());
}
return null;
}
};
//将任务放到线程池中
return doSubmit(task, executor, invocation.getMethod().getReturnType());
}
虽然上文中说了,Spring环境中只能有一个AsyncConfigurer
实现类,但是不意味着,在Spring环境中只能配置一个线程池,在Spring环境中是可以配置多个线程池,而且我们可以在使用@Async
注解进行异步操作的时候,通过在value属性上指定线程池BeanName,这样就可以指定相应的线程池来作为任务的载体。
determineAsyncExecutor
protected AsyncTaskExecutor determineAsyncExecutor(Method method) {
AsyncTaskExecutor executor = this.executors.get(method);
if (executor == null) {
Executor targetExecutor;
//获取@Async注解上的value值
String qualifier = getExecutorQualifier(method);
if (StringUtils.hasLength(qualifier)) {
//如果@Async有指定线程池。则通过BeanName找到相应的线程池
targetExecutor = findQualifiedExecutor(this.beanFactory, qualifier);
}
//否则使用默认的线程池
else {
targetExecutor = this.defaultExecutor;
if (targetExecutor == null) {
synchronized (this.executors) {
if (this.defaultExecutor == null) {
this.defaultExecutor = getDefaultExecutor(this.beanFactory);
}
targetExecutor = this.defaultExecutor;
}
}
}
if (targetExecutor == null) {
return null;
}
executor = (targetExecutor instanceof AsyncListenableTaskExecutor ?
(AsyncListenableTaskExecutor) targetExecutor : new TaskExecutorAdapter(targetExecutor));
this.executors.put(method, executor);
}
return executor;
}
总结
当我们想要在SpringBoot中方便的使用@Async
注解开启异步操作的时候,只需要实现AsyncConfigurer
接口(这样就配置了默认线程池配置,当然该类需要在Spring环境中,因为是默认的,所以只能有一个,没有多个实现类排优先级的说法),实现对线程池的配置,并在启动类上加上@EnableAsync
注解,即可使得@Async
注解生效。
我们甚至可以不显式的实现AsyncConfigurer
,我们可以在Spring环境中配置多个Executor
类型的Bean,在使用@Async
注解时,将注解的value指定为你Executor
类型的BeanName,就可以使用指定的线程池来作为任务的载体,这样就使用线程池也更加灵活。