Search code examples
javaspringtomcatspring-boottomcat8

How do I configure this property with Spring Boot and an embedded Tomcat?


Do I configure properties like the connectionTimeout in the application.properties file or is the somewhere else to do it? I can't figure this out from Google.

Tomcat properties list

I found this Spring-Boot example, but it does not include a connectionTimeout property and when I set server.tomcat.connectionTimeout=60000 in my application.properties file I get an error.


Solution

  • Spring Boot 1.4 and later

    As of Spring Boot 1.4 you can use the property server.connection-timeout. See Spring Boot's common application properties.

    Spring Boot 1.3 and earlier

    Provide a customized EmbeddedServletContainerFactory bean:

    @Bean
    public EmbeddedServletContainerFactory servletContainerFactory() {
        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
    
        factory.addConnectorCustomizers(connector -> 
                ((AbstractProtocol) connector.getProtocolHandler()).setConnectionTimeout(10000));
    
        // configure some more properties
    
        return factory;
    }
    

    If you are not using Java 8 or don't want to use Lambda Expressions, add the TomcatConnectorCustomizer like this:

        factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                ((AbstractProtocol) connector.getProtocolHandler()).setConnectionTimeout(10000);
            }
        });
    

    The setConnectionTimeout() method expects the timeout in milliseconds (see connectionTimeout in Apache Tomcat 8 Configuration Reference).