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):
package app.config;
import org.apache.catalina.Context;
import org.apache.catalina.webresources.ExtractingRoot;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TomcatConfig {
@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> servletContainerCustomizer() {
return (TomcatServletWebServerFactory container) -> {
container.addContextCustomizers((Context context) -> {
// This configuration is used to improve initialization performance.
context.setResources(new ExtractingRoot());
context.setReloadable(false);
});
};
}
}
References
0 comments
Add comment