Skip to content

Commit 1024d74

Browse files
htztomicmbhave
authored andcommitted
Add properties for Jetty threadpool
See gh-17871
1 parent 09b690b commit 1024d74

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

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

+40
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 amount of threads.
911+
*/
912+
private Integer maxThreads = 200;
913+
914+
/**
915+
* Minimum amount 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
*/

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

+36
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import org.eclipse.jetty.server.handler.ContextHandler;
3030
import org.eclipse.jetty.server.handler.HandlerCollection;
3131
import org.eclipse.jetty.server.handler.HandlerWrapper;
32+
import org.eclipse.jetty.util.thread.QueuedThreadPool;
33+
import org.eclipse.jetty.util.thread.ThreadPool;
3234

3335
import org.springframework.boot.autoconfigure.web.ServerProperties;
3436
import org.springframework.boot.cloud.CloudPlatform;
@@ -46,6 +48,7 @@
4648
*
4749
* @author Brian Clozel
4850
* @author Phillip Webb
51+
* @author HaiTao Zhang
4952
* @since 2.0.0
5053
*/
5154
public class JettyWebServerFactoryCustomizer
@@ -78,6 +81,12 @@ public void customize(ConfigurableJettyWebServerFactory factory) {
7881
.addServerCustomizers(new MaxHttpHeaderSizeCustomizer(maxHttpHeaderSize)));
7982
propertyMapper.from(jettyProperties::getMaxHttpPostSize).asInt(DataSize::toBytes).when(this::isPositive)
8083
.to((maxHttpPostSize) -> customizeMaxHttpPostSize(factory, maxHttpPostSize));
84+
propertyMapper.from(jettyProperties::getMaxThreads).when(this::isPositive)
85+
.to((maxThreads) -> customizeMaxThreads(factory, maxThreads));
86+
propertyMapper.from(jettyProperties::getMinThreads).when(this::isPositive)
87+
.to((minThreads) -> customizeMinThreads(factory, minThreads));
88+
propertyMapper.from(jettyProperties::getIdleTimeout).when(this::isPositive)
89+
.to((idleTimeout) -> customizeIdleTimeout(factory, idleTimeout));
8190
propertyMapper.from(properties::getConnectionTimeout).whenNonNull()
8291
.to((connectionTimeout) -> customizeConnectionTimeout(factory, connectionTimeout));
8392
propertyMapper.from(jettyProperties::getAccesslog).when(ServerProperties.Jetty.Accesslog::isEnabled)
@@ -131,6 +140,33 @@ else if (handler instanceof HandlerCollection) {
131140
});
132141
}
133142

143+
private void customizeMaxThreads(ConfigurableJettyWebServerFactory factory, int maxThreads) {
144+
factory.addServerCustomizers((connector) -> {
145+
ThreadPool threadPool = connector.getThreadPool();
146+
if (threadPool instanceof QueuedThreadPool) {
147+
((QueuedThreadPool) threadPool).setMaxThreads(maxThreads);
148+
}
149+
});
150+
}
151+
152+
private void customizeMinThreads(ConfigurableJettyWebServerFactory factory, int minThreads) {
153+
factory.addServerCustomizers((connector) -> {
154+
ThreadPool threadPool = connector.getThreadPool();
155+
if (threadPool instanceof QueuedThreadPool) {
156+
((QueuedThreadPool) threadPool).setMinThreads(minThreads);
157+
}
158+
});
159+
}
160+
161+
private void customizeIdleTimeout(ConfigurableJettyWebServerFactory factory, int idleTimeout) {
162+
factory.addServerCustomizers((connector) -> {
163+
ThreadPool threadPool = connector.getThreadPool();
164+
if (threadPool instanceof QueuedThreadPool) {
165+
((QueuedThreadPool) threadPool).setIdleTimeout(idleTimeout);
166+
}
167+
});
168+
}
169+
134170
private void customizeAccessLog(ConfigurableJettyWebServerFactory factory,
135171
ServerProperties.Jetty.Accesslog properties) {
136172
factory.addServerCustomizers((server) -> {

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

+19
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");

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

+33
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
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;
31+
import org.eclipse.jetty.util.thread.ThreadPool;
3032
import org.junit.jupiter.api.BeforeEach;
3133
import org.junit.jupiter.api.Test;
3234

@@ -49,6 +51,7 @@
4951
*
5052
* @author Brian Clozel
5153
* @author Phillip Webb
54+
* @author HaiTao Zhang
5255
*/
5356
class JettyWebServerFactoryCustomizerTests {
5457

@@ -112,6 +115,36 @@ void accessLogCanBeEnabled() {
112115
assertThat(logWriter.isAppend()).isFalse();
113116
}
114117

118+
@Test
119+
void maxThreadsCanBeCustomized() throws IOException {
120+
bind("server.jetty.max-threads=100");
121+
JettyWebServer server = customizeAndGetServer();
122+
ThreadPool threadPool = server.getServer().getThreadPool();
123+
if (threadPool instanceof QueuedThreadPool) {
124+
assertThat(((QueuedThreadPool) threadPool).getMaxThreads()).isEqualTo(100);
125+
}
126+
}
127+
128+
@Test
129+
void minThreadsCanBeCustomized() throws IOException {
130+
bind("server.jetty.min-threads=100");
131+
JettyWebServer server = customizeAndGetServer();
132+
ThreadPool threadPool = server.getServer().getThreadPool();
133+
if (threadPool instanceof QueuedThreadPool) {
134+
assertThat(((QueuedThreadPool) threadPool).getMinThreads()).isEqualTo(100);
135+
}
136+
}
137+
138+
@Test
139+
void idleTimeoutCanBeCustomized() throws IOException {
140+
bind("server.jetty.idle-timeout=100");
141+
JettyWebServer server = customizeAndGetServer();
142+
ThreadPool threadPool = server.getServer().getThreadPool();
143+
if (threadPool instanceof QueuedThreadPool) {
144+
assertThat(((QueuedThreadPool) threadPool).getIdleTimeout()).isEqualTo(100);
145+
}
146+
}
147+
115148
private CustomRequestLog getRequestLog(JettyWebServer server) {
116149
RequestLog requestLog = server.getServer().getRequestLog();
117150
assertThat(requestLog).isInstanceOf(CustomRequestLog.class);

0 commit comments

Comments
 (0)