Skip to content

Commit 64e8b1d

Browse files
committed
Polish "Add properties for Jetty threadpool"
See spring-projectsgh-17871
1 parent 1024d74 commit 64e8b1d

File tree

3 files changed

+18
-42
lines changed

3 files changed

+18
-42
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -907,12 +907,12 @@ public static class Jetty {
907907
private Integer selectors = -1;
908908

909909
/**
910-
* Maximum amount of threads.
910+
* Maximum number of threads.
911911
*/
912912
private Integer maxThreads = 200;
913913

914914
/**
915-
* Minimum amount of threads.
915+
* Minimum number of threads.
916916
*/
917917
private Integer minThreads = 8;
918918

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

+7-24
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;
@@ -82,11 +83,11 @@ public void customize(ConfigurableJettyWebServerFactory factory) {
8283
propertyMapper.from(jettyProperties::getMaxHttpPostSize).asInt(DataSize::toBytes).when(this::isPositive)
8384
.to((maxHttpPostSize) -> customizeMaxHttpPostSize(factory, maxHttpPostSize));
8485
propertyMapper.from(jettyProperties::getMaxThreads).when(this::isPositive)
85-
.to((maxThreads) -> customizeMaxThreads(factory, maxThreads));
86+
.to((maxThreads) -> customizeThreadPool(factory, (threadPool) -> threadPool.setMaxThreads(maxThreads)));
8687
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));
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)));
9091
propertyMapper.from(properties::getConnectionTimeout).whenNonNull()
9192
.to((connectionTimeout) -> customizeConnectionTimeout(factory, connectionTimeout));
9293
propertyMapper.from(jettyProperties::getAccesslog).when(ServerProperties.Jetty.Accesslog::isEnabled)
@@ -140,29 +141,11 @@ else if (handler instanceof HandlerCollection) {
140141
});
141142
}
142143

143-
private void customizeMaxThreads(ConfigurableJettyWebServerFactory factory, int maxThreads) {
144+
private void customizeThreadPool(ConfigurableJettyWebServerFactory factory, Consumer<QueuedThreadPool> customizer) {
144145
factory.addServerCustomizers((connector) -> {
145146
ThreadPool threadPool = connector.getThreadPool();
146147
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);
148+
customizer.accept((QueuedThreadPool) threadPool);
166149
}
167150
});
168151
}

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

+9-16
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.eclipse.jetty.server.RequestLog;
2929
import org.eclipse.jetty.server.RequestLogWriter;
3030
import org.eclipse.jetty.util.thread.QueuedThreadPool;
31-
import org.eclipse.jetty.util.thread.ThreadPool;
3231
import org.junit.jupiter.api.BeforeEach;
3332
import org.junit.jupiter.api.Test;
3433

@@ -116,33 +115,27 @@ void accessLogCanBeEnabled() {
116115
}
117116

118117
@Test
119-
void maxThreadsCanBeCustomized() throws IOException {
118+
void maxThreadsCanBeCustomized() {
120119
bind("server.jetty.max-threads=100");
121120
JettyWebServer server = customizeAndGetServer();
122-
ThreadPool threadPool = server.getServer().getThreadPool();
123-
if (threadPool instanceof QueuedThreadPool) {
124-
assertThat(((QueuedThreadPool) threadPool).getMaxThreads()).isEqualTo(100);
125-
}
121+
QueuedThreadPool threadPool = (QueuedThreadPool) server.getServer().getThreadPool();
122+
assertThat(threadPool.getMaxThreads()).isEqualTo(100);
126123
}
127124

128125
@Test
129-
void minThreadsCanBeCustomized() throws IOException {
126+
void minThreadsCanBeCustomized() {
130127
bind("server.jetty.min-threads=100");
131128
JettyWebServer server = customizeAndGetServer();
132-
ThreadPool threadPool = server.getServer().getThreadPool();
133-
if (threadPool instanceof QueuedThreadPool) {
134-
assertThat(((QueuedThreadPool) threadPool).getMinThreads()).isEqualTo(100);
135-
}
129+
QueuedThreadPool threadPool = (QueuedThreadPool) server.getServer().getThreadPool();
130+
assertThat(threadPool.getMinThreads()).isEqualTo(100);
136131
}
137132

138133
@Test
139-
void idleTimeoutCanBeCustomized() throws IOException {
134+
void idleTimeoutCanBeCustomized() {
140135
bind("server.jetty.idle-timeout=100");
141136
JettyWebServer server = customizeAndGetServer();
142-
ThreadPool threadPool = server.getServer().getThreadPool();
143-
if (threadPool instanceof QueuedThreadPool) {
144-
assertThat(((QueuedThreadPool) threadPool).getIdleTimeout()).isEqualTo(100);
145-
}
137+
QueuedThreadPool threadPool = (QueuedThreadPool) server.getServer().getThreadPool();
138+
assertThat(threadPool.getIdleTimeout()).isEqualTo(100);
146139
}
147140

148141
private CustomRequestLog getRequestLog(JettyWebServer server) {

0 commit comments

Comments
 (0)