原文地址:http://blog.jboost.cn/springboot-async.html

在业务开发中,有时候会遇到一些非核心的附加功能,比如短信或微信模板消息通知,或者一些耗时比较久,但主流程不需要立即获得其结果反馈的操作,比如保存图片、同步数据到其它合作方等等。如果将这些操作都置于主流程中同步处理,势必会对核心流程的性能造成影响,甚至由于第三方服务的问题导致自身服务不可用。这时候就应该将这些操作异步化,以提高主流程的性能,并与第三方解耦,提高主流程的可用性。

在Spring Boot中,或者说在Spring中,我们实现异步处理一般有以下几种方式:

1. 通过 @EnableAsync 与 @Asyc 注解结合实现
2. 通过异步事件实现
3. 通过消息队列实现

1. 基于注解实现

我们以前在Spring中提供异步支持一般是在配置文件 applicationContext.xml 中添加类似如下配置

<task:annotation-driven executor="executor" />
<task:executor id="executor" pool-size="10-200" queue-capacity="2000"/>

Spring的 @EnableAsync 注解的功能与<task:annotation-driven/>类似,将其添加于一个 @Configuration 配置类上,可对Spring应用的上下文开启异步方法支持。 @Async 注解可以标注在方法或类上,表示某个方法或某个类里的所有方法需要通过异步方式来调用。

我们以一个demo来示例具体用法,demo地址:https://github.com/ronwxy/springboot-demos/tree/master/springboot-async

1. 添加 @EnableAsync 注解

在一个 @Configuration 配置类上添加 @EnableAysnc 注解,我们一般可以添加到启动类上,如

@SpringBootApplication
@EnableAsync
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

2. 配置相关的异步执行线程池 

@Configuration
public class AsyncConfig implements AsyncConfigurer { @Value("${async.corePoolSize:10}")
private int corePoolSize; @Value("${async.maxPoolSize:200}")
private int maxPoolSize; @Value("${async.queueCapacity:2000}")
private int queueCapacity; @Value("${async.keepAlive:5}")
private int keepAlive; public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.setKeepAliveSeconds(keepAlive);
executor.setThreadNamePrefix("async-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.setDaemon(false); //以用户线程模式运行
executor.initialize();
return executor;
} public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new MyAsyncUncaughtExceptionHandler();
} public static class MyAsyncUncaughtExceptionHandler implements AsyncUncaughtExceptionHandler { public void handleUncaughtException(Throwable throwable, Method method, Object... objects) {
System.out.println("catch exception when invoke " + method.getName());
throwable.printStackTrace();
}
}
}

可通过配置类的方式对异步线程池进行配置,并提供异步执行时出现异常的处理方法,如

这里我们通过实现 AsyncConfigurer 接口提供了一个异步执行线程池对象,各参数的说明可以参考【线程池的基本原理,看完就懂了】,里面有很详细的介绍。且通过实现 AsyncUncaughtExceptionHandler 接口提供了一个异步执行过程中未捕获异常的处理类。

3. 定义异步方法

异步方法的定义只需要在类(类上注解表示该类的所有方法都异步执行)或方法上添加 @Async 注解即可,如

@Service
public class AsyncService { @Async
public void asyncMethod(){
System.out.println("2. running in thread: " + Thread.currentThread().getName());
} @Async
public void asyncMethodWithException() {
throw new RuntimeException("exception in async method");
}
}

4. 测试 

我们可以通过如下测试类来对异步方法进行测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class AnnotationBasedAsyncTest { @Autowired
private AsyncService asyncService; @Test
public void testAsync() throws InterruptedException {
System.out.println("1. running in thread: " + Thread.currentThread().getName());
asyncService.asyncMethod(); Thread.sleep(3);
} @Test
public void testAysncWithException() throws InterruptedException {
System.out.println("1. running in thread: " + Thread.currentThread().getName());
asyncService.asyncMethodWithException(); Thread.sleep(3);
}
}

因为异步方法在一个新的线程中执行,可能在主线程执行完后还没来得及处理,所以通过sleep来等待它执行完成。具体执行结果读者可自行尝试运行,这里就不贴图了。

2. 基于事件实现

第二种方式是通过Spring框架的事件监听机制实现,但Spring的事件监听默认是同步执行的,所以实际上还是需要借助 @EnableAsync 与 @Async 来实现异步。

