Skip to content

Commit

Permalink
Transition VirtualThreadsConfig to use @ConfigMapping.
Browse files Browse the repository at this point in the history
- Remove legacy configuration compiler arguments from Maven POM files to streamline build configuration.
  • Loading branch information
gastaldi committed Jan 21, 2025
1 parent 77a710b commit d19c10f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 29 deletions.
3 changes: 0 additions & 3 deletions extensions/virtual-threads/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AlegacyConfigRoot=true</arg>
</compilerArgs>
</configuration>
</execution>
</executions>
Expand Down
3 changes: 0 additions & 3 deletions extensions/virtual-threads/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AlegacyConfigRoot=true</arg>
</compilerArgs>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,29 @@
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;

@ConfigRoot(phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
public class VirtualThreadsConfig {
@ConfigMapping(prefix = "quarkus.virtual-threads")
public interface VirtualThreadsConfig {

/**
* Virtual thread name prefix. If left blank virtual threads will be unnamed.
*/
@ConfigItem(defaultValue = "quarkus-virtual-thread-")
Optional<String> namePrefix;
Optional<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<Duration> shutdownCheckInterval;
Optional<Duration> shutdownCheckInterval();

/**
* A flag to explicitly disabled virtual threads, even if the JVM support them.
Expand All @@ -37,6 +34,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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -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().orElse(config.shutdownTimeout()).toNanos();

long start = System.nanoTime();
int loop = 1;
Expand Down Expand Up @@ -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().orElse(null);
return new ContextPreservingExecutorService(newVirtualThreadPerTaskExecutorWithName(prefix));
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException | ClassNotFoundException e) {
logger.debug("Unable to invoke java.util.concurrent.Executors#newVirtualThreadPerTaskExecutor", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit d19c10f

Please sign in to comment.