From 1ea829e00300d6124d26405d2951ecca78877d46 Mon Sep 17 00:00:00 2001 From: Grigory Fadeev Date: Sat, 19 Nov 2016 19:42:16 +0200 Subject: [PATCH 1/2] Fix connector used to configure connection timeout Fix Tomcat customization so that the main connection is configured with any timeout. Closes gh-7425 --- .../autoconfigure/web/ServerProperties.java | 20 ++++++++++++------- .../web/ServerPropertiesTests.java | 8 ++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index ae4a553491..4579f32d04 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -809,14 +809,20 @@ public class ServerProperties } private void customizeConnectionTimeout( - TomcatEmbeddedServletContainerFactory factory, int connectionTimeout) { - for (Connector connector : factory.getAdditionalTomcatConnectors()) { - if (connector.getProtocolHandler() instanceof AbstractProtocol) { - AbstractProtocol handler = (AbstractProtocol) connector - .getProtocolHandler(); - handler.setConnectionTimeout(connectionTimeout); + TomcatEmbeddedServletContainerFactory factory, + final int connectionTimeout) { + factory.addConnectorCustomizers(new TomcatConnectorCustomizer() { + + @Override + public void customize(Connector connector) { + ProtocolHandler handler = connector.getProtocolHandler(); + if (handler instanceof AbstractProtocol) { + AbstractProtocol protocol = (AbstractProtocol) handler; + protocol.setConnectionTimeout(connectionTimeout); + } } - } + + }); } private void customizeRemoteIpValve(ServerProperties properties, diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java index 7a76eaffea..e581c42e7a 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java @@ -111,6 +111,14 @@ public class ServerPropertiesTests { assertThat(this.properties.getServerHeader()).isEqualTo("Custom Server"); } + @Test + public void testConnectionTimeout() throws Exception { + Map map = new HashMap(); + map.put("server.connection-timeout", "60000"); + bindProperties(map); + assertThat(this.properties.getConnectionTimeout()).isEqualTo(60000); + } + @Test public void testServletPathAsMapping() throws Exception { RelaxedDataBinder binder = new RelaxedDataBinder(this.properties, "server"); From ce58e16860f476c75119d88b571769221e7a41fb Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 22 Nov 2016 14:30:36 -0800 Subject: [PATCH 2/2] Add additional Tomcat timeout test Update the Tomcat sample to also test that the connection timeout is set. See gh-7425 --- .../src/main/resources/application.properties | 1 + .../tomcat/SampleTomcatApplicationTests.java | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/spring-boot-samples/spring-boot-sample-tomcat/src/main/resources/application.properties b/spring-boot-samples/spring-boot-sample-tomcat/src/main/resources/application.properties index 09ab26becc..4c0dadec6d 100644 --- a/spring-boot-samples/spring-boot-sample-tomcat/src/main/resources/application.properties +++ b/spring-boot-samples/spring-boot-sample-tomcat/src/main/resources/application.properties @@ -1,2 +1,3 @@ server.compression.enabled: true server.compression.min-response-size: 1 +server.connection-timeout=5000 diff --git a/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/SampleTomcatApplicationTests.java b/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/SampleTomcatApplicationTests.java index bc397cff32..3383a91b36 100644 --- a/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/SampleTomcatApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/SampleTomcatApplicationTests.java @@ -20,13 +20,18 @@ import java.io.ByteArrayInputStream; import java.nio.charset.Charset; import java.util.zip.GZIPInputStream; +import org.apache.coyote.AbstractProtocol; +import org.apache.coyote.ProtocolHandler; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext; +import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.context.ApplicationContext; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; @@ -52,6 +57,9 @@ public class SampleTomcatApplicationTests { @Autowired private TestRestTemplate restTemplate; + @Autowired + private ApplicationContext applicationContext; + @Test public void testHome() throws Exception { ResponseEntity entity = this.restTemplate.getForEntity("/", String.class); @@ -78,4 +86,15 @@ public class SampleTomcatApplicationTests { } } + @Test + public void testTimeout() throws Exception { + EmbeddedWebApplicationContext context = (EmbeddedWebApplicationContext) this.applicationContext; + TomcatEmbeddedServletContainer embeddedServletContainer = (TomcatEmbeddedServletContainer) context + .getEmbeddedServletContainer(); + ProtocolHandler protocolHandler = embeddedServletContainer.getTomcat() + .getConnector().getProtocolHandler(); + int timeout = ((AbstractProtocol) protocolHandler).getConnectionTimeout(); + assertThat(timeout).isEqualTo(5000); + } + }