1. 添加 @EnableAsync 注解

与上同,可添加到启动类上。

2. 自定义事件类
通过继承 ApplicationEvent 来自定义一个事件

public class MyEvent extends ApplicationEvent {

    private String arg;

    public MyEvent(Object source, String arg) {
super(source);
this.arg = arg;
} //getter/setter
}

3. 定义事件处理类
支持两种形式,一是通过实现 ApplicationListener 接口,如下

@Component
@Async
public class MyEventHandler implements ApplicationListener<MyEvent> { public void onApplicationEvent(MyEvent event) {
System.out.println("2. running in thread: " + Thread.currentThread().getName());
System.out.println("2. arg value: " + event.getArg());
}
}

二是通过 @EventListener 注解,如下

@Component
public class MyEventHandler2 { @EventListener
@Async
public void handle(MyEvent event){
System.out.println("3. running in thread: " + Thread.currentThread().getName());
System.out.println("3. arg value: " + event.getArg());
}
}

注意两者都需要添加 @Async 注解,否则默认是同步方式执行。

4. 定义事件发送类
可以通过实现 ApplicationEventPublisherAware 接口来使用 ApplicationEventPublisher 的 publishEvent()方法发送事件,

@Component
public class MyEventPublisher implements ApplicationEventPublisherAware { protected ApplicationEventPublisher applicationEventPublisher; public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
} public void publishEvent(ApplicationEvent event){
this.applicationEventPublisher.publishEvent(event);
}
}

5. 测试

可以通过如下测试类来进行测试,

@RunWith(SpringRunner.class)
@SpringBootTest
public class EventBasedAsyncTest { @Autowired
private MyEventPublisher myEventPublisher; @Test
public void testAsync() throws InterruptedException {
System.out.println("1. running in thread: " + Thread.currentThread().getName());
myEventPublisher.publishEvent(new MyEvent(this,"testing event based async"));
Thread.sleep(3);
}
}

运行后发现两个事件处理类都执行了,因为两者都监听了同一个事件 MyEvent 。

3. 基于消息队列实现

