Browse Source

Merge branch '3.4.x' into 3.5.x

Closes gh-47717
pull/47987/head
Brian Clozel 2 months ago
parent
commit
128975945b
  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 @@
package org.springframework.boot.autoconfigure.web.embedded; package org.springframework.boot.autoconfigure.web.embedded;
import org.eclipse.jetty.util.VirtualThreads; 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.autoconfigure.web.ServerProperties;
import org.springframework.boot.web.embedded.jetty.ConfigurableJettyWebServerFactory; import org.springframework.boot.web.embedded.jetty.ConfigurableJettyWebServerFactory;
@ -29,6 +29,7 @@ import org.springframework.util.Assert;
* Activates virtual threads on the {@link ConfigurableJettyWebServerFactory}. * Activates virtual threads on the {@link ConfigurableJettyWebServerFactory}.
* *
* @author Moritz Halbritter * @author Moritz Halbritter
* @author Brian Clozel
* @since 3.2.0 * @since 3.2.0
*/ */
public class JettyVirtualThreadsWebServerFactoryCustomizer public class JettyVirtualThreadsWebServerFactoryCustomizer
@ -43,9 +44,9 @@ public class JettyVirtualThreadsWebServerFactoryCustomizer
@Override @Override
public void customize(ConfigurableJettyWebServerFactory factory) { public void customize(ConfigurableJettyWebServerFactory factory) {
Assert.state(VirtualThreads.areSupported(), "Virtual threads are not supported"); Assert.state(VirtualThreads.areSupported(), "Virtual threads are not supported");
QueuedThreadPool threadPool = JettyThreadPool.create(this.serverProperties.getJetty().getThreads()); VirtualThreadPool virtualThreadPool = new VirtualThreadPool();
threadPool.setVirtualThreadsExecutor(VirtualThreads.getNamedVirtualThreadsExecutor("jetty-")); virtualThreadPool.setName("jetty-");
factory.setThreadPool(threadPool); factory.setThreadPool(virtualThreadPool);
} }
@Override @Override

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

@ -16,13 +16,7 @@
package org.springframework.boot.autoconfigure.web.embedded; package org.springframework.boot.autoconfigure.web.embedded;
import java.time.Duration; import org.eclipse.jetty.util.thread.VirtualThreadPool;
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.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange; import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE; import org.junit.jupiter.api.condition.JRE;
@ -51,14 +45,9 @@ class JettyVirtualThreadsWebServerFactoryCustomizerTests {
ConfigurableJettyWebServerFactory factory = mock(ConfigurableJettyWebServerFactory.class); ConfigurableJettyWebServerFactory factory = mock(ConfigurableJettyWebServerFactory.class);
customizer.customize(factory); customizer.customize(factory);
then(factory).should().setThreadPool(assertArg((threadPool) -> { then(factory).should().setThreadPool(assertArg((threadPool) -> {
assertThat(threadPool).isInstanceOf(QueuedThreadPool.class); assertThat(threadPool).isInstanceOf(VirtualThreadPool.class);
QueuedThreadPool queuedThreadPool = (QueuedThreadPool) threadPool; VirtualThreadPool virtualThreadPool = (VirtualThreadPool) threadPool;
Executor executor = queuedThreadPool.getVirtualThreadsExecutor(); assertThat(virtualThreadPool.getName()).isEqualTo("jetty-");
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-");
})); }));
} }

Loading…
Cancel
Save