springboot 异步注解源码解析

springboot中的async注解表示异步执行,将其标注的方法交给线程池进行处理。
如果要在项目里使用async注解,需要在首先加上EnableAsync注解。
当然我们也可以使用“javax.ejb.Asynchronous”注解或者自定义注解实现异步,如果是自定义注解,需要在EnableAsync的annotation属性处指定。一旦指定了EnableAsync的annotation属性,则async和“javax.ejb.Asynchronous”注解将无效。
对于EnableAsync注解,通过@import引入了AsyncConfigurationSelector类。

public class AsyncConfigurationSelector extends AdviceModeImportSelector<EnableAsync> {

	private static final String ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME =
			"org.springframework.scheduling.aspectj.AspectJAsyncConfiguration";

	/**
	 * {@inheritDoc}
	 * @return {@link ProxyAsyncConfiguration} or {@code AspectJAsyncConfiguration} for
	 * {@code PROXY} and {@code ASPECTJ} values of {@link EnableAsync#mode()}, respectively
	 */
	@Override
	@Nullable
	public String[] selectImports(AdviceMode adviceMode) {
		switch (adviceMode) {
			case PROXY:
				return new String[] { ProxyAsyncConfiguration.class.getName() };
			case ASPECTJ:
				return new String[] { ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME };
			default:
				return null;
		}
	}
}

springboot默认的adviceMode是proxy,将配置类ProxyAsyncConfiguration注入IOC容器。
在ProxyAsyncConfiguration分类AbstractAsyncConfiguration我们通过了与其它springboot中注解类似的方式实现类可配置化。

@Autowired(required = false)
void setConfigurers(Collection<AsyncConfigurer> configurers) {
	if (CollectionUtils.isEmpty(configurers)) {
		return;
	}
	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();
}

我们可以自定义一个实现AsyncConfigurer接口的类,在其中指定executor和exceptionHandler,并将其注入IOC容器来实现默认线程池和异常处理的定制化。
回到ProxyAsyncConfiguration类,这是一个configuration类,在其中通过@bean注入了AsyncAnnotationBeanPostProcessor 类。

@Bean(name = TaskManagementConfigUtils.ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public AsyncAnnotationBeanPostProcessor asyncAdvisor() {
	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(customAs
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值