Spring源码之Async注解

Spring源码之Async注解spring@Async注解的处理

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

@EnableAsync注解

	/** * 可自定义扫描的注解 */
	Class<? extends Annotation> annotation() default Annotation.class;

	/** * 采用代理的方式,如果设置为true,后续会使用CGLIB进行代理 */
	boolean proxyTargetClass() default false;

	/** * 使用代理的模式 */
	AdviceMode mode() default AdviceMode.PROXY;

	/** * 注解处理的顺序 */
	int order() default Ordered.LOWEST_PRECEDENCE;

ProxyAsyncConfiguration

	@Bean(name = TaskManagementConfigUtils.ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME)
	@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
	public AsyncAnnotationBeanPostProcessor asyncAdvisor() { 
   
	    //创建一个异步注解处理器
		AsyncAnnotationBeanPostProcessor bpp = new AsyncAnnotationBeanPostProcessor();
        //将从容器中获取到的 Executor 和 ExceptionHandler设置到对象中
		bpp.configure(this.executor, this.exceptionHandler);
		Class<? extends Annotation> customAsyncAnnotation = this.enableAsync.getClass("annotation");
		if (customAsyncAnnotation != AnnotationUtils.getDefaultValue(EnableAsync.class, "annotation")) { 
   
            //设置自定义的 异步注解
			bpp.setAsyncAnnotationType(customAsyncAnnotation);
		}
        //设置代理方式
		bpp.setProxyTargetClass(this.enableAsync.getBoolean("proxyTargetClass"));
        //设置加载顺序
		bpp.setOrder(this.enableAsync.<Integer>getNumber("order"));
		return bpp;
	}

	/** * 获取到注解是否开启异步代理 */
	@Override
	public void setImportMetadata(AnnotationMetadata importMetadata) { 
   
		this.enableAsync = AnnotationAttributes.fromMap(
				importMetadata.getAnnotationAttributes(EnableAsync.class.getName()));
		if (this.enableAsync == null) { 
   
			throw new IllegalArgumentException(
					"@EnableAsync is not present on importing class " + importMetadata.getClassName());
		}
	}

	/** * 从容器中找到实现了 AsyncConfigurer 类,然后获取到自定义的 线程执行器和异常处理器;默认为null */
	@Autowired
	void setConfigurers(ObjectProvider<AsyncConfigurer> configurers) { 
   
		//默认会使用第一个配置对象
		Supplier<AsyncConfigurer> configurer = SingletonSupplier.of(() -> { 
   
			List<AsyncConfigurer> candidates = configurers.stream().collect(Collectors.toList());
			if (CollectionUtils.isEmpty(candidates)) { 
   
				return null;
			}
			if (candidates.size() > 1) { 
   
				throw new IllegalStateException("Only one AsyncConfigurer may exist");
			}
			return candidates.get(0);
		});
		this.executor = adapt(configurer, AsyncConfigurer::getAsyncExecutor);
		this.exceptionHandler = adapt(configurer, AsyncConfigurer::getAsyncUncaughtExceptionHandler);
	}
	
	//这里会返回一个Supplier对象用于在 AsyncExecutionAspectSupport 当中设置默认的线程池
	private <T> Supplier<T> adapt(Supplier<AsyncConfigurer> supplier, Function<AsyncConfigurer, T> provider) { 
   
		return () -> { 
   
            //如果不配置 AsyncConfigurer 这里就会返回null
			AsyncConfigurer configurer = supplier.get();
			return (configurer != null ? provider.apply(configurer) : null);
		};
	}

AsyncAnnotationBeanPostProcessor

在这里插入图片描述

1. setBeanFactory()

实现了 BeanFactoryAware 接口,初始化 AsyncAnnotationBeanPostProcessor 时会调用内部的**setBeanFactory() **方法设置切面

在这里插入图片描述

2. postProcessAfterInitialization

方法在容器创建好之后执行,给bean对象创建一个代理对象

AsyncAnnotationAdvisor

	public AsyncAnnotationAdvisor(
			@Nullable Supplier<Executor> executor, @Nullable Supplier<AsyncUncaughtExceptionHandler> exceptionHandler) { 
   

		Set<Class<? extends Annotation>> asyncAnnotationTypes = new LinkedHashSet<>(2);
        //添加注解的类型
		asyncAnnotationTypes.add(Async.class);
		try { 
   
			asyncAnnotationTypes.add((Class<? extends Annotation>)
					ClassUtils.forName("javax.ejb.Asynchronous", AsyncAnnotationAdvisor.class.getClassLoader()));
		}
		catch (ClassNotFoundException ex) { 
   
			
		}
        //构建一个通知对象
		this.advice = buildAdvice(executor, exceptionHandler);
        //根据注解构造一个切点,切点用于对类进行匹配,匹配了之后真正执行的是 Advice 来进行
        //返回一个ComposablePointcut支持多个过滤方式进行匹配,就是将多个ClassFilter和MethodMather进行合并
		this.pointcut = buildPointcut(asyncAnnotationTypes);
	}

AnnotationAsyncExecutionInterceptor

在这里插入图片描述

	/** * 通过父类的构造器,设置一个默认的线程池的获取方式 */
	public AsyncExecutionAspectSupport(@Nullable Executor defaultExecutor, AsyncUncaughtExceptionHandler exceptionHandler) { 
   
        // defaultExecutor就是在 ProxyAsyncConfiguration 中设置的Supplier对象,如果获取不到就调用
		this.defaultExecutor = new SingletonSupplier<>(defaultExecutor, () -> getDefaultExecutor(this.beanFactory));
		this.exceptionHandler = SingletonSupplier.of(exceptionHandler);
	}

