From the course: Complete Guide to Spring MVC

Request and response processing - Spring Tutorial

From the course: Complete Guide to Spring MVC

Request and response processing

- [Instructor] Request and response processing. Spring MVC provides a flexible and efficient mechanism for processing HTTP requests and responses. With its asynchronous capabilities, such as DeferredResult and Callable, it can handle those long running tasks efficiently, improving both performance and responsiveness. The Servlet container keeps the request/response pair open, even after releasing the processing thread. So you might be wondering, you know, how is DeferredResult and Callable working? Through the power of the asynchronous request processing. We are enabling asynchronous handling of I/O-bound or long-running tasks. We're preventing blocking of threads, improving throughput and scalability. As we've learned, there are two main asynchronous request processing mechanisms, which are DeferredResult and Callable. For DeferredResult specifically, the way that it works is that the controller returns a DeferredResult to the container. The main thread is released while the task executes asynchronously in a separate thread, and once the result is ready, it is set on the DeferredResult. The container then dispatches the request back to Spring MVC for rendering the response. When do we use this? We use this for tasks with unpredictable completion times. Waiting for data from remote service would be a good example. For Callable, the way that this works is that the controller returns a Callable object. Spring MVC submits the Callable to a thread pool for execution, and the call() method executes asynchronously and returns a result. And finally, the container dispatches the request back to Spring MVC to render the response. You would use this for a predictable but long-running task. So for DeferredResult, remember it's unpredictable timing. Callable is predictable timing, such as computations or controlled external service calls. There's a seven-step process for request processing workflow. First is request arrival. A client sends an HTTP request of the server. Second is controller invocation. Spring MVC routes a request to a Controller method, and the Controller returns either a synchronous response, such as ResponseBody, or an asynchronous object, such as DeferredResult or Callable. Third, you have the asynchronous handling, so the Servlet container detects the asynchronous return value and frees the main thread. The task executes in a separate thread. It does DeferredResult, so result is set later by another thread, or Callable executes in a thread pool and immediately returns the result. Fourth is background processing. The long-running operation, such as the database access or API call, completes in the background. And five, you have result setting. The result is set on the DeferredResult or returned from the Callable. And then six, you dispatch to Servlet container. The Servlet container is notified that the task is complete. It dispatches the request back to Spring MVC. And seven, you have a response rendering. Spring MVC processes the result and renders a response to the client. Here's my checklist for asynchronous processing and what you should keep in mind. One is Servlet container configuration. Two is thread pool management. Three is error handling, and four is timeout management. First is Servlet container configuration. You want to ensure the container supports asynchronous request processing. If you want to go back to the previous video, you can see what you need to do to enable Servlet container configuration for Tomcat, Jetty and Undertow. Second is thread pool management. You want to configure thread pools for optimal performance to avoid overloading the server. Third is error handling. Use @ExceptionHandler or other mechanisms to handle errors during asynchronous processing. And finally, you have timeout management. Avoid indefinite weights by setting a timeout, and as you can see in all our examples, you want to use milliseconds. Over the past five years, there have been several refinements and updates to asynchronous request processing in Spring MVC, especially with the evolution of Spring framework and its ecosystem. These improvements focus on better scalability, flexibility, and the adoption of modern practices. While Spring MVC's traditional asynchronous features, like Callable and DeferredResult remain robust, there is a growing preference for Spring Webflux and its native support for non-blocking reactive programming points, and it's shifting towards more declarative asynchronous patterns. Developers are increasingly encouraged to adopt Mono and Flux when building new applications to fully leverage modern concurrency paradigms. However, if you are in a more legacy environment, that's where your traditional Spring MVC features would be used. For more detailed insights, I do encourage you to check out the Spring release notes, especially with the incoming of Spring 7 in September of 2025. Spring MVC's asynchronous request processing capabilities provide a mechanism to support scalable and user-responsive applications. By carefully designing your architecture and leveraging asynchronous features, you're able to optimize your thread and resource management and ensure your application is well suited to handle modern web demands.

Contents