Skip to content

Commit 140c37c

Browse files
committed
Enable virtual threads on Jetty
Closes gh-35703
1 parent 23979e6 commit 140c37c

File tree

7 files changed

+187
-31
lines changed

7 files changed

+187
-31
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementChildContextConfiguration.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementWebServerFactoryCustomizer;
2727
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
2828
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
29+
import org.springframework.boot.autoconfigure.web.embedded.JettyVirtualThreadsWebServerFactoryCustomizer;
2930
import org.springframework.boot.autoconfigure.web.embedded.JettyWebServerFactoryCustomizer;
3031
import org.springframework.boot.autoconfigure.web.embedded.NettyWebServerFactoryCustomizer;
3132
import org.springframework.boot.autoconfigure.web.embedded.TomcatVirtualThreadsWebServerFactoryCustomizer;
@@ -79,7 +80,8 @@ static class ReactiveManagementWebServerFactoryCustomizer
7980
super(beanFactory, ReactiveWebServerFactoryCustomizer.class, TomcatWebServerFactoryCustomizer.class,
8081
TomcatReactiveWebServerFactoryCustomizer.class,
8182
TomcatVirtualThreadsWebServerFactoryCustomizer.class, JettyWebServerFactoryCustomizer.class,
82-
UndertowWebServerFactoryCustomizer.class, NettyWebServerFactoryCustomizer.class);
83+
JettyVirtualThreadsWebServerFactoryCustomizer.class, UndertowWebServerFactoryCustomizer.class,
84+
NettyWebServerFactoryCustomizer.class);
8385
}
8486

8587
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfiguration.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
4040
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
4141
import org.springframework.boot.autoconfigure.web.ServerProperties;
42+
import org.springframework.boot.autoconfigure.web.embedded.JettyVirtualThreadsWebServerFactoryCustomizer;
4243
import org.springframework.boot.autoconfigure.web.embedded.JettyWebServerFactoryCustomizer;
4344
import org.springframework.boot.autoconfigure.web.embedded.TomcatVirtualThreadsWebServerFactoryCustomizer;
4445
import org.springframework.boot.autoconfigure.web.embedded.TomcatWebServerFactoryCustomizer;
@@ -124,8 +125,8 @@ static class ServletManagementWebServerFactoryCustomizer
124125
ServletManagementWebServerFactoryCustomizer(ListableBeanFactory beanFactory) {
125126
super(beanFactory, ServletWebServerFactoryCustomizer.class, TomcatServletWebServerFactoryCustomizer.class,
126127
TomcatWebServerFactoryCustomizer.class, TomcatVirtualThreadsWebServerFactoryCustomizer.class,
127-
JettyWebServerFactoryCustomizer.class, UndertowServletWebServerFactoryCustomizer.class,
128-
UndertowWebServerFactoryCustomizer.class);
128+
JettyWebServerFactoryCustomizer.class, JettyVirtualThreadsWebServerFactoryCustomizer.class,
129+
UndertowServletWebServerFactoryCustomizer.class, UndertowWebServerFactoryCustomizer.class);
129130
}
130131

131132
@Override

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

+7
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ public JettyWebServerFactoryCustomizer jettyWebServerFactoryCustomizer(Environme
8484
return new JettyWebServerFactoryCustomizer(environment, serverProperties);
8585
}
8686

87+
@Bean
88+
@ConditionalOnVirtualThreads
89+
JettyVirtualThreadsWebServerFactoryCustomizer jettyVirtualThreadsWebServerFactoryCustomizer(
90+
ServerProperties serverProperties) {
91+
return new JettyVirtualThreadsWebServerFactoryCustomizer(serverProperties);
92+
}
93+
8794
}
8895

