Spring中注解方式的异步请求
程序员文章站
2022-06-23 09:04:49
一、servlet3.0异步请求@webservlet(value = "/async", asyncsupported = true)public class helloasyncservlet e...
一、servlet3.0异步请求
@webservlet(value = "/async", asyncsupported = true) public class helloasyncservlet extends httpservlet { @override protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { //1、设置支持异步处理asyncsupported = true //2、开启异步模式 system.out.println("主线程开始:" + thread.currentthread() + "==>" + system.currenttimemillis()); asynccontext startasync = req.startasync(); //3、业务逻辑进行异步处理,开始异步处理 startasync.start(new runnable() { @override public void run() { try { system.out.println("副线程开始:" + thread.currentthread() + "==>" + system.currenttimemillis()); sayhello(); //获取到异步的上下文 asynccontext asynccontext = req.getasynccontext(); startasync.complete(); servletresponse response = asynccontext.getresponse(); response.getwriter().write("hello async!"); system.out.println("副线程结束:" + thread.currentthread() + "==>" + system.currenttimemillis()); } catch (exception e) { e.printstacktrace(); } } }); system.out.println("主线程结束:" + thread.currentthread() + "==>" + system.currenttimemillis()); } public void sayhello() throws exception { system.out.println(thread.currentthread() + "processing..."); thread.sleep(3000); } }
打印结果:
二、springmvc的异步请求
返回callable
@controller public class asynccontroller { @responsebody @requestmapping("/async01") public callable<string> async01(){ system.out.println("主线程开始:" + thread.currentthread() + "==>" + system.currenttimemillis()); callable<string> callable = new callable<string>() { public string call() throws exception { system.out.println("副线程开始:" + thread.currentthread() + "==>" + system.currenttimemillis()); thread.sleep(2000); system.out.println("副线程开始:" + thread.currentthread() + "==>" + system.currenttimemillis()); return "async01"; } }; system.out.println("主线程结束:" + thread.currentthread() + "==>" + system.currenttimemillis()); return callable; } }
- 控制器返回
callable
- spring进行异步处理,将
callable
提交给taskexecutor
,使用一个隔离的线程进行执行 -
dispatcherservlet
和所有的filter
退出web容器的线程,但是response
保持打开状态 -
callable
返回结果,spreingmvc将请求重新派发给容器,恢复之前的处理,callable
返回值就是目标方法的返回值 - 根据
callable
返回的结果,springmvc继续进行视图渲染流程等(从收到请求到视图渲染)
输出结果:
返回deferredresult
模拟一个消息中间件
public class deferredresultqueue { private static queue<deferredresult<object>> queue = new concurrentlinkeddeque<deferredresult<object>>(); public static void save(deferredresult<object> deferredresult){ queue.add(deferredresult); } public static deferredresult<object> get(){ return queue.poll(); } }
/createorder
请求会暂时保存deferredresultqueue
中,/create
请求会获取deferredresultqueue
中的请求,进行业务逻辑的处理并返回结果
@controller public class asynccontroller { @responsebody @requestmapping("/createorder") public deferredresult<object> createorder(){ deferredresult<object> deferredresult = new deferredresult<object>((long)3000,"create fail"); deferredresultqueue.save(deferredresult); return deferredresult; } @responsebody @requestmapping("/create") public string create(){ string order = uuid.randomuuid().tostring(); deferredresult<object> deferredresult = deferredresultqueue.get(); deferredresult.setresult(order); return "success:" + order; } }
到此这篇关于spring中注解方式的异步请求的文章就介绍到这了,更多相关注解方式的异步请求内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: java如何用递归方法求阶乘
推荐阅读
-
Spring在代码中获取bean的几种方式详解
-
php中curl和soap方式请求服务超时问题的解决
-
Android中实现异步任务机制的AsyncTask方式的使用讲解
-
微信小程序授权 获取用户的openid和session_key【后端使用java语言编写】,我写的是get方式,目的是测试能否获取到微信服务器中的数据,后期我会写上post请求方式。
-
vue中promise的使用及异步请求数据的方法
-
Spring框架中 @Autowired 和 @Resource 注解的区别
-
js中ajax请求的两种方式介绍
-
Spring中与Spring相关的注解
-
spring boot拦截器中获取request post请求中的参数
-
Spring的IOC控制反转实现注解方式配置