Springmvc asynchronous request

Springmvc asynchronous request

1, Servlet specification

Why asynchronous requests are required? Any technology creation will have some business scene requirements. Suppose we have a business scenario that, after a user input operation, business logic will take a lot of time to deal with, and if the approach is synchronous, the servlet thread is blocked until all processing is complete. If the server request gets a lot of process, it will reach the maximum servlet thread limit and further requests will reject the connection error. The servlet thread should just as HTTP protocol Porter, from acceptance to the user request, the data sent to a sub thread to handle, immediately returned to continue to accept other request, will be returned by the thread in response to the current request. The following two timing diagrams correspond to two ways of synchronization and asynchrony.

The asynchronous specification is provided within the servlet3.0 specification.

Springmvc asynchronous request
synchronization mode
Springmvc asynchronous request
asynchronous mode

2, springmvc asynchronous request

After SpringMvc3.2 supports asynchronous requests, a Callable or DeferredResult can be returned in controller, which improves the throughput of the system.

  • When the Callable process is roughly Spring will start a thread to Callable to TaskExecutor to DispatcherServlet, and then exit the main thread, while the Response remains open until Callable processing is complete, SpringMvc will re allocate a Request request, DispatcherServlet re call and Callable processing results returned value, and then return to the view.

Code example

@GetMapping public Callable< String> hello (throws Exception) {Callable< = new; String> HelloWorld Callable< String> public) {@Override (String (call) throws Exception {return "Hello World!"; return HelloWorld;}}};
//java8 can do this; @GetMapping, public, Callable< String> hello (), throws, Exception, {return ((), -> Hello, World);}
  • When the DeferredResult implementation process has a little different, when Callable is a thread by spring to start to carry out the Callble, while the DeferredResult thread of execution by our control (Programmer), only need to go to the set.

Code example

The definition of the implementation of service class / / TestService{public logic String (exec) {try {Thread.sleep} (1000L); catch (InterruptedException E) {e.printStackTrace}); LoggerFactory.getLogger ((TestService.class).Error ("TestService"); return HelloWorld;}} / / @GetMapping public controller DeferredResult< String> World (throws Exception{) logger.error ("DeferredResult"); DeferredResult< String> deferredResult = new DeferredResult< > (); / / asynchronous event driven programming model CompletableFuture.supplyAsync (New): TestService (exec).WhenCompleteAsync ((result, throwable) -> deferredResult.setResult (result)); return deferredResult;}

In general, both Callable and XOR DeferredResult extend the throughput of the system by releasing the container thread and executing logic through another secondary thread.

Expand a little information about reading Java multithreading