1. getExecutorQualifier()

protected String getExecutorQualifier(Method method) { 
   
    //获取到方法上面的注解,获取到vlue值,然后通过value值去确定线程池
		Async async = AnnotatedElementUtils.findMergedAnnotation(method, Async.class);
		if (async == null) { 
   
			async = AnnotatedElementUtils.findMergedAnnotation(method.getDeclaringClass(), Async.class);
		}
		return (async != null ? async.value() : null);
	}

2. invoke()

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);
    //确定异步使用的线程池 默认使用 SimpleAsyncTaskExecutor
		AsyncTaskExecutor executor = determineAsyncExecutor(userDeclaredMethod);
		if (executor == null) { 
   
			throw new IllegalStateException();
		}
		//包装成 Callable 类的任务
		Callable<Object> task = () -> { 
   
			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());
	}

3. determineAsyncExecutor()

protected AsyncTaskExecutor determineAsyncExecutor(Method method) { 
   
    //初始获取的会是null
		AsyncTaskExecutor executor = this.executors.get(method);
		if (executor == null) { 
   
			Executor targetExecutor;
            //调用复写的方法通过注解来获取到对应的value来确定容器中的线程池名称
			String qualifier = getExecutorQualifier(method);
			if (StringUtils.hasLength(qualifier)) { 
   
				targetExecutor = findQualifiedExecutor(this.beanFactory, qualifier);
			}
			else { 
   
                //调用默认的 SingletonSupplier 对象来获取到对应的默认线程池。如果设置的对象都没有获取到,就会调用 getDefaultExecutor() 方法
				targetExecutor = this.defaultExecutor.get();
			}
			if (targetExecutor == null) { 
   
				return null;
			}
			executor = (targetExecutor instanceof AsyncListenableTaskExecutor ?
					(AsyncListenableTaskExecutor) targetExecutor : new TaskExecutorAdapter(targetExecutor));
            //缓存上
			this.executors.put(method, executor);
		}
		return executor;
	}

4. getDefaultExecutor()

AsyncExecutionInterceptor复写的方法,如果都容器中都没有获取到对应的线程池,那么就返回 SimpleAsyncTaskExecutor

	protected Executor getDefaultExecutor(@Nullable BeanFactory beanFactory) { 
   
        //调用父类方法去获取线程池
		Executor defaultExecutor = super.getDefaultExecutor(beanFactory);
		return (defaultExecutor != null ? defaultExecutor : new SimpleAsyncTaskExecutor());
	}

面试题

spring中Async注解是使用线程池进行异步处理,还是使用单线程?
答:根据spring容器中配置的 ObjectProvider<AsyncConfigurer> configurers 配置对象进行配置线程池还是单线程进行异步处理;如果没有指定配置对象那么默认就会去容器中查找 TaskExecutor 类型如果抛出异常就会去找名称为 taskExecutor 类型为 Executor 的线程池;如果都没有则使用默认 SimpleAsyncTaskExecutor 单线程进行异步处理

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/157804.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 什么意思_html5文字居中代码【转载】display:inline-block兼容ie6/7的写法

    2022年4月22日
    103
  • xampp环境下drupal安装心得[通俗易懂]

    xampp环境下drupal安装心得[通俗易懂]开始学习drupal,首先在关于drupal的安装上就出现了困难,让自己郁闷了好久,所幸最后安装成功,虽然依然有一些小瑕疵:貌似没有邮件发送功能,但这些应该是之后可以调整的,装了不下30次,最后担心自己电脑问题,还在同学的电脑上试了一下,安装成功的时候自己快泪奔了%>_能够遇到的问题基本都遇到了,甚至有些问题自己遇到了网上都查不到资料,记下来备忘吧。​安

    2022年7月20日
    19
  • android缩放动画中心缩放_缩放动画

    android缩放动画中心缩放_缩放动画ScaleAnimation:缩放动画同样,创建ScaleAnimation也有两种方式XML文件+Java代码Java代码方式XML文件+Java代码效果图:文件目录:res/anim/scale.xml

    2022年10月15日
    4
  • linux删除宝塔面板_宝塔修改端口

    linux删除宝塔面板_宝塔修改端口https://www.2kss.com/45898.html

    2022年10月19日
    0
  • 了解你的敌人:跟踪僵尸网络

    了解你的敌人:跟踪僵尸网络了解你的敌人:跟踪僵尸网络 利用蜜网对僵尸主机了解更多 蜜网项目组 & 蜜网研究联盟http://www .honeynet.org最后修改日期 : 2005 年 3 月 13 日 翻译者artemis:吴智发密罐是一种用来发现攻击工具,攻击策略与攻击者攻击动机的知名技术。在本文中,我们考虑一种特殊的安全威胁:运行僵尸网络的个人与组织。僵尸网络是一个可以由攻击者远程控制的已被攻陷主机组成的网络。由

    2022年7月25日
    8
  • 什么是宿主机?_esxi宿主机

    什么是宿主机?_esxi宿主机就是主机,这个概念是相对于子机而言的,比如你安装有虚拟机的话,那么相对于虚拟机而言,你正在使用的计算机就是宿主机,虚拟机是安装在主机上的,必须在主机上才能运行,主机就是一个“宿主”。…

    2022年4月19日
    206

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号