Languages

Spring Boot 2.4 + Tomcat 9 is serving web page response content extremely slow for first API requests

5 points
Asked by:
Sylvie-Justice
490

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
Answered by:
Sylvie-Justice
490

I had the same issue.

Tested solutions:

  1. 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).
  2. 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

  1. https://dirask.com/posts/Spring-Boot-pre-compile-JSP-files-Tomcat-Server-jMme8j
0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join