Spring Boot 揭秘与实战(七) 实用技术篇 - 异步任务
Spring 对异步任务具有很好的支持。这篇文章,我们透过 Spring Boot 来讲解下单发服务模式和请求应答模式。
Spring Boot 集成异步任务
在 Spring Boot 中使用 @EnableAsync 开启异步支持。
- @Configuration
- @EnableAsync
- public class AsyncConfig {
- }
现在,我们在 Spring Boot 中,我们只需要通过使用 @Async 注解就能将原来的同步方法变为异步方法。
单发服务模式
多个服务之间逻辑上不存在相互依赖关系,执行先后顺序没有严格的要求,逻辑上可以被并行执行。对于单发服务只有请求,没有应答,很容易设计成异步的。发起服务调用后。立即返回,不需要同步阻塞等待应答。
- @Service
- public 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 机制来实现异步服务调用。
- @Service
- public 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
(完)

【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 百万级群聊的设计实践
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期