Skip to content

Commit e7ffeb3

Browse files
committed
Use Tomcat's new setter for max queue size
Closes gh-41093 Closes gh-40957 Closes gh-40945
1 parent fb9d779 commit e7ffeb3

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@
2121
import java.util.function.ObjIntConsumer;
2222
import java.util.stream.Collectors;
2323

24-
import javax.management.ObjectName;
25-
2624
import org.apache.catalina.Lifecycle;
27-
import org.apache.catalina.core.StandardThreadExecutor;
2825
import org.apache.catalina.valves.AccessLogValve;
2926
import org.apache.catalina.valves.ErrorReportValve;
3027
import org.apache.catalina.valves.RemoteIpValve;
@@ -39,7 +36,6 @@
3936
import org.springframework.boot.autoconfigure.web.ServerProperties;
4037
import org.springframework.boot.autoconfigure.web.ServerProperties.Tomcat.Accesslog;
4138
import org.springframework.boot.autoconfigure.web.ServerProperties.Tomcat.Remoteip;
42-
import org.springframework.boot.autoconfigure.web.ServerProperties.Tomcat.Threads;
4339
import org.springframework.boot.cloud.CloudPlatform;
4440
import org.springframework.boot.context.properties.PropertyMapper;
4541
import org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory;
@@ -98,7 +94,16 @@ public void customize(ConfigurableTomcatWebServerFactory factory) {
9894
.as(Long::intValue)
9995
.to(factory::setBackgroundProcessorDelay);
10096
customizeRemoteIpValve(factory);
101-
configureExecutor(factory, properties.getThreads());
97+
ServerProperties.Tomcat.Threads threadProperties = properties.getThreads();
98+
map.from(threadProperties::getMax)
99+
.when(this::isPositive)
100+
.to((maxThreads) -> customizeMaxThreads(factory, maxThreads));
101+
map.from(threadProperties::getMinSpare)
102+
.when(this::isPositive)
103+
.to((minSpareThreads) -> customizeMinThreads(factory, minSpareThreads));
104+
map.from(threadProperties::getMaxQueueCapacity)
105+
.when(this::isPositive)
106+
.to((maxQueueCapacity) -> customizeMaxQueueCapacity(factory, maxQueueCapacity));
102107
map.from(this.serverProperties.getMaxHttpRequestHeaderSize())
103108
.asInt(DataSize::toBytes)
104109
.when(this::isPositive)
@@ -146,23 +151,25 @@ public void customize(ConfigurableTomcatWebServerFactory factory) {
146151
customizeErrorReportValve(this.serverProperties.getError(), factory);
147152
}
148153

149-
private void configureExecutor(ConfigurableTomcatWebServerFactory factory, Threads threadProperties) {
150-
factory.addProtocolHandlerCustomizers((handler) -> {
151-
StandardThreadExecutor executor = new StandardThreadExecutor();
152-
executor.setMinSpareThreads(threadProperties.getMinSpare());
153-
executor.setMaxThreads(threadProperties.getMax());
154-
executor.setMaxQueueSize(threadProperties.getMaxQueueCapacity());
155-
if (handler instanceof AbstractProtocol<?> protocol) {
156-
executor.setNamePrefix(ObjectName.unquote(protocol.getName()) + "-exec-");
157-
}
158-
handler.setExecutor(executor);
159-
});
160-
}
161-
162154
private boolean isPositive(int value) {
163155
return value > 0;
164156
}
165157

158+
@SuppressWarnings("rawtypes")
159+
private void customizeMaxThreads(ConfigurableTomcatWebServerFactory factory, int maxThreads) {
160+
customizeHandler(factory, maxThreads, AbstractProtocol.class, AbstractProtocol::setMaxThreads);
161+
}
162+
163+
@SuppressWarnings("rawtypes")
164+
private void customizeMinThreads(ConfigurableTomcatWebServerFactory factory, int minSpareThreads) {
165+
customizeHandler(factory, minSpareThreads, AbstractProtocol.class, AbstractProtocol::setMinSpareThreads);
166+
}
167+
168+
@SuppressWarnings("rawtypes")
169+
private void customizeMaxQueueCapacity(ConfigurableTomcatWebServerFactory factory, int maxQueueCapacity) {
170+
customizeHandler(factory, maxQueueCapacity, AbstractProtocol.class, AbstractProtocol::setMaxQueueSize);
171+
}
172+
166173
@SuppressWarnings("rawtypes")
167174
private void customizeAcceptCount(ConfigurableTomcatWebServerFactory factory, int acceptCount) {
168175
customizeHandler(factory, acceptCount, AbstractProtocol.class, AbstractProtocol::setAcceptCount);

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717
package org.springframework.boot.autoconfigure.web.embedded;
1818

1919
import java.util.Locale;
20-
import java.util.concurrent.Executor;
2120
import java.util.function.Consumer;
2221

2322
import org.apache.catalina.Context;
2423
import org.apache.catalina.Valve;
25-
import org.apache.catalina.core.StandardThreadExecutor;
2624
import org.apache.catalina.startup.Tomcat;
2725
import org.apache.catalina.valves.AccessLogValve;
2826
import org.apache.catalina.valves.ErrorReportValve;
@@ -572,12 +570,10 @@ void configureExecutor() {
572570
bind("server.tomcat.threads.max=10", "server.tomcat.threads.min-spare=2",
573571
"server.tomcat.threads.max-queue-capacity=20");
574572
customizeAndRunServer((server) -> {
575-
Executor executor = server.getTomcat().getConnector().getProtocolHandler().getExecutor();
576-
assertThat(executor).isInstanceOf(StandardThreadExecutor.class);
577-
StandardThreadExecutor standardThreadExecutor = (StandardThreadExecutor) executor;
578-
assertThat(standardThreadExecutor.getMaxThreads()).isEqualTo(10);
579-
assertThat(standardThreadExecutor.getMinSpareThreads()).isEqualTo(2);
580-
assertThat(standardThreadExecutor.getMaxQueueSize()).isEqualTo(20);
573+
AbstractProtocol<?> protocol = (AbstractProtocol<?>) server.getTomcat().getConnector().getProtocolHandler();
574+
assertThat(protocol.getMaxThreads()).isEqualTo(10);
575+
assertThat(protocol.getMinSpareThreads()).isEqualTo(2);
576+
assertThat(protocol.getMaxQueueSize()).isEqualTo(20);
581577
});
582578
}
583579

0 commit comments

Comments
 (0)