Skip to content

Commit b853dff

Browse files
committed
Merge pull request #17871 from htztomic
* pr/17871: Polish "Add properties for Jetty threadpool" Add properties for Jetty threadpool Closes gh-17871
2 parents 09b690b + 64e8b1d commit b853dff

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
* @author Andrew McGhie
6363
* @author Rafiullah Hamedy
6464
* @author Dirk Deyne
65+
* @author HaiTao Zhang
6566
* @since 1.0.0
6667
*/
6768
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
@@ -905,6 +906,21 @@ public static class Jetty {
905906
*/
906907
private Integer selectors = -1;
907908

909+
/**
910+
* Maximum number of threads.
911+
*/
912+
private Integer maxThreads = 200;
913+
914+
/**
915+
* Minimum number of threads.
916+
*/
917+
private Integer minThreads = 8;
918+
919+
/**
920+
* Maximum thread idle time.
921+
*/
922+
private Integer idleTimeout = 60000;
923+
908924
public Accesslog getAccesslog() {
909925
return this.accesslog;
910926
}
@@ -933,6 +949,30 @@ public void setSelectors(Integer selectors) {
933949
this.selectors = selectors;
934950
}
935951

952+
public void setMinThreads(Integer minThreads) {
953+
this.minThreads = minThreads;
954+
}
955+
956+
public Integer getMinThreads() {
957+
return this.minThreads;
958+
}
959+
960+
public void setMaxThreads(Integer maxThreads) {
961+
this.maxThreads = maxThreads;
962+
}
963+
964+
public Integer getMaxThreads() {
965+
return this.maxThreads;
966+
}
967+
968+
public void setIdleTimeout(Integer idleTimeout) {
969+
this.idleTimeout = idleTimeout;
970+
}
971+
972+
public Integer getIdleTimeout() {
973+
return this.idleTimeout;
974+
}
975+
936976
/**
937977
* Jetty access log properties.
938978
*/

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.time.Duration;
2020
import java.util.Arrays;
21+
import java.util.function.Consumer;
2122

2223
import org.eclipse.jetty.server.AbstractConnector;
2324
import org.eclipse.jetty.server.ConnectionFactory;
@@ -29,6 +30,8 @@
2930
import org.eclipse.jetty.server.handler.ContextHandler;
3031
import org.eclipse.jetty.server.handler.HandlerCollection;
3132
import org.eclipse.jetty.server.handler.HandlerWrapper;
33+
import org.eclipse.jetty.util.thread.QueuedThreadPool;
34+
import org.eclipse.jetty.util.thread.ThreadPool;
3235

3336
import org.springframework.boot.autoconfigure.web.ServerProperties;
3437
import org.springframework.boot.cloud.CloudPlatform;
@@ -46,6 +49,7 @@
4649
*
4750
* @author Brian Clozel
4851
* @author Phillip Webb
52+
* @author HaiTao Zhang
4953
* @since 2.0.0
5054
*/
5155
public class JettyWebServerFactoryCustomizer
@@ -78,6 +82,12 @@ public void customize(ConfigurableJettyWebServerFactory factory) {
7882
.addServerCustomizers(new MaxHttpHeaderSizeCustomizer(maxHttpHeaderSize)));
7983
propertyMapper.from(jettyProperties::getMaxHttpPostSize).asInt(DataSize::toBytes).when(this::isPositive)
8084
.to((maxHttpPostSize) -> customizeMaxHttpPostSize(factory, maxHttpPostSize));
85+
propertyMapper.from(jettyProperties::getMaxThreads).when(this::isPositive)
86+
.to((maxThreads) -> customizeThreadPool(factory, (threadPool) -> threadPool.setMaxThreads(maxThreads)));
87+
propertyMapper.from(jettyProperties::getMinThreads).when(this::isPositive)
88+
.to((minThreads) -> customizeThreadPool(factory, (threadPool) -> threadPool.setMinThreads(minThreads)));
89+
propertyMapper.from(jettyProperties::getIdleTimeout).when(this::isPositive).to(
90+
(idleTimeout) -> customizeThreadPool(factory, (threadPool) -> threadPool.setIdleTimeout(idleTimeout)));
8191
propertyMapper.from(properties::getConnectionTimeout).whenNonNull()
8292
.to((connectionTimeout) -> customizeConnectionTimeout(factory, connectionTimeout));
8393
propertyMapper.from(jettyProperties::getAccesslog).when(ServerProperties.Jetty.Accesslog::isEnabled)
@@ -131,6 +141,15 @@ else if (handler instanceof HandlerCollection) {
131141
});
132142
}
133143

