From fe7501b2926722bf1ba4cd6e96fa5a6a8b4399d0 Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Thu, 4 Apr 2024 11:44:14 +0200 Subject: [PATCH] Use named virtual threads for Jetty Closes gh-40152 --- ...ttyVirtualThreadsWebServerFactoryCustomizer.java | 2 +- ...rtualThreadsWebServerFactoryCustomizerTests.java | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/JettyVirtualThreadsWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/JettyVirtualThreadsWebServerFactoryCustomizer.java index 05720b3c82d..3ac9409be20 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/JettyVirtualThreadsWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/JettyVirtualThreadsWebServerFactoryCustomizer.java @@ -44,7 +44,7 @@ public class JettyVirtualThreadsWebServerFactoryCustomizer 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.getDefaultVirtualThreadsExecutor()); + threadPool.setVirtualThreadsExecutor(VirtualThreads.getNamedVirtualThreadsExecutor("jetty-")); factory.setThreadPool(threadPool); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/JettyVirtualThreadsWebServerFactoryCustomizerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/JettyVirtualThreadsWebServerFactoryCustomizerTests.java index 6f7fb09dd7f..c8f0baa9794 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/JettyVirtualThreadsWebServerFactoryCustomizerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/JettyVirtualThreadsWebServerFactoryCustomizerTests.java @@ -16,7 +16,13 @@ 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.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledForJreRange; import org.junit.jupiter.api.condition.JRE; @@ -47,7 +53,12 @@ class JettyVirtualThreadsWebServerFactoryCustomizerTests { then(factory).should().setThreadPool(assertArg((threadPool) -> { assertThat(threadPool).isInstanceOf(QueuedThreadPool.class); QueuedThreadPool queuedThreadPool = (QueuedThreadPool) threadPool; - assertThat(queuedThreadPool.getVirtualThreadsExecutor()).isNotNull(); + Executor executor = queuedThreadPool.getVirtualThreadsExecutor(); + assertThat(executor).isNotNull(); + AtomicReference 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-"); })); }