From eeb1e1fc35267f5a200284a8390dccc10fc8b355 Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Thu, 27 Jul 2023 16:30:59 +0200 Subject: [PATCH] Add VirtualThreads bean and auto-configuration This bean is only in the context if virtual threads are enabled. It can be used to get access to the virtual thread executor. --- .../autoconfigure/thread/VirtualThreads.java | 50 ++++++++++++++++ .../VirtualThreadsAutoConfiguration.java | 39 ++++++++++++ .../autoconfigure/thread/package-info.java | 20 +++++++ ...ot.autoconfigure.AutoConfiguration.imports | 3 +- .../VirtualThreadsAutoConfigurationTests.java | 60 +++++++++++++++++++ .../thread/VirtualThreadsTests.java | 47 +++++++++++++++ 6 files changed, 218 insertions(+), 1 deletion(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thread/VirtualThreads.java create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thread/VirtualThreadsAutoConfiguration.java create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thread/package-info.java create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thread/VirtualThreadsAutoConfigurationTests.java create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thread/VirtualThreadsTests.java diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thread/VirtualThreads.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thread/VirtualThreads.java new file mode 100644 index 0000000000..db4fd60b66 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thread/VirtualThreads.java @@ -0,0 +1,50 @@ +/* + * Copyright 2012-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.thread; + +import java.lang.reflect.Method; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +import org.springframework.util.Assert; +import org.springframework.util.ReflectionUtils; + +/** + * Virtual thread support. + * + * @author Moritz Halbritter + * @since 3.2.0 + */ +public class VirtualThreads { + + private final Executor executor; + + VirtualThreads() { + Method method = ReflectionUtils.findMethod(Executors.class, "newVirtualThreadPerTaskExecutor"); + Assert.notNull(method, "Executors.newVirtualThreadPerTaskExecutor() method is missing"); + this.executor = (Executor) ReflectionUtils.invokeMethod(method, null); + } + + /** + * Returns the virtual thread executor. + * @return the virtual thread executor + */ + public Executor getExecutor() { + return this.executor; + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thread/VirtualThreadsAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thread/VirtualThreadsAutoConfiguration.java new file mode 100644 index 0000000000..b2936490e4 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thread/VirtualThreadsAutoConfiguration.java @@ -0,0 +1,39 @@ +/* + * Copyright 2012-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.thread; + +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnVirtualThreads; +import org.springframework.context.annotation.Bean; + +/** + * {@link EnableAutoConfiguration Auto-configuration} for virtual threads. + * + * @author Moritz Halbritter + * @since 3.2.0 + */ +@AutoConfiguration +@ConditionalOnVirtualThreads +public class VirtualThreadsAutoConfiguration { + + @Bean + VirtualThreads virtualThreads() { + return new VirtualThreads(); + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thread/package-info.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thread/package-info.java new file mode 100644 index 0000000000..61c141a651 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thread/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2012-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Classes related to threads. + */ +package org.springframework.boot.autoconfigure.thread; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 5997fb8ea2..46be5e5216 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -119,6 +119,7 @@ org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfigurati org.springframework.boot.autoconfigure.ssl.SslAutoConfiguration org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration +org.springframework.boot.autoconfigure.thread.VirtualThreadsAutoConfiguration org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration @@ -144,4 +145,4 @@ org.springframework.boot.autoconfigure.websocket.reactive.WebSocketReactiveAutoC org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration org.springframework.boot.autoconfigure.websocket.servlet.WebSocketMessagingAutoConfiguration org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration -org.springframework.boot.autoconfigure.webservices.client.WebServiceTemplateAutoConfiguration \ No newline at end of file +org.springframework.boot.autoconfigure.webservices.client.WebServiceTemplateAutoConfiguration diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thread/VirtualThreadsAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thread/VirtualThreadsAutoConfigurationTests.java new file mode 100644 index 0000000000..12d80789f9 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thread/VirtualThreadsAutoConfigurationTests.java @@ -0,0 +1,60 @@ +/* + * Copyright 2012-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.thread; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledForJreRange; +import org.junit.jupiter.api.condition.JRE; + +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.context.annotation.ImportCandidates; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link VirtualThreadsAutoConfiguration}. + * + * @author Moritz Halbritter + */ +class VirtualThreadsAutoConfigurationTests { + + private final ApplicationContextRunner runner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(VirtualThreadsAutoConfiguration.class)); + + @Test + void shouldBeRegisteredInAutoConfigurationImports() { + assertThat(ImportCandidates.load(AutoConfiguration.class, null).getCandidates()) + .contains(VirtualThreadsAutoConfiguration.class.getName()); + } + + @Test + @EnabledForJreRange(min = JRE.JAVA_21) + void shouldSupplyBeans() { + this.runner.withPropertyValues("spring.threads.virtual.enabled=true") + .run((context) -> assertThat(context).hasSingleBean(VirtualThreads.class)); + } + + @Test + @EnabledForJreRange(min = JRE.JAVA_21) + void shouldNotSupplyBeansIfVirtualThreadsAreNotEnabled() { + this.runner.withPropertyValues("spring.threads.virtual.enabled=false") + .run((context) -> assertThat(context).doesNotHaveBean(VirtualThreads.class)); + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thread/VirtualThreadsTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thread/VirtualThreadsTests.java new file mode 100644 index 0000000000..9f2bf4c40d --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thread/VirtualThreadsTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2012-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.thread; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledForJreRange; +import org.junit.jupiter.api.condition.JRE; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +/** + * Tests for {@link VirtualThreads}. + * + * @author Moritz Halbritter + */ +class VirtualThreadsTests { + + @Test + @EnabledForJreRange(max = JRE.JAVA_20) + void shouldThrowExceptionBelowJava21() { + assertThatThrownBy(VirtualThreads::new).isInstanceOf(IllegalArgumentException.class) + .hasMessage("Executors.newVirtualThreadPerTaskExecutor() method is missing"); + } + + @Test + @EnabledForJreRange(min = JRE.JAVA_21) + void shouldReturnExecutorOnJava21AndUp() { + VirtualThreads virtualThreads = new VirtualThreads(); + assertThat(virtualThreads.getExecutor()).isNotNull(); + } + +}