Skip to content

Commit

Permalink
Fix exception in logs on live reload
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Jul 9, 2020
1 parent 4fa78b0 commit b0c5120
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

import java.io.File;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -51,6 +54,12 @@ public class VertxCoreRecorder {

static volatile int blockingThreadPoolSize;

/**
* This is a bit of a hack. In dev mode we undeploy all the verticles on restart, except
* for this one
*/
private static volatile String webDeploymentId;

public Supplier<Vertx> configureVertx(VertxConfiguration config,
LaunchMode launchMode, ShutdownContext shutdown, List<Consumer<VertxOptions>> customizers) {
if (launchMode != LaunchMode.DEVELOPMENT) {
Expand All @@ -72,19 +81,28 @@ public void run() {
shutdown.addLastShutdownTask(new Runnable() {
@Override
public void run() {
CountDownLatch latch = new CountDownLatch(1);
List<CountDownLatch> latches = new ArrayList<>();
if (vertx.v != null) {
vertx.v.eventBus().close(new Handler<AsyncResult<Void>>() {
@Override
public void handle(AsyncResult<Void> event) {
vertx.v.eventBus().start(new Handler<AsyncResult<Void>>() {
Set<String> ids = new HashSet<>(vertx.v.deploymentIDs());
for (String id : ids) {
if (!id.equals(webDeploymentId)) {
CountDownLatch latch = new CountDownLatch(1);
latches.add(latch);
vertx.v.undeploy(id, new Handler<AsyncResult<Void>>() {
@Override
public void handle(AsyncResult<Void> event) {
latch.countDown();
}
});
}
});
}
for (CountDownLatch latch : latches) {
try {
latch.await();
} catch (InterruptedException e) {
LOGGER.error("Failed waiting for verticle undeploy", e);
}
}
}
}
});
Expand Down Expand Up @@ -391,4 +409,8 @@ VertxOptions customize(VertxOptions options) {
return options;
}
}

public static void setWebDeploymentId(String webDeploymentId) {
VertxCoreRecorder.webDeploymentId = webDeploymentId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ public void handle(AsyncResult<String> event) {
try {

String deploymentId = futureResult.get();
VertxCoreRecorder.setWebDeploymentId(deploymentId);
closeTask = new Runnable() {
@Override
public synchronized void run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.eventbus.MessageConsumer;

public class MyVerticle extends AbstractVerticle {

private final String id = UUID.randomUUID().toString();
private volatile MessageConsumer<Object> messageConsumer;

@Override
public void start(Future<Void> done) {
vertx.eventBus().consumer("address")
.handler(m -> m.reply("ok-" + id))
messageConsumer = vertx.eventBus().consumer("address")
.handler(m -> m.reply("ok-" + id));
messageConsumer
.completionHandler(ar -> done.handle(ar.mapEmpty()));
}

@Override
public void stop() throws Exception {
messageConsumer.unregister();
}
}

0 comments on commit b0c5120

Please sign in to comment.