8996
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.web.embedded;
18+
19+
import java.util.concurrent.BlockingQueue;
20+
import java.util.concurrent.SynchronousQueue;
21+
22+
import org.eclipse.jetty.util.BlockingArrayQueue;
23+
import org.eclipse.jetty.util.thread.QueuedThreadPool;
24+
import org.eclipse.jetty.util.thread.ThreadPool;
25+
26+
import org.springframework.boot.autoconfigure.web.ServerProperties;
27+
28+
/**
29+
* Creates a {@link ThreadPool} for Jetty, applying the
30+
* {@link ServerProperties.Jetty.Threads} properties.
31+
*
32+
* @author Moritz Halbritter
33+
*/
34+
final class JettyThreadPool {
35+
36+
private JettyThreadPool() {
37+
}
38+
39+
static QueuedThreadPool create(ServerProperties.Jetty.Threads properties) {
40+
BlockingQueue<Runnable> queue = determineBlockingQueue(properties.getMaxQueueCapacity());
41+
int maxThreadCount = (properties.getMax() > 0) ? properties.getMax() : 200;
42+
int minThreadCount = (properties.getMin() > 0) ? properties.getMin() : 8;
43+
int threadIdleTimeout = (properties.getIdleTimeout() != null) ? (int) properties.getIdleTimeout().toMillis()
44+
: 60000;
45+
return new QueuedThreadPool(maxThreadCount, minThreadCount, threadIdleTimeout, queue);
46+
}
47+
48+
private static BlockingQueue<Runnable> determineBlockingQueue(Integer maxQueueCapacity) {
49+
if (maxQueueCapacity == null) {
50+
return null;
51+
}
52+
if (maxQueueCapacity == 0) {
53+
return new SynchronousQueue<>();
54+
}
55+
else {
56+
return new BlockingArrayQueue<>(maxQueueCapacity);
57+
}
58+
}
59+
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.web.embedded;
18+
19+
import org.eclipse.jetty.util.VirtualThreads;
20+
import org.eclipse.jetty.util.thread.QueuedThreadPool;
21+
22+
import org.springframework.boot.autoconfigure.web.ServerProperties;
23+
import org.springframework.boot.web.embedded.jetty.ConfigurableJettyWebServerFactory;
24+
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
25+
import org.springframework.core.Ordered;
26+
import org.springframework.util.Assert;
27+
28+
/**
29+
* Activates virtual threads on the {@link ConfigurableJettyWebServerFactory}.
30+
*
31+
* @author Moritz Halbritter
32+
* @since 3.2.0
33+
*/
34+
public class JettyVirtualThreadsWebServerFactoryCustomizer
35+
implements WebServerFactoryCustomizer<ConfigurableJettyWebServerFactory>, Ordered {
36+
37+
private final ServerProperties serverProperties;
38+
39+
public JettyVirtualThreadsWebServerFactoryCustomizer(ServerProperties serverProperties) {
40+
this.serverProperties = serverProperties;
41+
}
42+
43+
@Override
44+
public void customize(ConfigurableJettyWebServerFactory factory) {
45+
Assert.state(VirtualThreads.areSupported(), "Virtual threads are not supported");
46+
QueuedThreadPool threadPool = JettyThreadPool.create(this.serverProperties.getJetty().getThreads());
47+
threadPool.setVirtualThreadsExecutor(VirtualThreads.getDefaultVirtualThreadsExecutor());
48+
factory.setThreadPool(threadPool);
49+
}
50+
51+
@Override
52+
public int getOrder() {
53+
return JettyWebServerFactoryCustomizer.ORDER + 1;
54+
}
55+
56+
}

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

+4-28
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
import java.time.Duration;
2020
import java.util.Arrays;
21-
import java.util.concurrent.BlockingQueue;
22-
import java.util.concurrent.SynchronousQueue;
2321

2422
import org.eclipse.jetty.server.AbstractConnector;
2523
import org.eclipse.jetty.server.ConnectionFactory;
@@ -31,9 +29,6 @@
3129
import org.eclipse.jetty.server.handler.ContextHandler;
3230
import org.eclipse.jetty.server.handler.HandlerCollection;
3331
import org.eclipse.jetty.server.handler.HandlerWrapper;
34-
import org.eclipse.jetty.util.BlockingArrayQueue;
35-
import org.eclipse.jetty.util.thread.QueuedThreadPool;
36-
import org.eclipse.jetty.util.thread.ThreadPool;
3732

3833
import org.springframework.boot.autoconfigure.web.ServerProperties;
3934
import org.springframework.boot.cloud.CloudPlatform;
@@ -60,6 +55,8 @@
6055
public class JettyWebServerFactoryCustomizer
6156
implements WebServerFactoryCustomizer<ConfigurableJettyWebServerFactory>, Ordered {
6257

58+
static final int ORDER = 0;
59+
6360
private final Environment environment;
6461

6562
private final ServerProperties serverProperties;
@@ -71,15 +68,15 @@ public JettyWebServerFactoryCustomizer(Environment environment, ServerProperties
7168

7269
@Override
7370
public int getOrder() {
74-
return 0;
71+
return ORDER;
7572
}
7673

7774
@Override
7875
public void customize(ConfigurableJettyWebServerFactory factory) {
7976
ServerProperties.Jetty properties = this.serverProperties.getJetty();
8077
factory.setUseForwardHeaders(getOrDeduceUseForwardHeaders());
8178
ServerProperties.Jetty.Threads threadProperties = properties.getThreads();
82-
factory.setThreadPool(determineThreadPool(properties.getThreads()));
79+
factory.setThreadPool(JettyThreadPool.create(properties.getThreads()));
8380
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
8481
map.from(properties::getMaxConnections).to(factory::setMaxConnections);
8582
map.from(threadProperties::getAcceptors).to(factory::setAcceptors);
@@ -151,27 +148,6 @@ else if (handler instanceof HandlerCollection collection) {
151148
});
152149
}
153150

154-
private ThreadPool determineThreadPool(ServerProperties.Jetty.Threads properties) {
155-
BlockingQueue<Runnable> queue = determineBlockingQueue(properties.getMaxQueueCapacity());
156-
int maxThreadCount = (properties.getMax() > 0) ? properties.getMax() : 200;
157-
int minThreadCount = (properties.getMin() > 0) ? properties.getMin() : 8;
158-
int threadIdleTimeout = (properties.getIdleTimeout() != null) ? (int) properties.getIdleTimeout().toMillis()
159-
: 60000;
160-
return new QueuedThreadPool(maxThreadCount, minThreadCount, threadIdleTimeout, queue);
161-
}
162-
163-
private BlockingQueue<Runnable> determineBlockingQueue(Integer maxQueueCapacity) {
164-
if (maxQueueCapacity == null) {
165-
return null;
166-
}
167-
if (maxQueueCapacity == 0) {
168-
return new SynchronousQueue<>();
169-
}
170-
else {
171-
return new BlockingArrayQueue<>(maxQueueCapacity);
172-
}
173-
}
174-
175151
private void customizeAccessLog(ConfigurableJettyWebServerFactory factory,
176152
ServerProperties.Jetty.Accesslog properties) {
177153
factory.addServerCustomizers((server) -> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.web.embedded;
18+
19+
import org.eclipse.jetty.util.thread.QueuedThreadPool;
20+
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.condition.EnabledForJreRange;
22+
import org.junit.jupiter.api.condition.JRE;
23+
24+
import org.springframework.boot.autoconfigure.web.ServerProperties;
25+
import org.springframework.boot.web.embedded.jetty.ConfigurableJettyWebServerFactory;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
import static org.mockito.ArgumentMatchers.assertArg;
29+
import static org.mockito.BDDMockito.then;
30+
import static org.mockito.Mockito.mock;
31+
32+
/**
33+
* Tests for {@link JettyVirtualThreadsWebServerFactoryCustomizer}.
34+
*
35+
* @author Moritz Halbritter
36+
*/
37+
class JettyVirtualThreadsWebServerFactoryCustomizerTests {
38+
39+
@Test
40+
@EnabledForJreRange(min = JRE.JAVA_21)
41+
void shouldConfigureVirtualThreads() {
42+
ServerProperties properties = new ServerProperties();
43+
JettyVirtualThreadsWebServerFactoryCustomizer customizer = new JettyVirtualThreadsWebServerFactoryCustomizer(
44+
properties);
45+
ConfigurableJettyWebServerFactory factory = mock(ConfigurableJettyWebServerFactory.class);
46+
customizer.customize(factory);
47+
then(factory).should().setThreadPool(assertArg((threadPool) -> {
48+
assertThat(threadPool).isInstanceOf(QueuedThreadPool.class);
49+
QueuedThreadPool queuedThreadPool = (QueuedThreadPool) threadPool;
50+
assertThat(queuedThreadPool.getVirtualThreadsExecutor()).isNotNull();
51+
}));
52+
}
53+
54+
}

0 commit comments

Comments
 (0)