Skip to content

Commit

Permalink
Merge pull request #4924 from geoand/#4834
Browse files Browse the repository at this point in the history
Ensure that vert.x is closed after Arc
  • Loading branch information
stuartwdouglas authored Oct 28, 2019
2 parents 2c1f2cf + 8a9c8e8 commit bb6d667
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ public final class ShutdownContextBuildItem extends SimpleBuildItem
public void addShutdownTask(Runnable runnable) {
throw new IllegalStateException();
}

@Override
public void addLastShutdownTask(Runnable runnable) {
throw new IllegalStateException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@
public interface ShutdownContext {

void addShutdownTask(Runnable runnable);

// these are executed after all the ones add via addShutdownTask in the reverse order from which they were added
void addLastShutdownTask(Runnable runnable);
}
16 changes: 14 additions & 2 deletions core/runtime/src/main/java/io/quarkus/runtime/StartupContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ public class StartupContext implements Closeable {

private final Map<String, Object> values = new HashMap<>();
private final List<Runnable> shutdownTasks = new ArrayList<>();
private final List<Runnable> lastShutdownTasks = new ArrayList<>();
private final ShutdownContext shutdownContext = new ShutdownContext() {
@Override
public void addShutdownTask(Runnable runnable) {
shutdownTasks.add(runnable);
}

@Override
public void addLastShutdownTask(Runnable runnable) {
lastShutdownTasks.add(runnable);
}
};

public StartupContext() {
Expand All @@ -36,7 +42,14 @@ public Object getValue(String name) {

@Override
public void close() {
List<Runnable> toClose = new ArrayList<>(shutdownTasks);
runAllInReverseOrder(shutdownTasks);
shutdownTasks.clear();
runAllInReverseOrder(lastShutdownTasks);
lastShutdownTasks.clear();
}

private void runAllInReverseOrder(List<Runnable> tasks) {
List<Runnable> toClose = new ArrayList<>(tasks);
Collections.reverse(toClose);
for (Runnable r : toClose) {
try {
Expand All @@ -45,6 +58,5 @@ public void close() {
LOG.error("Running a shutdown task failed", e);
}
}
shutdownTasks.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ public Supplier<Vertx> configureVertx(BeanContainer container, VertxConfiguratio
VertxCoreProducer producer = container.instance(VertxCoreProducer.class);
producer.initialize(vertx);
if (launchMode != LaunchMode.DEVELOPMENT) {
shutdown.addShutdownTask(new Runnable() {
// we need this to be part of the last shutdown tasks because closing it early (basically before Arc)
// could cause problem to beans that rely on Vert.x and contain shutdown tasks
shutdown.addLastShutdownTask(new Runnable() {
@Override
public void run() {
destroy();
Expand Down

0 comments on commit bb6d667

Please sign in to comment.