1.本文内容
详解@EnableAsync & @Async,主要分下面几个点进行介绍
- 作用
- 用法
- 获取异步执行结果
- 自定义异常处理
- 线程隔离
- 源码
2.作用
spring容器中实现bean方法的异步调用。
比如有个logService的bean,logservice中有个log方法用来记录日志,当调用logService.log(msg)的时候,希望异步执行,那么可以通过@EnableAsync & @Async来实现。
3.用法
- 需要异步执行的方法上面使用@Async注解标注,若bean中所有的方法都需要异步执行,可以直接将@Async加载类上
- 将@EnableAsync添加在spring配置类上,此时@Async注解才会起效
常见2中用法:
- 无返回值的
- 可以获取返回值的
4.无返回值
案例
来一个记录日志的
@Component
public class LogService {
//标注,这是一个异步方法
@Async
public void log(String msg) throws InterruptedException {
System.out.println(Thread.currentThread() + "开始记录日志," + System.currentTimeMillis());
//模拟耗时2秒
TimeUnit.SECONDS.sleep(2);
System.out.println(Thread.currentThread() + "日志记录完毕," + System.currentTimeMillis());
}
}
来个配置类:
@ComponentScan
@Configuration
@EnableAsync
public class MainConfig {
//添加一个线程池,如果没有,虽然不影响运行结果,但是过程会有报错:没有这个taskExecutor bean
@Bean
public Executor taskExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
//默认是1个线程,设置一下
taskExecutor.setCorePoolSize(10);
return taskExecutor;
}
}
测试:
@Test
public void asyncTest() throws InterruptedException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
LogService logService = context.getBean("logService", LogService.class);
System.out.println(Thread.currentThread() + " logService.log start," + System.currentTimeMillis());
logService.log("异步执行方法!");
System.out.println(Thread.currentThread() + " logService.log end," + System.currentTimeMillis());
//休眠一下,防止@Test退出
TimeUnit.SECONDS.sleep(3);
}
运行结果:
Thread[main,5,main] logService.log start,1603072253843
Thread[main,5,main] logService.log end,1603072253853
Thread[taskExecutor-1,5,main]开始记录日志,1603072253865
Thread[taskExecutor-1,5,main]日志记录完毕,1603072255872
5.获取异步返回值
用法
若需要获取异步执行结果,方法返回值必须是Future
类型,使用Spring提供的静态方法org.springframework.scheduling.annotation.AsyncResult#forValue
创建返回值:
public Future<String> getGoodsInfo(long goodsId) throws InterruptedException {
return AsyncResult.forValue(String.format("商品%s基本信息!", goodsId));
}
案例
场景:电商中商品详情页通常会有很多信息:商品基本信息、商品描述信息、商品评论信息,通过3个方法来或者这几个信息。
这3个方法之间无关联,所以可以采用异步的方式并行获取,提升效率。
商品信息的获取:
@Async
@Component
public class GoodService {
//模拟获取商品基本信息,内部耗时500毫秒
public Future<String> getGoodsInfo(long goodsId) throws InterruptedException {
TimeUnit.MILLISECONDS.sleep(500);
return AsyncResult.forValue(String.format("商品%s基本信息!", goodsId));
}
//模拟获取商品描述信息,内部耗时500毫秒
public Future<String> getGoodsDesc(long goodsId) throws InterruptedException {
TimeUnit.MILLISECONDS.sleep(500);
return AsyncResult.forValue(String.format("商品%s描述信息!", goodsId));
}
//模拟获取商品评论信息列表,内部耗时500毫秒
public Future<List<String>> getGoodsComments(long goodsId) throws InterruptedException {
TimeUnit.MILLISECONDS.sleep(500);
List<String> comments = Arrays.asList("评论1", "评论2");
return AsyncResult.forValue(comments);
}
}
配置类:
@ComponentScan
@Configuration
@EnableAsync
public class MainConfig1 {
@Bean
public Executor taskExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(10);
return taskExecutor;
}
}
测试:
@Test
public void asyncTest1() throws InterruptedException, ExecutionException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig1.class);
GoodService goodsService = context.getBean(GoodService.class);
long starTime = System.currentTimeMillis();
System.out.println("开始获取商品的各种信息");
long goodsId = 1L;
Future<String> goodsInfoFuture = goodsService.getGoodsInfo(goodsId);
Future<String> goodsDescFuture = goodsService.getGoodsDesc(goodsId);
Future<List<String>> goodsCommentsFuture = goodsService.getGoodsComments(goodsId);
System.out.println(goodsInfoFuture.get());
System.out.println(goodsDescFuture.get());
System.out.println(goodsCommentsFuture.get());
System.out.println("商品信息获取完毕,总耗时(ms):" + (System.currentTimeMillis() - starTime));
//休眠一下,防止@Test退出
TimeUnit.SECONDS.sleep(3);
}
运行结果:
开始获取商品的各种信息
商品1基本信息!
商品1描述信息!
[评论1, 评论2]
商品信息获取完毕,总耗时(ms):522
方法之间无关联的可以采用异步的方式,并行去获取,最终耗时为最长的那个方法,整体相对于同步的方式性能提升不少。
6.自定义异常处理
异步方法若发生了异常,我们如何获取异常信息呢?此时可以通过自定义异常处理来解决。
异常处理分2中情况
- 当返回值是Future的时候,方法内部有异常的时候,异常会向外抛出,可以对Future.get采用try…catch来捕获异常
- 当返回值不是Future的时候,可以自定义一个bean,实现AsyncConfigurer接口中的getAsyncUncaughtExceptionHandler方法,返回自定义的异常处理器
返回值为Future类型
try {
Future<String> future = logService.mockException();
System.out.println(future.get());
} catch (ExecutionException e) {
System.out.println("捕获 ExecutionException 异常");
//通过e.getCause获取实际的异常信息
e.getCause().printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
无返回值的异常
当返回值不是Future的时候,可以自定义一个bean,实现AsyncConfigurer接口中的getAsyncUncaughtExceptionHandler方法,返回自定义的异常处理器,当目标方法执行过程中抛出异常的时候,此时会自动回调AsyncUncaughtExceptionHandler#handleUncaughtException这个方法,可以在这个方法中处理异常,如下:
@Component
public class CustomerAsyncConfigurer implements AsyncConfigurer {
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new AsyncUncaughtExceptionHandler() {
@Override
public void handleUncaughtException(Throwable ex, Method method, Object... params) {
//定义自定义异常
System.out.println("异常通知。。。");
}
};
}
}
7.线程池隔离
什么是线程池隔离
一个系统中可能有很多业务,比如充值服务、提现服务或者其他服务,这些服务中都有一些方法需要异步执行,默认情况下他们会使用同一个线程池去执行,如果有一个业务量比较大,占用了线程池中的大量线程,此时会导致其他业务的方法无法执行,那么我们可以采用线程隔离的方式,对不同的业务使用不同的线程池,相互隔离,互不影响。
@Async
注解有个value
参数,用来指定线程池的bean名称,方法运行的时候,就会采用指定的线程池来执行目标方法。
使用步骤
- 在spring容器中,自定义线程池相关的bean
- @Async(“线程池bean名称”)
案例
模拟用户账户操作
@Component
public class CashOutService {
//模拟异步提现
@Async(MainConfig.CASHOUT_EXECUTORS_BEAN_NAME)
public void cashOut() {
System.out.println(Thread.currentThread() + "模拟异步提现");
}
}
@Component
public class RechargeService {
//模拟异步充值
@Async(MainConfig.RECHARGE_EXECUTORS_BEAN_NAME)
public void recharge() {
System.out.println(Thread.currentThread() + "模拟异步充值");
}
}
配置类:
@ComponentScan
@Configuration
@EnableAsync
public class MainConfig {
//充值值业务线程池bean名称
public static final String RECHARGE_EXECUTORS_BEAN_NAME = "rechargeExecutors";
//提现业务线程池bean名称
public static final String CASHOUT_EXECUTORS_BEAN_NAME = "cashOutExecutors";
@Bean(RECHARGE_EXECUTORS_BEAN_NAME)
public Executor taskExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(10);
taskExecutor.setThreadNamePrefix(RECHARGE_EXECUTORS_BEAN_NAME + "-");
return taskExecutor;
}
@Bean(CASHOUT_EXECUTORS_BEAN_NAME)
public Executor taskExecutor1() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(10);
taskExecutor.setThreadNamePrefix(CASHOUT_EXECUTORS_BEAN_NAME + "-");
return taskExecutor;
}
}
测试:
@Test
public void asyncTest2() throws InterruptedException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(com.spring.async2.MainConfig.class);
RechargeService rechargeService = context.getBean(RechargeService.class);
rechargeService.recharge();
CashOutService cashOutService = context.getBean(CashOutService.class);
cashOutService.cashOut();
//休眠一下,防止@Test退出
TimeUnit.SECONDS.sleep(3);
}
运行结果:
Thread[cashOutExecutors-1,5,main]模拟异步提现
Thread[rechargeExecutors-1,5,main]模拟异步充值
8.源码
内部使用aop实现的,@EnableAsync
会引入一个bean后置处理器:AsyncAnnotationBeanPostProcessor
,将其注册到spring容器,这个bean后置处理器在所有bean创建过程中,判断bean的类上是否有@Async
注解或者类中是否有@Async
标注的方法,如果有,会通过aop给这个bean生成代理对象,会在代理对象中添加一个切面:org.springframework.scheduling.annotation.AsyncAnnotationAdvisor
,这个切面中会引入一个拦截器:AnnotationAsyncExecutionInterceptor
,方法异步调用的关键代码就是在这个拦截器的invoke方法中实现的