I have a Spring Boot app with a @RestController
and I'm using Undertow as the embedded server. Now, as far as I understand, Undertow's using two thread pools: A pool of IO threads handling the incoming requests and a pool of worker threads to which blocking tasks are delegated. As already answered here, Spring controller methods are called on one of the worker threads. However, the logic in the controller method might take some time to complete. So I was thinking about using Spring's support for asynchronous request processing by returning a Callable
from the controller method. For example:
@RestController
public class MyController {
@RequestMapping(path = "/myPath", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public Callable<MyResult> handle() {
// This part is handled by the Undertow worker thread
doSomething();
return () -> {
// This part is handed off to a thread managed by Spring
return doSomeLongerProcessing();
};
}
}
My question now is: Would you really use an additional thread pool for handling the longer running task? Or is it best practice to rather let everything run in the Undertow worker thread? As far as I understand that's exactly what the worker pool is for, right?