小编给大家分享一下spring中注解方式的异步请求如何实现,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!一、Servlet3.0异步请求@WEBServlet(value = "/async&
小编给大家分享一下spring中注解方式的异步请求如何实现,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
@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); }}
打印结果:
返回Callable
@Controllerpublic 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
中的请求,进行业务逻辑的处理并返回结果
@Controllerpublic 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中注解方式的异步请求如何实现”有了一定的了解,如果想了解更多相关知识,欢迎关注编程网精选频道,感谢各位的阅读!
--结束END--
本文标题: Spring中注解方式的异步请求如何实现
本文链接: https://lsjlt.com/news/279114.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0