diff --git a/api/src/main/java/com/microsoft/gctoolkit/aggregator/Aggregator.java b/api/src/main/java/com/microsoft/gctoolkit/aggregator/Aggregator.java index ce11ab10..76c70431 100644 --- a/api/src/main/java/com/microsoft/gctoolkit/aggregator/Aggregator.java +++ b/api/src/main/java/com/microsoft/gctoolkit/aggregator/Aggregator.java @@ -6,6 +6,7 @@ import com.microsoft.gctoolkit.event.jvm.JVMTermination; import java.util.Arrays; +import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.function.Consumer; @@ -117,13 +118,22 @@ public void onCompletion(Runnable task) { this.completionTask = task; } + private static final ExecutorService executorService = + Executors.newSingleThreadExecutor(runnable -> { + // Use a daemon thread for executing the completion task; otherwise, the JVM will not exit. + Thread thread = new Thread(runnable, "aggregator-completion"); + thread.setDaemon(true); + return thread; + } + ); + /** * Call a callback when aggregation is completed. */ private void complete() { - if (completionTask != null) - Executors.newSingleThreadExecutor().execute(completionTask); - + if (completionTask != null) { + executorService.execute(completionTask); + } } /** diff --git a/vertx/src/main/java/com/microsoft/gctoolkit/vertx/DataSourceVerticle.java b/vertx/src/main/java/com/microsoft/gctoolkit/vertx/DataSourceVerticle.java index 922ce05c..ce1ad57e 100644 --- a/vertx/src/main/java/com/microsoft/gctoolkit/vertx/DataSourceVerticle.java +++ b/vertx/src/main/java/com/microsoft/gctoolkit/vertx/DataSourceVerticle.java @@ -43,11 +43,6 @@ public void start(Promise promise) { } } - @Override - public void stop(Promise promise) { - promise.complete(); - } - @Override public boolean equals(Object other) { // we want Object.equals(other) because it's ok to have more than 1 AggregatorEngine on the bus diff --git a/vertx/src/main/java/com/microsoft/gctoolkit/vertx/JVMEventVerticle.java b/vertx/src/main/java/com/microsoft/gctoolkit/vertx/JVMEventVerticle.java index 1c8cde02..7df0ef6c 100644 --- a/vertx/src/main/java/com/microsoft/gctoolkit/vertx/JVMEventVerticle.java +++ b/vertx/src/main/java/com/microsoft/gctoolkit/vertx/JVMEventVerticle.java @@ -45,11 +45,6 @@ public void start(Promise promise) { }).completionHandler(result -> {promise.complete();}); } - @Override - public void stop(Promise promise) { - promise.complete(); - } - @Override public boolean equals(Object other) { // we want Object.equals(other) because it's ok to have more than 1 AggregatorEngine on the bus