From 34eb3695e4ec1393e82f6995b4dfac3f9e79ce9a Mon Sep 17 00:00:00 2001 From: Quinten De Swaef Date: Mon, 4 Apr 2016 22:49:38 +0200 Subject: [PATCH 1/2] Allow Tomcat's minimum threads to be configured via the environment Closes gh-5572 --- .../autoconfigure/web/ServerProperties.java | 32 +++++++++++++++++++ .../web/ServerPropertiesTests.java | 8 +++++ .../appendix-application-properties.adoc | 1 + 3 files changed, 41 insertions(+) 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 80d61fcb9a5..849bfb695af 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 @@ -580,6 +580,11 @@ public class ServerProperties */ private int maxThreads = 0; // Number of threads in protocol handler + /** + * Minimum amount of worker threads. + */ + private int minSpareThreads = 0; //Number of minimum spare threads in protocol handler + /** * Maximum size in bytes of the HTTP message header. */ @@ -598,6 +603,14 @@ public class ServerProperties this.maxThreads = maxThreads; } + public int getMinSpareThreads() { + return minSpareThreads; + } + + public void setMinSpareThreads(int minSpareThreads) { + this.minSpareThreads = minSpareThreads; + } + public int getMaxHttpHeaderSize() { return this.maxHttpHeaderSize; } @@ -684,6 +697,9 @@ public class ServerProperties if (this.maxThreads > 0) { customizeMaxThreads(factory); } + if (this.minSpareThreads > 0) { + customizeMinThreads(factory); + } if (this.maxHttpHeaderSize > 0) { customizeMaxHttpHeaderSize(factory); } @@ -747,6 +763,22 @@ public class ServerProperties }); } + @SuppressWarnings("rawtypes") + private void customizeMinThreads(TomcatEmbeddedServletContainerFactory factory) { + factory.addConnectorCustomizers(new TomcatConnectorCustomizer() { + @Override + public void customize(Connector connector) { + + ProtocolHandler handler = connector.getProtocolHandler(); + if (handler instanceof AbstractProtocol) { + AbstractProtocol protocol = (AbstractProtocol) handler; + protocol.setMinSpareThreads(Tomcat.this.minSpareThreads); + } + + } + }); + } + @SuppressWarnings("rawtypes") private void customizeMaxHttpHeaderSize( TomcatEmbeddedServletContainerFactory factory) { 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 9a0a97bc917..71ea172c6a0 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 @@ -258,6 +258,14 @@ public class ServerPropertiesTests { assertThat(this.properties.getTomcat().getMaxHttpHeaderSize()).isEqualTo(9999); } + @Test + public void testCustomizeTomcatMinSpareThreads() throws Exception { + Map map = new HashMap(); + map.put("server.tomcat.min-spare-threads", "10"); + bindProperties(map); + assertThat(this.properties.getTomcat().getMinSpareThreads()).isEqualTo(10); + } + @Test public void customizeTomcatDisplayName() throws Exception { Map map = new HashMap(); diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index afad598c8b0..029523d2ffc 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -204,6 +204,7 @@ content into your application; rather pick only the properties that you need. 172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3} # regular expression matching trusted IP addresses. server.tomcat.max-http-header-size=0 # Maximum size in bytes of the HTTP message header. server.tomcat.max-threads=0 # Maximum amount of worker threads. + server.tomcat.min-spare-threads=0 # Maximum amount of worker threads. server.tomcat.port-header=X-Forwarded-Port # Name of the HTTP header used to override the original port value. server.tomcat.protocol-header= # Header that holds the incoming protocol, usually named "X-Forwarded-Proto". server.tomcat.protocol-header-https-value=https # Value of the protocol header that indicates that the incoming request uses SSL. From 487f7310fdd2e77fb6908b475d26a478e762ff4b Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 5 Apr 2016 11:00:44 +0100 Subject: [PATCH 2/2] Polish contribution --- .../boot/autoconfigure/web/ServerProperties.java | 5 +++-- .../boot/autoconfigure/web/ServerPropertiesTests.java | 1 + .../src/main/asciidoc/appendix-application-properties.adoc | 2 +- 3 files changed, 5 insertions(+), 3 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 849bfb695af..aeaad83b98a 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 @@ -72,6 +72,7 @@ import org.springframework.util.StringUtils; * @author Ivan Sopov * @author Marcos Barbero * @author Eddú Meléndez + * @author Quinten De Swaef */ @ConfigurationProperties(prefix = "server", ignoreUnknownFields = true) public class ServerProperties @@ -583,7 +584,7 @@ public class ServerProperties /** * Minimum amount of worker threads. */ - private int minSpareThreads = 0; //Number of minimum spare threads in protocol handler + private int minSpareThreads = 0; // Minimum spare threads in protocol handler /** * Maximum size in bytes of the HTTP message header. @@ -604,7 +605,7 @@ public class ServerProperties } public int getMinSpareThreads() { - return minSpareThreads; + return this.minSpareThreads; } public void setMinSpareThreads(int minSpareThreads) { 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 71ea172c6a0..a72fcc6b1a3 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 @@ -62,6 +62,7 @@ import static org.mockito.Mockito.verify; * @author Andy Wilkinson * @author Phillip Webb * @author Eddú Meléndez + * @author Quinten De Swaef */ public class ServerPropertiesTests { diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 029523d2ffc..57a96cde136 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -204,7 +204,7 @@ content into your application; rather pick only the properties that you need. 172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3} # regular expression matching trusted IP addresses. server.tomcat.max-http-header-size=0 # Maximum size in bytes of the HTTP message header. server.tomcat.max-threads=0 # Maximum amount of worker threads. - server.tomcat.min-spare-threads=0 # Maximum amount of worker threads. + server.tomcat.min-spare-threads=0 # Minimum amount of worker threads. server.tomcat.port-header=X-Forwarded-Port # Name of the HTTP header used to override the original port value. server.tomcat.protocol-header= # Header that holds the incoming protocol, usually named "X-Forwarded-Proto". server.tomcat.protocol-header-https-value=https # Value of the protocol header that indicates that the incoming request uses SSL.