Spring中注解方式的异步请求
程序员文章站
2022-03-11 14:13:06
一、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中注解方式的异步请求的文章就介绍到这了,更多相关注解方式的异步请求内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!