In a Web application, if the HTTP request from the client is processed for a long time, the HTTP response will time out and it will be difficult to check whether the HTTP request was accepted normally. Even if processing that takes a long time is performed in the background, if the number of concurrent executions of asynchronous tasks is not limited, the number of threads may increase and the resources of the operating environment may be exhausted, resulting in inoperability.
Consider a method to asynchronously call a process that takes a long time as shown in the figure below in a Tomcat application that runs on a Tomcat container.
** Figure 1 Asynchronous processing flow **
In Figure 1, there are three things in the Tomcat container: Controller, Service, and Entity. When an HTTP request comes from the client, the Service is called asynchronously from the Controller via the Tomcat container. An HTTP response can be returned to the client immediately after the asynchronous call, so the client can be notified whether the HTTP request has been accepted or rejected. If you reflect the execution status of the asynchronous task in Entity, you can check the execution status by issuing another HTTP request later.
As mentioned at the beginning, implementing asynchronous calls requires control over the flow of asynchronous tasks, but what you need to know here is that it is not recommended that your application spawn "appropriately" threads. This is because the Tomcat container and Java EE container do not recognize the threads created by the application, and the thread life cycle cannot be managed properly. Therefore, it is proposed by "Concurrency Utilities for Java EE (JSR 236)", which is a specification for managing the number of threads and life cycle. And introduced in Java EE 7.
The Tomcat container is like a subset of the Java EE container and does not support JSR 236, but the Spring Framework allows you to manage thread lifecycles. Here, we will consider how to implement flow control for non-simultaneous processing of "** single tenant support " and " multi-tenant support **" using the Spring Framework.
This article consists of three articles.
--"Implementation of asynchronous processing in Tomcat" ← This article -"Implementation of asynchronous processing for single tenant in Tomcat" -"Implementation of multi-tenant asynchronous processing in Tomcat"
The source code created for this article can be found on GitHub.
--For single tenant --If there is a one-to-one correspondence between asynchronous methods and requests, it can be handled by the Spring Framework function. --For multi-tenant --If there is a one-to-many correspondence between asynchronous methods and requests, you need to prepare your own thread pool and its life cycle.
Recommended Posts