前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring 异步请求

Spring 异步请求

作者头像
数媒派
发布2022-12-01 15:28:34
7840
发布2022-12-01 15:28:34
举报
文章被收录于专栏:产品优化产品优化

Spring 异步请求

官方文档 Asynchronous Request Processing

Servlet3.0

  1. A ServletRequest can be put in asynchronous mode by calling request.startAsync(). The main effect of doing so is that the Servlet, as well as any Filters, can exit but the response will remain open to allow processing to complete later.
  2. The call to request.startAsync() returns AsyncContext which can be used for further control over async processing. For example it provides the method dispatch, that is similar to a forward from the Servlet API except it allows an application to resume request processing on a Servlet container thread.
  3. The ServletRequest provides access to the current DispatcherType that can be used to distinguish between processing the initial request, an async dispatch, a forward, and other dispatcher types.
代码语言:javascript
复制
//1、支持异步处理asyncSupported=true
@WebServlet(value="/async",asyncSupported=true)
public class HelloAsyncServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //2、开启异步模式
        AsyncContext startAsync = req.startAsync();

        //3、业务逻辑进行异步处理,开始异步处理
        startAsync.start(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    startAsync.complete();
                    //4、获取到异步上下文
                    AsyncContext asyncContext = req.getAsyncContext();
                    //5、获取响应
                    ServletResponse response = asyncContext.getResponse();
                    response.getWriter().write("hello async...");
                } catch (Exception e) {
                }
            }
        });
    }
}

Callable

With the above in mind, the following is the sequence of events for async request processing with a Callable:

  1. Controller returns a Callable.
  2. Spring MVC starts asynchronous processing and submits the Callable to a TaskExecutor for processing in a separate thread.
  3. The DispatcherServlet and all Filter’s exit the Servlet container thread but the response remains open.
  4. The Callable produces a result and Spring MVC dispatches the request back to the Servlet container to resume processing.
  5. The DispatcherServlet is invoked again and processing resumes with the asynchronously produced result from the Callable.
代码语言:javascript
复制
@PostMapping
public Callable<String> processUpload(final MultipartFile file) {

    return new Callable<String>() {
        public String call() throws Exception {
            // ...
            return "someView";
        }
    };

}

DeferredResult

The sequence for DeferredResult is very similar except it’s up to the application to produce the asynchronous result from any thread:

  1. Controller returns a DeferredResult and saves it in some in-memory queue or list where it can be accessed.
  2. Spring MVC starts async processing.
  3. The DispatcherServlet and all configured Filter’s exit the request processing thread but the response remains open.
  4. The application sets the DeferredResult from some thread and Spring MVC dispatches the request back to the Servlet container.
  5. The DispatcherServlet is invoked again and processing resumes with the asynchronously produced result.
代码语言:javascript
复制
@RequestMapping("/quotes")
@ResponseBody
public DeferredResult<String> quotes() {
    DeferredResult<String> deferredResult = new DeferredResult<String>();
    // Save the deferredResult somewhere..
    return deferredResult;
}

// In some other thread...
deferredResult.setResult(data);
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Spring 异步请求
    • Servlet3.0
      • Callable
        • DeferredResult
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档