Callable / Runnable Controller methods: what's the point?

In Spring, you can let the controllers return Callable instead of T, which will immediately free the request processing thread and calculate the result in the MvcAsync thread controlled by WebAsyncManager. You just need to wrap the contents of the controller method in return () -> {... return result; };. Very easy!

But what is the point? What is the difference between a) has 500 request processing requests and allows them to do all the work and b) has only a few request processing threads and performs all requests in Callables with concurrencyLimit 500 ?

The second option b) actually looks worse for me, since there is overhead for managing the magic of MvcAsync.

I get how you can collect @Async methods to execute two methods simultaneously and return the result after completion, but I obviously did not understand the Callable controller methods.

+4
source share
1 answer

Suppose you have a Tomcat server that has 10 threads that listen on client requests. If you have a client that invokes an endpoint that needs to be answered for 5 seconds, this client holds this thread for those 5 seconds. Add multiple concurrent clients, and within these 5 seconds you will end the flow soon.

, 5 -, , , , .

, Spring Callable, CompletableFuture ListenableFuture - , .

, -, . , . IO ( NIO) API.

, Servlet API Servlet Async IO, Spring , , . , :

/ Servlet Async - NIO.

, , , - -. , , - . -, , - , , , , - . , Callable, CompletableFuture ListenableFuture - , Spring , .

, , , NIO, , , , ( Tomcat), , .

, , Spring Project Reactor , API, .

, Netty, RxJava, . .

, Spring, . , Vert.x Ratpack, , , Node.js .

+3

Source: https://habr.com/ru/post/1668973/


All Articles