Browse Source

Configure Jetty with VirtualThreadPool

Prior to this commit, Spring Boot would auto-configure a
`QueuedThreadPool` on the Jetty embedded server when the virtual threads
option is enabled.

This commit configures instead the dedicated `VirtualThreadPool` that
the Jetty team recommends when Virtual Threads is supported.

Fixes gh-47690
pull/47902/head
Brian Clozel 2 months ago
parent
commit
9dda15a825
  1. 9
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/JettyVirtualThreadsWebServerFactoryCustomizer.java
  2. 19
      spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/JettyVirtualThreadsWebServerFactoryCustomizerTests.java

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

@ -17,7 +17,7 @@ @@ -17,7 +17,7 @@
package org.springframework.boot.autoconfigure.web.embedded;
import org.eclipse.jetty.util.VirtualThreads;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.util.thread.VirtualThreadPool;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.web.embedded.jetty.ConfigurableJettyWebServerFactory;
@ -29,6 +29,7 @@ import org.springframework.util.Assert; @@ -29,6 +29,7 @@ import org.springframework.util.Assert;
* Activates virtual threads on the {@link ConfigurableJettyWebServerFactory}.
*
* @author Moritz Halbritter
* @author Brian Clozel
* @since 3.2.0
*/
public class JettyVirtualThreadsWebServerFactoryCustomizer
@ -43,9 +44,9 @@ public class JettyVirtualThreadsWebServerFactoryCustomizer @@ -43,9 +44,9 @@ public class JettyVirtualThreadsWebServerFactoryCustomizer
@Override
public void customize(ConfigurableJettyWebServerFactory factory) {
Assert.state(VirtualThreads.areSupported(), "Virtual threads are not supported");
QueuedThreadPool threadPool = JettyThreadPool.create(this.serverProperties.getJetty().getThreads());
threadPool.setVirtualThreadsExecutor(VirtualThreads.getNamedVirtualThreadsExecutor("jetty-"));
factory.setThreadPool(threadPool);
VirtualThreadPool virtualThreadPool = new VirtualThreadPool();
virtualThreadPool.setName("jetty-");
factory.setThreadPool(virtualThreadPool);
}
@Override

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

@ -16,13 +16,7 @@ @@ -16,13 +16,7 @@
package org.springframework.boot.autoconfigure.web.embedded;
import java.time.Duration;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicReference;
import org.awaitility.Awaitility;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.hamcrest.Matchers;
import org.eclipse.jetty.util.thread.VirtualThreadPool;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
@ -51,14 +45,9 @@ class JettyVirtualThreadsWebServerFactoryCustomizerTests { @@ -51,14 +45,9 @@ class JettyVirtualThreadsWebServerFactoryCustomizerTests {
ConfigurableJettyWebServerFactory factory = mock(ConfigurableJettyWebServerFactory.class);
customizer.customize(factory);
then(factory).should().setThreadPool(assertArg((threadPool) -> {
assertThat(threadPool).isInstanceOf(QueuedThreadPool.class);
QueuedThreadPool queuedThreadPool = (QueuedThreadPool) threadPool;
Executor executor = queuedThreadPool.getVirtualThreadsExecutor();
assertThat(executor).isNotNull();
AtomicReference<String> threadName = new AtomicReference<>();
executor.execute(() -> threadName.set(Thread.currentThread().getName()));
Awaitility.await().atMost(Duration.ofSeconds(1)).untilAtomic(threadName, Matchers.notNullValue());
assertThat(threadName.get()).startsWith("jetty-");
assertThat(threadPool).isInstanceOf(VirtualThreadPool.class);
VirtualThreadPool virtualThreadPool = (VirtualThreadPool) threadPool;
assertThat(virtualThreadPool.getName()).isEqualTo("jetty-");
}));
}

Loading…
Cancel
Save