Implementation of asynchronous processing in Tomcat

Asynchronous processing in tomcat

Introduction

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.

Article structure

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"

Sample source

The source code created for this article can be found on GitHub.

Summary

--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

Implementation of asynchronous processing in Tomcat
Implementation of multi-tenant asynchronous processing in Tomcat
Implementation of asynchronous processing for single tenant in Tomcat
Implementation of gzip in java
Implementation of tri-tree in Java
Implementation of HashMap in kotlin
[Rails] Asynchronous implementation of like function
Try implementing asynchronous processing in Azure
Implementation of ls command in Ruby
Order of processing in the program
Implementation of like function in Java
Implementation of DBlayer in Java (RDB, MySQL)
Basic basis of Android asynchronous processing "AsyncTask"
About UI thread processing in Android asynchronous
How to implement asynchronous processing in Outsystems
Implementation of GKAccessPoint
Asynchronous processing with regular execution in Spring Boot
Implementation of digit grouping in flea market apps
SKStoreReviewController implementation memo in Swift UI of iOS14
[Rails] Implementation of retweet function in SNS application
[Rails] Implementation of "notify notification in some way"
[Java / Spring Boot] Spring security ⑤ --Implementation of logout processing
[Rails] Implementation of batch processing using whenever (gem)
Interpreter implementation in Java
Format of the log output by Tomcat itself in Tomcat 8
Implementation of flash messages
I checked asynchronous execution of queries in Spring Boot 1.5.9
Prepare the execution environment of Tomcat in IntelliJ Community
Access the war file in the root directory of Tomcat
About the problem of deadlock in parallel processing in gem'sprockets' 4.0
Implementation example of simple LISP processing system (Java version)
Boyer-Moore implementation in Java
Asynchronous processing and Web API integration in Android Studio
Addition of variables in iterative processing in a while statement
Applied implementation of chat-space
Heapsort implementation (in java)
Use MouseListener in Processing
Implementation of pagination function
Implementation example of simple LISP processing system (Ruby version)
Output in multiples of 3
About merge processing implementation including sorting function of Stream API
Rails sorting function implementation (displayed in order of number of like)
Rails implementation of ajax removal
[Swift] Simple implementation of UIImageView
Cleansing processing of Japanese sentences
Implementation of sequential search function
[Swift] Implementation of ultra-simple billing
Implementation of like function (Ajax)
[Rails 6] Implementation of search function
[Swift] About asynchronous processing "Operation"
Implementation of image preview function
[Swift] What is asynchronous processing?
[Java] Implementation of Faistel Network
Implementation of XLPagerTabStrip with TabBarController
Asynchronous processing with Shoryuken + SQS
Overall architecture of Tomcat (Catalina)
Implementation of category pull-down function
[Java] Timer processing implementation method
Implementation of unit test code
Measured parallel processing in Java
Mazume judgment processing in fishing