Skip to content

Commit ff3b495

Browse files
committed
Polish 'Auto-configure rest client when virtual threads are enabled'
See gh-44952
1 parent 3686e9a commit ff3b495

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
package org.springframework.boot.autoconfigure.web.client;
1818

1919
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
20+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2021
import org.springframework.boot.autoconfigure.condition.ConditionalOnThreading;
2122
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
23+
import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration;
2224
import org.springframework.boot.autoconfigure.thread.Threading;
2325
import org.springframework.context.annotation.Conditional;
2426

@@ -28,10 +30,10 @@
2830
*
2931
* @author Dmitry Sulman
3032
*/
31-
class NotReactiveWebApplicationOrVirtualThreadsEnabledCondition extends AnyNestedCondition {
33+
class NotReactiveWebApplicationOrVirtualThreadsExecutorEnabledCondition extends AnyNestedCondition {
3234

33-
NotReactiveWebApplicationOrVirtualThreadsEnabledCondition() {
34-
super(ConfigurationPhase.PARSE_CONFIGURATION);
35+
NotReactiveWebApplicationOrVirtualThreadsExecutorEnabledCondition() {
36+
super(ConfigurationPhase.REGISTER_BEAN);
3537
}
3638

3739
@Conditional(NotReactiveWebApplicationCondition.class)
@@ -40,6 +42,7 @@ private static final class NotReactiveWebApplication {
4042
}
4143

4244
@ConditionalOnThreading(Threading.VIRTUAL)
45+
@ConditionalOnBean(name = TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME)
4346
private static final class VirtualThreadsEnabled {
4447

4548
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/client/RestClientAutoConfiguration.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
2828
import org.springframework.boot.autoconfigure.http.client.HttpClientAutoConfiguration;
2929
import org.springframework.boot.autoconfigure.ssl.SslAutoConfiguration;
30+
import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration;
3031
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
3132
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings;
3233
import org.springframework.boot.ssl.SslBundles;
@@ -51,9 +52,9 @@
5152
* @since 3.2.0
5253
*/
5354
@AutoConfiguration(after = { HttpClientAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class,
54-
SslAutoConfiguration.class })
55+
SslAutoConfiguration.class, TaskExecutionAutoConfiguration.class })
5556
@ConditionalOnClass(RestClient.class)
56-
@Conditional(NotReactiveWebApplicationOrVirtualThreadsEnabledCondition.class)
57+
@Conditional(NotReactiveWebApplicationOrVirtualThreadsExecutorEnabledCondition.class)
5758
public class RestClientAutoConfiguration {
5859

5960
@Bean

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/client/RestClientAutoConfigurationTests.java

+22-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
2828
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
2929
import org.springframework.boot.autoconfigure.http.client.HttpClientAutoConfiguration;
30+
import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration;
3031
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
3132
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings;
3233
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings.Redirects;
@@ -57,6 +58,7 @@
5758
* @author Arjen Poutsma
5859
* @author Moritz Halbritter
5960
* @author Dmytro Nosan
61+
* @author Dmitry Sulman
6062
*/
6163
class RestClientAutoConfigurationTests {
6264

@@ -287,16 +289,34 @@ void whenServletWebApplicationRestClientIsConfigured() {
287289

288290
@Test
289291
@EnabledForJreRange(min = JRE.JAVA_21)
290-
void whenReactiveWebApplicationAndVirtualThreadsAreEnabledOnJava21AndLaterRestClientIsConfigured() {
292+
void whenReactiveWebApplicationAndVirtualThreadsEnabledAndTaskExecutorBean() {
291293
new ReactiveWebApplicationContextRunner().withPropertyValues("spring.threads.virtual.enabled=true")
292-
.withConfiguration(AutoConfigurations.of(RestClientAutoConfiguration.class))
294+
.withConfiguration(
295+
AutoConfigurations.of(RestClientAutoConfiguration.class, TaskExecutionAutoConfiguration.class))
293296
.run((context) -> {
294297
assertThat(context).hasSingleBean(HttpMessageConvertersRestClientCustomizer.class);
295298
assertThat(context).hasSingleBean(RestClientBuilderConfigurer.class);
296299
assertThat(context).hasSingleBean(RestClient.Builder.class);
297300
});
298301
}
299302

303+
@Test
304+
@EnabledForJreRange(min = JRE.JAVA_21)
305+
void whenReactiveWebApplicationAndVirtualThreadsDisabled() {
306+
new ReactiveWebApplicationContextRunner().withPropertyValues("spring.threads.virtual.enabled=false")
307+
.withConfiguration(
308+
AutoConfigurations.of(RestClientAutoConfiguration.class, TaskExecutionAutoConfiguration.class))
309+
.run((context) -> assertThat(context).doesNotHaveBean(RestClient.Builder.class));
310+
}
311+
312+
@Test
313+
@EnabledForJreRange(min = JRE.JAVA_21)
314+
void whenReactiveWebApplicationAndVirtualThreadsEnabledAndNoTaskExecutorBean() {
315+
new ReactiveWebApplicationContextRunner().withPropertyValues("spring.threads.virtual.enabled=true")
316+
.withConfiguration(AutoConfigurations.of(RestClientAutoConfiguration.class))
317+
.run((context) -> assertThat(context).doesNotHaveBean(RestClient.Builder.class));
318+
}
319+
300320
@Configuration(proxyBeanMethods = false)
301321
static class CodecConfiguration {
302322

0 commit comments

Comments
 (0)