Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 662
smuggledPancakes Avatar asked Jul 16 '15 18:07

smuggledPancakes


1 Answers

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

like image 195
hzpz Avatar answered Oct 15 '22 03:10

hzpz