use of io.vertx.core.eventbus.EventBus in project vert.x by eclipse.
the class EventBusTestBase method testSendFromExecuteBlocking.
@Test
public void testSendFromExecuteBlocking() throws Exception {
String expectedBody = TestUtils.randomAlphaString(20);
CountDownLatch receivedLatch = new CountDownLatch(1);
startNodes(2);
vertices[1].eventBus().<String>consumer(ADDRESS1, msg -> {
assertEquals(expectedBody, msg.body());
receivedLatch.countDown();
}).completionHandler(ar -> {
assertTrue(ar.succeeded());
vertices[0].executeBlocking(fut -> {
vertices[0].eventBus().send(ADDRESS1, expectedBody);
try {
awaitLatch(receivedLatch);
} catch (InterruptedException e) {
Thread.interrupted();
fut.fail(e);
}
fut.complete();
}, ar2 -> {
if (ar2.succeeded()) {
testComplete();
} else {
fail(ar2.cause());
}
});
});
await();
}
use of io.vertx.core.eventbus.EventBus in project vert.x by eclipse.
the class EventBusTestBase method testNoHandlersCallbackContext.
@Test
public void testNoHandlersCallbackContext() {
startNodes(2);
waitFor(4);
// On an "external" thread
vertices[0].eventBus().send("blah", "blah", ar -> {
assertTrue(ar.failed());
if (ar.cause() instanceof ReplyException) {
ReplyException cause = (ReplyException) ar.cause();
assertSame(ReplyFailure.NO_HANDLERS, cause.failureType());
} else {
fail(ar.cause());
}
assertTrue("Not an EL thread", Context.isOnEventLoopThread());
complete();
});
// On a EL context
vertices[0].runOnContext(v -> {
Context ctx = vertices[0].getOrCreateContext();
vertices[0].eventBus().send("blah", "blah", ar -> {
assertTrue(ar.failed());
if (ar.cause() instanceof ReplyException) {
ReplyException cause = (ReplyException) ar.cause();
assertSame(ReplyFailure.NO_HANDLERS, cause.failureType());
} else {
fail(ar.cause());
}
assertSame(ctx, vertices[0].getOrCreateContext());
complete();
});
});
// On a Worker context
vertices[0].deployVerticle(new AbstractVerticle() {
@Override
public void start() throws Exception {
Context ctx = getVertx().getOrCreateContext();
vertices[0].eventBus().send("blah", "blah", ar -> {
assertTrue(ar.failed());
if (ar.cause() instanceof ReplyException) {
ReplyException cause = (ReplyException) ar.cause();
assertSame(ReplyFailure.NO_HANDLERS, cause.failureType());
} else {
fail(ar.cause());
}
assertSame(ctx, getVertx().getOrCreateContext());
complete();
});
}
}, new DeploymentOptions().setWorker(true));
// Inside executeBlocking
vertices[0].executeBlocking(fut -> {
vertices[0].eventBus().send("blah", "blah", ar -> {
assertTrue(ar.failed());
if (ar.cause() instanceof ReplyException) {
ReplyException cause = (ReplyException) ar.cause();
assertSame(ReplyFailure.NO_HANDLERS, cause.failureType());
} else {
fail(ar.cause());
}
assertTrue("Not an EL thread", Context.isOnEventLoopThread());
complete();
});
fut.complete();
}, false, null);
await();
}
use of io.vertx.core.eventbus.EventBus in project vert.x by eclipse.
the class EventBusTestBase method testReplyFromWorker.
@Test
public void testReplyFromWorker() throws Exception {
String expectedBody = TestUtils.randomAlphaString(20);
startNodes(2);
CountDownLatch latch = new CountDownLatch(1);
vertices[0].deployVerticle(new AbstractVerticle() {
@Override
public void start() throws Exception {
vertices[1].eventBus().<String>consumer(ADDRESS1, msg -> {
msg.reply(expectedBody);
}).completionHandler(ar -> {
assertTrue(ar.succeeded());
latch.countDown();
});
}
}, new DeploymentOptions().setWorker(true));
awaitLatch(latch);
vertices[0].eventBus().send(ADDRESS1, "whatever", reply -> {
assertTrue(reply.succeeded());
assertEquals(expectedBody, reply.result().body());
testComplete();
});
await();
}
use of io.vertx.core.eventbus.EventBus in project vert.x by eclipse.
the class EventBusExamples method example12.
public void example12() {
VertxOptions options = new VertxOptions();
Vertx.clusteredVertx(options, res -> {
if (res.succeeded()) {
Vertx vertx = res.result();
EventBus eventBus = vertx.eventBus();
System.out.println("We now have a clustered event bus: " + eventBus);
} else {
System.out.println("Failed: " + res.cause());
}
});
}
use of io.vertx.core.eventbus.EventBus in project vert.x by eclipse.
the class MetricsContextTest method testMessageHandler.
private void testMessageHandler(BiConsumer<Vertx, Handler<Void>> runOnContext, BiConsumer<Thread, Context> checker) {
AtomicReference<Thread> consumerThread = new AtomicReference<>();
AtomicReference<Context> consumerContext = new AtomicReference<>();
AtomicBoolean registeredCalled = new AtomicBoolean();
AtomicBoolean unregisteredCalled = new AtomicBoolean();
AtomicBoolean beginHandleCalled = new AtomicBoolean();
AtomicBoolean endHandleCalled = new AtomicBoolean();
VertxMetricsFactory factory = (vertx, options) -> new DummyVertxMetrics() {
@Override
public EventBusMetrics createMetrics(EventBus eventBus) {
return new DummyEventBusMetrics() {
@Override
public boolean isEnabled() {
return true;
}
@Override
public Void handlerRegistered(String address, String repliedAddress) {
registeredCalled.set(true);
return null;
}
@Override
public void handlerUnregistered(Void handler) {
unregisteredCalled.set(true);
}
@Override
public void beginHandleMessage(Void handler, boolean local) {
consumerThread.set(Thread.currentThread());
consumerContext.set(Vertx.currentContext());
beginHandleCalled.set(true);
}
@Override
public void endHandleMessage(Void handler, Throwable failure) {
endHandleCalled.set(true);
checker.accept(consumerThread.get(), consumerContext.get());
}
};
}
};
Vertx vertx = vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory)));
EventBus eb = vertx.eventBus();
runOnContext.accept(vertx, v -> {
MessageConsumer<Object> consumer = eb.consumer("the_address");
consumer.handler(msg -> {
checker.accept(consumerThread.get(), consumerContext.get());
executeInVanillaThread(() -> {
vertx.getOrCreateContext().runOnContext(v2 -> {
consumer.unregister(onSuccess(v3 -> {
assertTrue(registeredCalled.get());
assertTrue(beginHandleCalled.get());
assertTrue(endHandleCalled.get());
waitUntil(() -> unregisteredCalled.get());
testComplete();
}));
});
});
}).completionHandler(onSuccess(v2 -> {
eb.send("the_address", "the_msg");
}));
});
await();
}
Aggregations