From 33636685b6407bfcc4cc3ec4b269bbdfb8b42374 Mon Sep 17 00:00:00 2001 From: George Gastaldi Date: Tue, 21 Jan 2025 10:28:16 -0300 Subject: [PATCH] Transition `VirtualThreadsConfig` to use `@ConfigMapping` - Remove legacy configuration compiler arguments from Maven POM files to streamline build configuration. --- extensions/virtual-threads/deployment/pom.xml | 3 --- extensions/virtual-threads/runtime/pom.xml | 3 --- .../virtual/threads/VirtualThreadsConfig.java | 27 ++++++++++--------- .../threads/VirtualThreadsRecorder.java | 12 ++++----- .../VirtualThreadExecutorSupplierTest.java | 9 ++++--- 5 files changed, 25 insertions(+), 29 deletions(-) diff --git a/extensions/virtual-threads/deployment/pom.xml b/extensions/virtual-threads/deployment/pom.xml index c5c58e98b1f7d..23f0afc4dae7c 100644 --- a/extensions/virtual-threads/deployment/pom.xml +++ b/extensions/virtual-threads/deployment/pom.xml @@ -43,9 +43,6 @@ ${project.version} - - -AlegacyConfigRoot=true - diff --git a/extensions/virtual-threads/runtime/pom.xml b/extensions/virtual-threads/runtime/pom.xml index d01ec6de6546c..693d72542e6b9 100644 --- a/extensions/virtual-threads/runtime/pom.xml +++ b/extensions/virtual-threads/runtime/pom.xml @@ -60,9 +60,6 @@ ${project.version} - - -AlegacyConfigRoot=true - diff --git a/extensions/virtual-threads/runtime/src/main/java/io/quarkus/virtual/threads/VirtualThreadsConfig.java b/extensions/virtual-threads/runtime/src/main/java/io/quarkus/virtual/threads/VirtualThreadsConfig.java index 66ae8c1efba9b..2ba7b14f445d6 100644 --- a/extensions/virtual-threads/runtime/src/main/java/io/quarkus/virtual/threads/VirtualThreadsConfig.java +++ b/extensions/virtual-threads/runtime/src/main/java/io/quarkus/virtual/threads/VirtualThreadsConfig.java @@ -1,34 +1,35 @@ package io.quarkus.virtual.threads; import java.time.Duration; -import java.util.Optional; -import io.quarkus.runtime.annotations.ConfigItem; import io.quarkus.runtime.annotations.ConfigPhase; import io.quarkus.runtime.annotations.ConfigRoot; +import io.smallrye.config.ConfigMapping; +import io.smallrye.config.WithDefault; +@ConfigMapping(prefix = "quarkus.virtual-threads") @ConfigRoot(phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED) -public class VirtualThreadsConfig { +public interface VirtualThreadsConfig { /** - * Virtual thread name prefix. If left blank virtual threads will be unnamed. + * Virtual thread name prefix. The name of the virtual thread will be the prefix followed by a unique number. + * The default value is "quarkus-virtual-thread-". */ - @ConfigItem(defaultValue = "quarkus-virtual-thread-") - Optional namePrefix; + @WithDefault("quarkus-virtual-thread-") + String namePrefix(); /** * The shutdown timeout. If all pending work has not been completed by this time * then any pending tasks will be interrupted, and the shutdown process will continue */ - @ConfigItem(defaultValue = "1M") - public Duration shutdownTimeout; + @WithDefault("1M") + Duration shutdownTimeout(); /** * The frequency at which the status of the executor service should be checked during shutdown. - * Setting this key to an empty value disables the shutdown check interval. */ - @ConfigItem(defaultValue = "5s") - public Optional shutdownCheckInterval; + @WithDefault("5s") + Duration shutdownCheckInterval(); /** * A flag to explicitly disabled virtual threads, even if the JVM support them. @@ -37,6 +38,6 @@ public class VirtualThreadsConfig { * This flag is intended to be used when running with virtual threads become more expensive than plain worker threads, * because of pinning, monopolization or thread-based object pool. */ - @ConfigItem(defaultValue = "true") - public boolean enabled; + @WithDefault("true") + boolean enabled(); } diff --git a/extensions/virtual-threads/runtime/src/main/java/io/quarkus/virtual/threads/VirtualThreadsRecorder.java b/extensions/virtual-threads/runtime/src/main/java/io/quarkus/virtual/threads/VirtualThreadsRecorder.java index 46565e8a9eddd..3311506579fb6 100644 --- a/extensions/virtual-threads/runtime/src/main/java/io/quarkus/virtual/threads/VirtualThreadsRecorder.java +++ b/extensions/virtual-threads/runtime/src/main/java/io/quarkus/virtual/threads/VirtualThreadsRecorder.java @@ -20,7 +20,7 @@ public class VirtualThreadsRecorder { private static final Logger logger = Logger.getLogger("io.quarkus.virtual-threads"); - static VirtualThreadsConfig config = new VirtualThreadsConfig(); + static volatile VirtualThreadsConfig config; private static volatile ExecutorService current; private static final Object lock = new Object(); @@ -34,7 +34,7 @@ public ExecutorService get() { public void setupVirtualThreads(VirtualThreadsConfig c, ShutdownContext shutdownContext, LaunchMode launchMode) { config = c; - if (config.enabled) { + if (config.enabled()) { if (launchMode == LaunchMode.DEVELOPMENT) { shutdownContext.addLastShutdownTask(new Runnable() { @Override @@ -55,8 +55,8 @@ public void run() { if (service != null) { service.shutdown(); - final long timeout = config.shutdownTimeout.toNanos(); - final long interval = config.shutdownCheckInterval.orElse(config.shutdownTimeout).toNanos(); + final long timeout = config.shutdownTimeout().toNanos(); + final long interval = config.shutdownCheckInterval().toNanos(); long start = System.nanoTime(); int loop = 1; @@ -132,9 +132,9 @@ public void uncaughtException(Thread t, Throwable e) { * change --release, --source, --target flags and to enable previews. */ private static ExecutorService createExecutor() { - if (config.enabled) { + if (config.enabled()) { try { - String prefix = config.namePrefix.orElse(null); + String prefix = config.namePrefix(); return new ContextPreservingExecutorService(newVirtualThreadPerTaskExecutorWithName(prefix)); } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException | ClassNotFoundException e) { logger.debug("Unable to invoke java.util.concurrent.Executors#newVirtualThreadPerTaskExecutor", e); diff --git a/extensions/virtual-threads/runtime/src/test/java/io/quarkus/virtual/threads/VirtualThreadExecutorSupplierTest.java b/extensions/virtual-threads/runtime/src/test/java/io/quarkus/virtual/threads/VirtualThreadExecutorSupplierTest.java index 74b5a49c48fab..6d11ed4a42451 100644 --- a/extensions/virtual-threads/runtime/src/test/java/io/quarkus/virtual/threads/VirtualThreadExecutorSupplierTest.java +++ b/extensions/virtual-threads/runtime/src/test/java/io/quarkus/virtual/threads/VirtualThreadExecutorSupplierTest.java @@ -6,7 +6,6 @@ import java.lang.reflect.Method; import java.time.Duration; import java.util.List; -import java.util.Optional; import java.util.concurrent.Callable; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; @@ -19,6 +18,7 @@ import org.junit.jupiter.api.condition.EnabledForJreRange; import org.junit.jupiter.api.condition.JRE; +import io.smallrye.config.SmallRyeConfigBuilder; import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.helpers.test.UniAssertSubscriber; import io.vertx.core.Context; @@ -28,9 +28,10 @@ class VirtualThreadExecutorSupplierTest { @BeforeEach void configRecorder() { - VirtualThreadsRecorder.config = new VirtualThreadsConfig(); - VirtualThreadsRecorder.config.enabled = true; - VirtualThreadsRecorder.config.namePrefix = Optional.empty(); + VirtualThreadsRecorder.config = new SmallRyeConfigBuilder() + .addDiscoveredConverters() + .withMapping(VirtualThreadsConfig.class) + .build().getConfigMapping(VirtualThreadsConfig.class); } @Test