简体   繁体   English

异步控制器Spring MVC中的请求流

[英]Request Flow in Asynchronous Controller Spring MVC

I was trying to understand Asynchronous Controller implementation from one of links: 我试图通过以下链接之一来了解异步控制器的实现:

http://shengwangi.blogspot.in/2015/09/asynchronous-spring-mvc-hello-world.html http://shengwangi.blogspot.in/2015/09/asynchronous-spring-mvc-hello-world.html

I was puzzled on point that Controller thread received request and exists. 我对控制器线程收到请求并存在感到困惑。 Then service method received the request for further processing. 然后服务方法收到了进一步处理的请求。

@RequestMapping("/helloAsync")
  public Callable<String> sayHelloAsync() {
    logger.info("Entering controller");

    Callable<String> asyncTask = new Callable<String>() {

      @Override
      public String call() throws Exception {
        return helloService.doSlowWork();
      }
    };

    logger.info("Leaving  controller");
    return asyncTask;
  }

Since, Controller exists it and pass the control to appropriate handler mapping/ jsp. 因为,Controller存在它,并将控制权传递给适当的处理程序映射/ jsp。 What will be seen on the browser for the user ? 用户在浏览器上会看到什么?

Browser waits for the response to process it. 浏览器等待响应来处理它。

Asynchronous process takes place only at the server end and it has nothing to do with the browser. 异步过程仅发生在服务器端,与浏览器无关。 Browser sends the request and waits for the server to write the response back. 浏览器发送请求,并等待服务器写回响应。

Since you returned Callable doesnt mean that controller exists the flow. 自从您返回Callable并不意味着控制器存在该流。 Spring`s response handlers will wait for async task to get executed to write the response back. Spring的响应处理程序将等待异步任务执行以将响应写回。

Please go through AsyncHandlerMethodReturnValueHandler which handles Asynchronous response returned from the controller. 请通过AsyncHandlerMethodReturnValueHandler来处理从控制器返回的异步响应。

if you return callable then it will be handled by CallableHandlerMethodReturnvaluehandler : 如果返回callable,它将由CallableHandlerMethodReturnvaluehandler处理:

public void handleReturnValue(Object returnValue, MethodParameter returnType,
        ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

    if (returnValue == null) {
        mavContainer.setRequestHandled(true);
        return;
    }

    Callable<?> callable = (Callable<?>) returnValue;
    WebAsyncUtils.getAsyncManager(webRequest).startCallableProcessing(callable, mavContainer);
}

I had cleared my doubt from this link: 我已经从以下链接中消除了怀疑:

https://dzone.com/articles/jax-rs-20-asynchronous-server-and-client https://dzone.com/articles/jax-rs-20-asynchronous-server-and-client

However, they used different way to accomplish the asynchronous processing but the core concept should be the same for every approach. 但是,他们使用不同的方法来完成异步处理,但是每种方法的核心概念都应该相同。

Some important part of the article: 本文的一些重要部分:

The idea behind asynchronous processing model is to separate connection accepting and request processing operations. 异步处理模型背后的思想是将连接接受和请求处理操作分开。 Technically speaking it means to allocate two different threads, one to accept the client connection and the other to handle heavy and time consuming operations. 从技术上讲,这意味着分配两个不同的线程,一个线程接受客户端连接,另一个线程处理繁重且耗时的操作。 In this model, the container dispatched a thread to accept client connection (acceptor), hand over the request to processing (worker) thread and releases the acceptor one. 在此模型中,容器调度了一个线程来接受客户端连接(接受者),将请求移交给处理(工作者)线程,然后释放一个接受者。 The result is sent back to the client by the worker thread. 结果由工作线程发送回客户端。 In this mechanism client's connection remains open. 在这种机制下,客户端的连接保持打开状态。 May not impact on performance so much, such processing model impacts on server's THROUGHPUT and SCALABILITY a lot. 可能不会对性能造成太大影响,这种处理模型对服务器的THROUGHPUTSCALABILITY的影响很大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM