简体   繁体   中英

How to set Tomcat's “reloadable” flag in a Spring Boot application?

I am developing a web application in Spring Boot, and deploy it on a Tomcat 8.5 running on Windows. The issue I am facing is that Tomcat periodically produces very high CPU load due to Catalina checking if the app needs reloading. According to the Tomcat 8 documentation , there is a reloadable flag that can be set to false for an application to disable this behaviour. Here's what the documentation says (emphasis is mine):

Set to true if you want Catalina to monitor classes in /WEB-INF/classes/ and /WEB-INF/lib for changes, and automatically reload the web application if a change is detected. This feature is very useful during application development, but it requires significant runtime overhead and is not recommended for use on deployed production applications. You can use the Manager web application, however, to trigger reloads of deployed applications on demand.

NOTE - The value for this property will be inherited from the reloadable attribute you set on the surrounding Context component, and any value you explicitly set here will be replaced.

The question is: how do I set this particular reloadable flag to false in Spring Boot? Preferably without resorting to a manually created META-INF/context.xml , I am using annotations for configuration.

You should be able to achieve this by creating an EmbeddedServletContainerCustomizer

@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
    return new EmbeddedServletContainerCustomizer() {
        @Override
        public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
            if (configurableEmbeddedServletContainer instanceof TomcatEmbeddedServletContainerFactory) {
                ((TomcatEmbeddedServletContainerFactory) configurableEmbeddedServletContainer).addContextCustomizers(
                        new TomcatContextCustomizer() {
                            @Override
                            public void customize(Context context) {
                                context.setReloadable(false);
                            }
                        }
                );
            }
        }
    };
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM