EN
Spring Boot 2.4 + Tomcat 9 is serving web page response content extremely slow for first API requests
1 answers
5 points
After migration from Spring Framework + Tomcat 8 to Spring Boot 2.4 + Tomcat 9, I have noticed big extreme slowness in receiving web content. Initial JSP compilation takes a lot of time, first request to REST API too.
After the Spring Boot Application start, for each:
- first JSP file loading time is very long,
- first request to the database with Hibernate is very long.
All application is packed into WAR file.
1 answer
7 points
I had the same issue.
Tested solutions:
- The first solution was to write simple logic to force pre-compilation for each JSP file - it solved only JSP views-based problems. It was required to run it always after Tomcat started (the solution is here).
- Later, I have got the idea to check if Tomcat does not extract WAR package each time when some initialization is needed - the point to get that conclusion was the problem didn't occur with the exploded package / extracted package - it solved JSP and Hibernate slow request problem.
The configuration that solved the problem is (TomcatConfig.java
file):
xxxxxxxxxx
1
package app.config;
2
3
import org.apache.catalina.Context;
4
import org.apache.catalina.webresources.ExtractingRoot;
5
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
6
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
7
import org.springframework.context.annotation.Bean;
8
import org.springframework.context.annotation.Configuration;
9
10
11
public class TomcatConfig {
12
13
14
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> servletContainerCustomizer() {
15
return (TomcatServletWebServerFactory container) -> {
16
container.addContextCustomizers((Context context) -> {
17
// This configuration is used to improve initialization performance.
18
19
context.setResources(new ExtractingRoot());
20
context.setReloadable(false);
21
});
22
};
23
}
24
}
References
0 commentsShow commentsAdd comment