简体   繁体   中英

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

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. 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. Spring`s response handlers will wait for async task to get executed to write the response back.

Please go through AsyncHandlerMethodReturnValueHandler which handles Asynchronous response returned from the controller.

if you return callable then it will be handled by 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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