以上两种方式都是基于服务器本机运行,如果服务进程出现异常退出,可能导致异步执行中断。如果需要保证任务执行的可靠性,可以借助消息队列的持久化与重试机制。阿里云上的消息队列服务提供了几种类型的消息支持,如顺序消息、定时/延时消息、事务消息等(详情可参考:https://help.aliyun.com/document_detail/29532.html?spm=5176.234368.1278132.btn4.6f43db25Rn8oey ),如果项目是基于阿里云部署的,可以考虑使用其中一类消息服务来实现业务需求。

4. 总结

本文对spring boot下异步处理的几种方法进行了介绍,如果对任务执行的可靠性要求不高,则推荐使用第一种方式,如果可靠性要求较高,则推荐使用自建消息队列或云消息队列服务的方式。
本文demo源码地址:https://github.com/ronwxy/springboot-demos/tree/master/springboot-async/src/main/java/cn/jboost/async

我的个人博客地址:http://blog.jboost.cn
我的微信公众号:jboost-ksxy (一个不只有技术干货的公众号,欢迎关注,及时获取更新内容)
———————————————————————————————————————————————————————————————

Spring Boot从入门到实战(十):异步处理的更多相关文章

  1. Spring Boot 从入门到实战汇总

    之前写过几篇spring boot入门到实战的博文,因为某些原因没能继续. 框架更新迭代很快,之前还是基于1.x,现在2.x都出来很久了.还是希望能从基于该框架项目开发的整体有一个比较系统的梳理,于是 ...

  2. Spring Boot从入门到实战:整合Web项目常用功能

    在Web应用开发过程中,一般都涵盖一些常用功能的实现,如数据库访问.异常处理.消息队列.缓存服务.OSS服务,以及接口日志配置,接口文档生成等.如果每个项目都来一套,则既费力又难以维护.可以通过Spr ...

  3. Spring Boot从入门到实战:整合通用Mapper简化单表操作

    数据库访问是web应用必不可少的部分.现今最常用的数据库ORM框架有Hibernate与Mybatis,Hibernate貌似在传统IT企业用的较多,而Mybatis则在互联网企业应用较多.通用Map ...

  4. Spring Boot从入门到实战:集成AOPLog来记录接口访问日志

    日志是一个Web项目中必不可少的部分,借助它我们可以做许多事情,比如问题排查.访问统计.监控告警等.一般通过引入slf4j的一些实现框架来做日志功能,如log4j,logback,log4j2,其性能 ...

  5. Spring Boot 快速入门

    Spring Boot 快速入门 http://blog.csdn.net/xiaoyu411502/article/details/47864969 今天给大家介绍一下Spring Boot MVC ...

  6. Spring Boot快速入门(二):http请求

    原文地址:https://lierabbit.cn/articles/4 一.准备 postman:一个接口测试工具 创建一个新工程 选择web 不会的请看Spring Boot快速入门(一):Hel ...

  7. Spring Boot -01- 快速入门篇(图文教程)

    Spring Boot -01- 快速入门篇(图文教程) 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到[慕课网]手机 app ...

  8. spring boot入门教程——Spring Boot快速入门指南

    Spring Boot已成为当今最流行的微服务开发框架,本文是如何使用Spring Boot快速开始Web微服务开发的指南,我们将使创建一个可运行的包含内嵌Web容器(默认使用的是Tomcat)的可运 ...

  9. Spring Boot干货系列:(十二)Spring Boot使用单元测试(转)

    前言这次来介绍下Spring Boot中对单元测试的整合使用,本篇会通过以下4点来介绍,基本满足日常需求 Service层单元测试 Controller层单元测试 新断言assertThat使用 单元 ...

随机推荐

  1. DateTimeToGreenUnix

    @暗夜魔尊 { Unix date conversion support with time-zone detect } function DateTimeToGreenUnix(const AVal ...

  2. Delphi中用MessageBox()API函数做倒计时对话框(使用Hook安装CBTHookCallback,计时器更改文字,SetWindowText API真正修改文字,引用未知函数)good

    API有隐藏的MessageBoxTimeOut函数可以做计时对话框,缺点是不能显示还剩下多少秒关闭. const IDTIMEDOUT = 32000; function MessageBoxTim ...

  3. python中的while循环,格式化输出,运算符,编码

    一.while循环 1.1语法 while 条件: 代码块(循环体) else: 当上面的条件为假的的时候,才会执行. 执行顺序:先判断条件是否为真,如果是真的,执行循环体,再次判断条件,直到条件不成 ...

  4. 爬取虎扑NBA首页主干道推荐贴的一只小爬虫,日常爬不冷笑话解闷

    虎扑是广大jrs的家园,步行街是这个家园里最繁华的地段.据称广大jrs平均学历985,步行街街薪30w起步. 大学时经舍友安利,开始了解虎扑,主要是看看NBA的一些资讯. 偶尔也上上这个破街,看看jr ...

  5. Linux实战型企业运维工程师试题

    1.如何通过Linux配置一个局域网或者IDC机房上网网关,请给出步骤及命令?答:上网网关配置(1)开启内核转发:sed -i 's#net.ipv4.ip_forward = 0#net.ipv4. ...

  6. JVM 参数类型

    标准参数 -help -server -client -version -showversion -cp -classpath X参数 非标准化参数(在各个JDK版本中可能会变,但是变动比较小) -X ...

  7. EhCache注解 (转载)

    其实EhCache使用的就是Spring Cache的注解. 1.1 @Cacheable @Cacheable可以标记在一个方法上,也可以标记在一个类上.当标记在一个方法上时表示该方法是支持缓存的, ...

  8. Google浏览器插件之闪存过滤器

    一件很有意思的事情引发的无聊尝试. 博客园有个很有趣的功能,就是闪存,翻阅到07年园长对闪存的定义:      记录一闪而过的想法,高兴或者不高兴都可以发一下.我用这个一直以来的想法就是,想到点啥发点 ...

  9. 小范笔记:ASP.NET Core API 基础知识与Axios前端提交数据

    跟同事合作前后端分离项目,自己对 WebApi 的很多知识不够全,虽说不必要学全栈,可是也要了解基础知识,才能合理设计接口.API,方便与前端交接. 晚上回到宿舍后,对 WebApi 的知识查漏补缺, ...

  10. 安装Ruby、多版本Ruby共存、Ruby安装慢问题

    rbenv rbenv可以管理多个版本的ruby.可以分为3种范围(或者说不同生效作用域)的版本: local版:本地,针对各项目范围 global版:全局,没有shell和local版时使用glob ...