use of io.vertx.core.eventbus.MessageConsumer 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();
}
use of io.vertx.core.eventbus.MessageConsumer in project vert.x by eclipse.
the class NetTest method setHandlers.
void setHandlers(NetSocket sock) {
Handler<Message<Buffer>> resumeHandler = m -> sock.resume();
MessageConsumer reg = vertx.eventBus().<Buffer>consumer("client_resume").handler(resumeHandler);
sock.closeHandler(v -> reg.unregister());
}
use of io.vertx.core.eventbus.MessageConsumer in project hono by eclipse.
the class RequestResponseEndpoint method onLinkAttach.
/**
* Handles a client's request to establish a link for receiving responses
* to service invocations.
* <p>
* This method registers a consumer on the vert.x event bus for the given reply-to address.
* Response messages received over the event bus are transformed into AMQP messages using
* the {@link #getAmqpReply(EventBusMessage)} method and sent to the client over the established
* link.
*
* @param con The AMQP connection that the link is part of.
* @param sender The link to establish.
* @param replyToAddress The reply-to address to create a consumer on the event bus for.
*/
@Override
public final void onLinkAttach(final ProtonConnection con, final ProtonSender sender, final ResourceIdentifier replyToAddress) {
if (isValidReplyToAddress(replyToAddress)) {
logger.debug("establishing sender link with client [{}]", sender.getName());
final MessageConsumer<JsonObject> replyConsumer = vertx.eventBus().consumer(replyToAddress.toString(), message -> {
// TODO check for correct session here...?
if (logger.isTraceEnabled()) {
logger.trace("forwarding reply to client [{}]: {}", sender.getName(), message.body().encodePrettily());
}
final EventBusMessage response = EventBusMessage.fromJson(message.body());
filterResponse(Constants.getClientPrincipal(con), response).recover(t -> {
final int status = Optional.of(t).map(cause -> {
if (cause instanceof ServiceInvocationException) {
return ((ServiceInvocationException) cause).getErrorCode();
} else {
return null;
}
}).orElse(HttpURLConnection.HTTP_INTERNAL_ERROR);
return Future.succeededFuture(response.getResponse(status));
}).map(filteredResponse -> {
final Message amqpReply = getAmqpReply(filteredResponse);
sender.send(amqpReply);
return null;
});
});
sender.setQoS(ProtonQoS.AT_LEAST_ONCE);
sender.closeHandler(senderClosed -> {
logger.debug("client [{}] closed sender link, removing associated event bus consumer [{}]", sender.getName(), replyConsumer.address());
replyConsumer.unregister();
if (senderClosed.succeeded()) {
senderClosed.result().close();
}
});
sender.open();
} else {
logger.debug("client [{}] provided invalid reply-to address", sender.getName());
sender.setCondition(ProtonHelper.condition(AmqpError.INVALID_FIELD, String.format("reply-to address must have the following format %s/<tenant>/<reply-address>", getName())));
sender.close();
}
}
use of io.vertx.core.eventbus.MessageConsumer in project vertx-openshift-it by cescoffier.
the class DistributedPublish method setup.
void setup(Handler<AsyncResult<Void>> handler) {
EventBus eventBus = vertx.eventBus();
MessageConsumer<String> consumer = createMessageConsumer(eventBus, address);
consumer.handler(msg -> {
eventBus.send(msg.body(), clusterManager.getNodeID());
}).completionHandler(ar -> {
if (ar.succeeded()) {
handler.handle(Future.succeededFuture());
} else {
handler.handle(Future.failedFuture(ar.cause()));
}
});
}
use of io.vertx.core.eventbus.MessageConsumer in project vertx-openshift-it by cescoffier.
the class DistributedTimeout method setup.
void setup(Handler<AsyncResult<Void>> handler) {
EventBus eventBus = vertx.eventBus();
MessageConsumer<Boolean> consumer = createMessageConsumer(eventBus, address);
consumer.handler(msg -> {
Boolean reply = msg.body();
if (reply) {
msg.reply(clusterManager.getNodeID());
}
}).completionHandler(ar -> {
if (ar.succeeded()) {
handler.handle(Future.succeededFuture());
} else {
handler.handle(Future.failedFuture(ar.cause()));
}
});
}
Aggregations