SpringBoot——》异步执行:@EnableAsync、@Async
发布日期:2021-09-14 23:18:26
浏览次数:5
分类:技术文章
本文共 3023 字,大约阅读时间需要 10 分钟。
版权声明:本文为博主原创文章,无需授权即可转载,甚至无需保留以上版权声明,转载时请务必注明作者。
https://blog.csdn.net/weixin_43453386/article/details/106209402同步调用 :整个处理过程按顺序执行,每一步必须等到上一步执行完后才能执行
异步调用 :只是发送了调用的指令,调用者无需等待被调用的方法完全执行完毕,继续执行下面的流程。
一、开启异步:@EnableAsync
@EnableAsync@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
二、实现异步:@Async
1、Service代码示例
import org.springframework.scheduling.annotation.Async;import org.springframework.scheduling.annotation.AsyncResult;import org.springframework.stereotype.Service;import java.util.concurrent.Future;/** ** 异步 服务类 *
* * @author xiaoxian * @since 2020-05-19 */@Servicepublic class AsyncService { /** * 无返回值 */ @Async public void asyncWithNoReturn() { System.out.println("开始执行asyncWithNoReturn..."); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("结束执行asyncWithNoReturn..."); } /** * 有返回值 * @return */ @Async public FutureasyncWithReturn() { System.out.println("开始执行asyncWithReturn..."); Future future = new AsyncResult<>("success"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); future = new AsyncResult ("error"); } System.out.println("开始执行asyncWithReturn..."); return future; }}
2、Controller代码示例
import com.eju.fangshop.core.service.AsyncService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import java.util.concurrent.ExecutionException;import java.util.concurrent.Future;/** ** 异步 前端控制器 *
* * @author lz * @since 2020-05-14 */@RestControllerpublic class AsyncController{ @Autowired AsyncService asyncService; /** * 调用无返回值方法 * @return */ @GetMapping("/asyncWithNoReturn") public String asyncWithNoReturn(){ asyncService.asyncWithNoReturn(); return "success"; } /** * 调用有返回值方法 * get()方法的作用是获取执行结果,此方法会产生阻塞,等到任务执行完毕后才能获得执行结果 * @return */ @GetMapping("/asyncWithReturn") public String asyncWithReturn(){ String result = null; Futurefuture = asyncService.asyncWithReturn(); try { result = future.get(); } catch (InterruptedException e) { e.printStackTrace(); result = "error"; } catch (ExecutionException e) { e.printStackTrace(); result = "error"; } return result; }}
三、测试异步
启动项目,打开postman,进行测试
1、测试无返回值
- url:http://127.0.0.1:8902/asyncWithNoReturn
- status:200
- time:17ms
如下图所示,发送请求后,立即返回结果。
2、测试有返回值
- url:http://127.0.0.1:8902/asyncWithReturn
- status:200
- time:3.02s
如下图所示,由于我们调用了get()方法,发送请求后,会在3000毫秒后,才会返回结果。
转载地址:https://blog.csdn.net/weixin_43453386/article/details/106209402 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2024年04月08日 16时25分23秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Math - 高斯分布(正态分布)
2021-06-30
android学习笔记----简易音乐播放器原理
2021-06-30
Unity编辑器扩展——标签属性Attribute
2021-06-30
Unity中实现拖拽操作
2021-06-30
Unity中的UGUI事件系统
2021-06-30
C#中的常量
2021-06-30
C#中的静态变量与非静态变量
2021-06-30
C#中的ref、out、params关键字
2021-06-30
C#中的多态性
2021-06-30
C#中的命名空间
2021-06-30
设计模式——状态模式
2021-06-30
设计模式——工厂模式
2021-06-30
Unity中实现有限状态机FSM
2021-06-30
Unity中实现反弹
2021-06-30
U3D游戏开发框架(九)——事件序列
2021-06-30
Unity中解决“SetDestination“ can only be called on an active agent that has been placed on a NavMesh
2021-06-30
Unity中的刚体
2021-06-30
Unity中的坐标转换
2021-06-30
Unity中为什么不能对transform.position.x直接赋值?
2021-06-30