Spring Boot 揭秘与实战(七) 实用技术篇 - 异步任务
拓展阅读: http://www.jianshu.com/p/86e915d616c4

发表于 2017-01-06 | Spring框架 | SpringBoot

Spring 对异步任务具有很好的支持。这篇文章,我们透过 Spring Boot 来讲解下单发服务模式和请求应答模式。

Spring Boot 集成异步任务

在 Spring Boot 中使用 @EnableAsync 开启异步支持。

  1. @Configuration
  2. @EnableAsync
  3. public class AsyncConfig {
  4. }

现在,我们在 Spring Boot 中,我们只需要通过使用 @Async 注解就能将原来的同步方法变为异步方法。

单发服务模式

多个服务之间逻辑上不存在相互依赖关系,执行先后顺序没有严格的要求,逻辑上可以被并行执行。对于单发服务只有请求,没有应答,很容易设计成异步的。发起服务调用后。立即返回,不需要同步阻塞等待应答。

  1. @Service
  2. public class MsgServer {
  3. @Async
  4. public void sendA() throws Exception {
  5. System.out.println("send A");
  6. Long startTime = System.currentTimeMillis();
  7. Thread.sleep(2000);
  8. Long endTime = System.currentTimeMillis();
  9. System.out.println("耗时:" + (endTime - startTime));
  10. }
  11. @Async
  12. public void sendB() throws Exception {
  13. System.out.println("send B");
  14. Long startTime = System.currentTimeMillis();
  15. Thread.sleep(2000);
  16. Long endTime = System.currentTimeMillis();
  17. System.out.println("耗时:" + (endTime - startTime));
  18. }
  19. }

此时,因为我们添加了 @Async 注解,它就变成了异步方法。

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringApplicationConfiguration(classes = WebMain.class)
  3. public class AsyncTest {
  4. @Autowired
  5. private MsgServer msgServer;
  6. @Test
  7. public void test() throws Exception {
  8. msgServer.sendA();
  9. msgServer.sendB();
  10. }
  11. }

请求应答模式

对于,请求的内容,需要应答,例如我们需要在多个方法调用都完成后,才进行接下来的操作,此时我们可以利用 Java 的 Future-Listener 机制来实现异步服务调用。

  1. @Service
  2. public class MsgFutureServer {
  3. public static Random random = new Random();
  4. @Async
  5. public Future<String> sendA() throws Exception {
  6. System.out.println("send A");
  7. Long startTime = System.currentTimeMillis();
  8. Thread.sleep(2000);
  9. Long endTime = System.currentTimeMillis();
  10. System.out.println("耗时:" + (endTime - startTime));
  11. return new AsyncResult<String>("success");
  12. }
  13. @Async
  14. public Future<String> sendB() throws Exception {
  15. System.out.println("send B");
  16. Long startTime = System.currentTimeMillis();
  17. Thread.sleep(2000);
  18. Long endTime = System.currentTimeMillis();
  19. System.out.println("耗时:" + (endTime - startTime));
  20. return new AsyncResult<String>("success");
  21. }
  22. }

下面的单元测试,在等待完成两个异步任务后,再统计具体耗时时长。

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringApplicationConfiguration(classes = WebMain.class)
  3. public class AsyncFutureTest {
  4. @Autowired
  5. private MsgFutureServer msgFutureServer;
  6. @Test
  7. public void test() throws Exception {
  8. long startTime = System.currentTimeMillis();
  9. Future<String> task1 = msgFutureServer.sendA();
  10. Future<String> task2 = msgFutureServer.sendB();
  11. while(true) {
  12. if(task1.isDone() && task2.isDone() ) {
  13. break;
  14. }
  15. }
  16. long endTime = System.currentTimeMillis();
  17. System.out.println("总耗时:" + (endTime - startTime));
  18. }
  19. }

源代码

相关示例完整代码: springboot-action

最新文章

  1. SQLServer2005如何批量修改架构名 - wuxiaokaixinguo的专栏
  2. 在EF4.1的DBContext中实现事务处理(BeginTransaction)和直接执行SQL语句的示例
  3. Cordova调用Activity
  4. Fedora下载地址
  5. VS2005工程迁移到Eclipse CDT
  6. c-参数(argument)
  7. ZOJ 3430 Detect the Virus 【AC自动机+解码】
  8. 如何让div横向排列
  9. 商派shopex
  10. 删除cookie固定格式
  11. premake Ubuntu下的安装
  12. ios多线程开发总结
  13. 微信小程序 canvas导出图片模糊
  14. 项目案例【Net Core】如何注入多个服务实现类
  15. Linux大学实验
  16. shell脚本学习系列之一---入门
  17. java获取视频缩略图
  18. 64位Ubuntu下配置CP-ABE环境
  19. autohotkey快捷键
  20. cnblogs用户体验及建议

热门文章

  1. printf回到上一行开头以及回到本行开头的方法
  2. squid中实现https的透明代理
  3. EntityFramework:我想我需要和 Session.Save 语义一样的方法
  4. 6)Linux程序设计入门--消息管理
  5. Pydoc 本地 HTML 形式查看
  6. 图片变换【Matrix】矩阵 简介
  7. js 获取url的get传值函数
  8. 解决 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile)
  9. 【算法】Logistic regression (逻辑回归) 概述
  10. URL转发