use of io.vertx.core.eventbus.EventBus in project vert.x by eclipse.
the class MetricsTest method testReplyFailureTimeout1.
@Test
public void testReplyFailureTimeout1() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
EventBus eb = vertx.eventBus();
FakeEventBusMetrics metrics = FakeMetricsBase.getMetrics(eb);
eb.consumer(ADDRESS1, msg -> {
});
eb.send(ADDRESS1, "bar", new DeliveryOptions().setSendTimeout(10), ar -> {
assertTrue(ar.failed());
latch.countDown();
});
awaitLatch(latch);
assertEquals(1, metrics.getReplyFailureAddresses().size());
assertEquals(Collections.singletonList(ReplyFailure.TIMEOUT), metrics.getReplyFailures());
}
use of io.vertx.core.eventbus.EventBus in project vert.x by eclipse.
the class EventBusTestBase method testSendWhileUnsubscribing.
@Test
public void testSendWhileUnsubscribing() throws Exception {
startNodes(2);
AtomicBoolean unregistered = new AtomicBoolean();
Verticle sender = new AbstractVerticle() {
@Override
public void start() throws Exception {
getVertx().runOnContext(v -> sendMsg());
}
private void sendMsg() {
if (!unregistered.get()) {
getVertx().eventBus().send("whatever", "marseille");
burnCpu();
getVertx().runOnContext(v -> sendMsg());
} else {
getVertx().eventBus().send("whatever", "marseille", ar -> {
Throwable cause = ar.cause();
assertThat(cause, instanceOf(ReplyException.class));
ReplyException replyException = (ReplyException) cause;
assertEquals(ReplyFailure.NO_HANDLERS, replyException.failureType());
testComplete();
});
}
}
};
Verticle receiver = new AbstractVerticle() {
boolean unregisterCalled;
@Override
public void start(Future<Void> startFuture) throws Exception {
EventBus eventBus = getVertx().eventBus();
MessageConsumer<String> consumer = eventBus.consumer("whatever");
consumer.handler(m -> {
if (!unregisterCalled) {
consumer.unregister(v -> unregistered.set(true));
unregisterCalled = true;
}
m.reply("ok");
}).completionHandler(startFuture);
}
};
CountDownLatch deployLatch = new CountDownLatch(1);
vertices[0].exceptionHandler(this::fail).deployVerticle(receiver, onSuccess(receiverId -> {
vertices[1].exceptionHandler(this::fail).deployVerticle(sender, onSuccess(senderId -> {
deployLatch.countDown();
}));
}));
awaitLatch(deployLatch);
await();
CountDownLatch closeLatch = new CountDownLatch(2);
vertices[0].close(v -> closeLatch.countDown());
vertices[1].close(v -> closeLatch.countDown());
awaitLatch(closeLatch);
}
use of io.vertx.core.eventbus.EventBus in project vert.x by eclipse.
the class VertxImpl method close.
@Override
public synchronized void close(Handler<AsyncResult<Void>> completionHandler) {
if (closed || eventBus == null) {
// Just call the handler directly since pools shutdown
if (completionHandler != null) {
completionHandler.handle(Future.succeededFuture());
}
return;
}
closed = true;
closeHooks.run(ar -> {
deploymentManager.undeployAll(ar1 -> {
if (haManager() != null) {
haManager().stop();
}
addressResolver.close(ar2 -> {
eventBus.close(ar3 -> {
closeClusterManager(ar4 -> {
Set<HttpServer> httpServers = new HashSet<>(sharedHttpServers.values());
Set<NetServerBase> netServers = new HashSet<>(sharedNetServers.values());
sharedHttpServers.clear();
sharedNetServers.clear();
int serverCount = httpServers.size() + netServers.size();
AtomicInteger serverCloseCount = new AtomicInteger();
Handler<AsyncResult<Void>> serverCloseHandler = res -> {
if (res.failed()) {
log.error("Failure in shutting down server", res.cause());
}
if (serverCloseCount.incrementAndGet() == serverCount) {
deleteCacheDirAndShutdown(completionHandler);
}
};
for (HttpServer server : httpServers) {
server.close(serverCloseHandler);
}
for (NetServerBase server : netServers) {
server.close(serverCloseHandler);
}
if (serverCount == 0) {
deleteCacheDirAndShutdown(completionHandler);
}
});
});
});
});
});
}
use of io.vertx.core.eventbus.EventBus in project vertx-tcp-eventbus-bridge by vert-x3.
the class TcpEventBusBridgeInteropTest method testInteropWithPlainJava.
@Test
public void testInteropWithPlainJava(TestContext context) {
final Async async = context.async();
// register a local consumer
final EventBus eb = vertx.eventBus();
eb.consumer("io.vertx", msg -> {
// assert that headers are received
context.assertEquals("findAll", msg.headers().get("action"));
// assert body is not null
context.assertNotNull(msg.body());
msg.reply(msg.body());
});
// run some plain java (blocking)
vertx.executeBlocking(f -> {
try {
JsonObject headers = new JsonObject();
headers.put("action", "findAll");
JsonObject body = new JsonObject();
body.put("model", "news");
JsonObject protocol = new JsonObject();
protocol.put("type", "send");
protocol.put("headers", headers);
protocol.put("body", body);
protocol.put("address", "io.vertx");
protocol.put("replyAddress", "durp");
Buffer buffer = Buffer.buffer();
buffer.appendInt(protocol.encode().getBytes().length);
buffer.appendString(protocol.encode());
Socket clientSocket = new Socket("localhost", 7000);
DataOutputStream output = new DataOutputStream(clientSocket.getOutputStream());
output.write(buffer.getBytes());
DataInputStream input = new DataInputStream(clientSocket.getInputStream());
int bytesLength = input.readInt();
byte[] bytes = new byte[bytesLength];
for (int i = 0; i < bytesLength; i++) {
bytes[i] = input.readByte();
}
input.close();
output.close();
clientSocket.close();
JsonObject reply = new JsonObject(new String(bytes));
// assert that the body is the same we sent
context.assertEquals(body, reply.getJsonObject("body"));
f.complete();
} catch (IOException e) {
f.fail(e);
}
}, res -> {
context.assertTrue(res.succeeded());
async.complete();
});
}
use of io.vertx.core.eventbus.EventBus in project Summer by yale8848.
the class TestService method test.
public SummerResponse test(Vertx vertx, RoutingContext routingContext) {
EventBus eb = vertx.eventBus();
EventMessage eventMessage = EventMessage.message(null);
eventMessage.setKey("68257");
return SummerResponse.fail().message("bbb");
}
Aggregations