144+
private void customizeThreadPool(ConfigurableJettyWebServerFactory factory, Consumer<QueuedThreadPool> customizer) {
145+
factory.addServerCustomizers((connector) -> {
146+
ThreadPool threadPool = connector.getThreadPool();
147+
if (threadPool instanceof QueuedThreadPool) {
148+
customizer.accept((QueuedThreadPool) threadPool);
149+
}
150+
});
151+
}
152+
134153
private void customizeAccessLog(ConfigurableJettyWebServerFactory factory,
135154
ServerProperties.Jetty.Accesslog properties) {
136155
factory.addServerCustomizers((server) -> {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
* @author Quinten De Swaef
7575
* @author Venil Noronha
7676
* @author Andrew McGhie
77+
* @author HaiTao Zhang
7778
*/
7879
class ServerPropertiesTests {
7980

@@ -218,6 +219,24 @@ void testCustomizeJettySelectors() {
218219
assertThat(this.properties.getJetty().getSelectors()).isEqualTo(10);
219220
}
220221

222+
@Test
223+
void testCustomizeJettyMaxThreads() {
224+
bind("server.jetty.max-threads", "10");
225+
assertThat(this.properties.getJetty().getMaxThreads()).isEqualTo(10);
226+
}
227+
228+
@Test
229+
void testCustomizeJettyMinThreads() {
230+
bind("server.jetty.min-threads", "10");
231+
assertThat(this.properties.getJetty().getMinThreads()).isEqualTo(10);
232+
}
233+
234+
@Test
235+
void testCustomizeJettyIdleTimeout() {
236+
bind("server.jetty.idle-timeout", "10");
237+
assertThat(this.properties.getJetty().getIdleTimeout()).isEqualTo(10);
238+
}
239+
221240
@Test
222241
void testCustomizeUndertowServerOption() {
223242
bind("server.undertow.options.server.ALWAYS_SET_KEEP_ALIVE", "true");

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.eclipse.jetty.server.HttpConfiguration.ConnectionFactory;
2828
import org.eclipse.jetty.server.RequestLog;
2929
import org.eclipse.jetty.server.RequestLogWriter;
30+
import org.eclipse.jetty.util.thread.QueuedThreadPool;
3031
import org.junit.jupiter.api.BeforeEach;
3132
import org.junit.jupiter.api.Test;
3233

@@ -49,6 +50,7 @@
4950
*
5051
* @author Brian Clozel
5152
* @author Phillip Webb
53+
* @author HaiTao Zhang
5254
*/
5355
class JettyWebServerFactoryCustomizerTests {
5456

@@ -112,6 +114,30 @@ void accessLogCanBeEnabled() {
112114
assertThat(logWriter.isAppend()).isFalse();
113115
}
114116

117+
@Test
118+
void maxThreadsCanBeCustomized() {
119+
bind("server.jetty.max-threads=100");
120+
JettyWebServer server = customizeAndGetServer();
121+
QueuedThreadPool threadPool = (QueuedThreadPool) server.getServer().getThreadPool();
122+
assertThat(threadPool.getMaxThreads()).isEqualTo(100);
123+
}
124+
125+
@Test
126+
void minThreadsCanBeCustomized() {
127+
bind("server.jetty.min-threads=100");
128+
JettyWebServer server = customizeAndGetServer();
129+
QueuedThreadPool threadPool = (QueuedThreadPool) server.getServer().getThreadPool();
130+
assertThat(threadPool.getMinThreads()).isEqualTo(100);
131+
}
132+
133+
@Test
134+
void idleTimeoutCanBeCustomized() {
135+
bind("server.jetty.idle-timeout=100");
136+
JettyWebServer server = customizeAndGetServer();
137+
QueuedThreadPool threadPool = (QueuedThreadPool) server.getServer().getThreadPool();
138+
assertThat(threadPool.getIdleTimeout()).isEqualTo(100);
139+
}
140+
115141
private CustomRequestLog getRequestLog(JettyWebServer server) {
116142
RequestLog requestLog = server.getServer().getRequestLog();
117143
assertThat(requestLog).isInstanceOf(CustomRequestLog.class);

0 commit comments

Comments
 (0)