springboot 异步任务

不羁的心 提交于 2020-01-16 14:36:14


拓展阅读: http://www.jianshu.com/p/86e915d616c4

发表于  | Spring框架 | SpringBoot

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

Spring Boot 集成异步任务

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

@Configuration@EnableAsyncpublic class AsyncConfig { }

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

单发服务模式

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

@Servicepublic class MsgServer { @Async public void sendA() throws Exception { System.out.println("send A"); Long startTime = System.currentTimeMillis(); Thread.sleep(2000); Long endTime = System.currentTimeMillis(); System.out.println("耗时:" + (endTime - startTime)); }  @Async public void sendB() throws Exception { System.out.println("send B"); Long startTime = System.currentTimeMillis(); Thread.sleep(2000); Long endTime = System.currentTimeMillis(); System.out.println("耗时:" + (endTime - startTime)); }}

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

@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = WebMain.class)public class AsyncTest {  @Autowired private MsgServer msgServer;  @Test public void test() throws Exception {        msgServer.sendA();        msgServer.sendB(); }}

请求应答模式

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

@Servicepublic class MsgFutureServer { public static Random random = new Random();  @Async public Future<String> sendA() throws Exception { System.out.println("send A"); Long startTime = System.currentTimeMillis(); Thread.sleep(2000); Long endTime = System.currentTimeMillis(); System.out.println("耗时:" + (endTime - startTime)); return new AsyncResult<String>("success"); }  @Async public Future<String> sendB() throws Exception { System.out.println("send B"); Long startTime = System.currentTimeMillis(); Thread.sleep(2000); Long endTime = System.currentTimeMillis(); System.out.println("耗时:" + (endTime - startTime)); return new AsyncResult<String>("success"); }}

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

@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = WebMain.class)public class AsyncFutureTest {  @Autowired private MsgFutureServer msgFutureServer;  @Test public void test() throws Exception { long startTime = System.currentTimeMillis();  Future<String> task1 = msgFutureServer.sendA(); Future<String> task2 = msgFutureServer.sendB();  while(true) { if(task1.isDone() && task2.isDone() ) { break; } }  long endTime = System.currentTimeMillis(); System.out.println("总耗时:" + (endTime - startTime)); }}

源代码

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!