Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exception in logs on live reload #10595

Merged
merged 1 commit into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -1,20 +1,28 @@
package io.quarkus.vertx.http.hotreload;

import javax.annotation.PreDestroy;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.inject.Inject;

import io.quarkus.runtime.StartupEvent;
import io.vertx.core.Vertx;
import io.vertx.core.eventbus.MessageConsumer;

@ApplicationScoped
public class VertxEventBusConsumer {

@Inject
Vertx vertx;
private MessageConsumer<Object> messageConsumer;

public void init(@Observes StartupEvent event) {
vertx.eventBus().consumer("my-address", m -> m.reply("hello"));
messageConsumer = vertx.eventBus().consumer("my-address", m -> m.reply("hello"));
}

@PreDestroy
public void stop() {
messageConsumer.unregister();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public class VertxEventBusProducer {
Vertx vertx;

public void register(@Observes StartupEvent ev) {
// Don't use rc.vertx() as it would be the RestEasy instance
router.get("/").handler(rc -> vertx.eventBus().<String> request("my-address", "", m -> {
if (m.failed()) {
rc.response().end("failed");
Expand Down
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();
}
}