Skip to content

Commit cff1b33

Browse files
committed
Configure virtual threads on Undertow if enabled
Closes gh-38819
1 parent 653474f commit cff1b33

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,15 +34,18 @@
3434
import org.springframework.boot.autoconfigure.thread.Threading;
3535
import org.springframework.boot.autoconfigure.web.ServerProperties;
3636
import org.springframework.boot.context.properties.EnableConfigurationProperties;
37+
import org.springframework.boot.web.embedded.undertow.UndertowDeploymentInfoCustomizer;
3738
import org.springframework.context.annotation.Bean;
3839
import org.springframework.context.annotation.Configuration;
3940
import org.springframework.core.env.Environment;
41+
import org.springframework.core.task.VirtualThreadTaskExecutor;
4042

4143
/**
4244
* {@link EnableAutoConfiguration Auto-configuration} for embedded servlet and reactive
4345
* web servers customizations.
4446
*
4547
* @author Phillip Webb
48+
* @author Moritz Halbritter
4649
* @since 2.0.0
4750
*/
4851
@AutoConfiguration
@@ -107,6 +110,12 @@ public UndertowWebServerFactoryCustomizer undertowWebServerFactoryCustomizer(Env
107110
return new UndertowWebServerFactoryCustomizer(environment, serverProperties);
108111
}
109112

113+
@Bean
114+
@ConditionalOnThreading(Threading.VIRTUAL)
115+
UndertowDeploymentInfoCustomizer virtualThreadsUndertowDeploymentInfoCustomizer() {
116+
return (deploymentInfo) -> deploymentInfo.setExecutor(new VirtualThreadTaskExecutor("undertow-"));
117+
}
118+
110119
}
111120

112121
/**

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -77,8 +77,7 @@ public int getOrder() {
7777
public void customize(ConfigurableUndertowWebServerFactory factory) {
7878
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
7979
ServerOptions options = new ServerOptions(factory);
80-
ServerProperties properties = this.serverProperties;
81-
map.from(properties::getMaxHttpRequestHeaderSize)
80+
map.from(this.serverProperties::getMaxHttpRequestHeaderSize)
8281
.asInt(DataSize::toBytes)
8382
.when(this::isPositive)
8483
.to(options.option(UndertowOptions.MAX_HEADER_SIZE));
@@ -164,6 +163,7 @@ private abstract static class AbstractOptions {
164163
lookup.put(getCanonicalName(field.getName()), option);
165164
}
166165
catch (IllegalAccessException ex) {
166+
// Ignore
167167
}
168168
}
169169
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2012-2024 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 io.undertow.servlet.api.DeploymentInfo;
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.AutoConfigurations;
25+
import org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration.UndertowWebServerFactoryCustomizerConfiguration;
26+
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
27+
import org.springframework.boot.web.embedded.undertow.UndertowDeploymentInfoCustomizer;
28+
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
29+
import org.springframework.core.task.VirtualThreadTaskExecutor;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
/**
34+
* Tests for {@link UndertowWebServerFactoryCustomizerConfiguration}.
35+
*
36+
* @author Moritz Halbritter
37+
*/
38+
class UndertowWebServerFactoryCustomizerConfigurationTests {
39+
40+
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner(
41+
AnnotationConfigServletWebApplicationContext::new)
42+
.withConfiguration(AutoConfigurations.of(EmbeddedWebServerFactoryCustomizerAutoConfiguration.class));
43+
44+
@EnabledForJreRange(min = JRE.JAVA_21)
45+
@Test
46+
void shouldUseVirtualThreadsIfEnabled() {
47+
this.contextRunner.withPropertyValues("spring.threads.virtual.enabled=true").run((context) -> {
48+
assertThat(context).hasSingleBean(UndertowDeploymentInfoCustomizer.class);
49+
assertThat(context).hasBean("virtualThreadsUndertowDeploymentInfoCustomizer");
50+
UndertowDeploymentInfoCustomizer customizer = context.getBean(UndertowDeploymentInfoCustomizer.class);
51+
DeploymentInfo deploymentInfo = new DeploymentInfo();
52+
customizer.customize(deploymentInfo);
53+
assertThat(deploymentInfo.getExecutor()).isInstanceOf(VirtualThreadTaskExecutor.class);
54+
});
55+
}
56+
57+
}

0 commit comments

Comments
 (0)