use of io.vertx.core.eventbus.ReplyException in project vert.x by eclipse.
the class ReplyExceptionMessageCodec method decodeFromWire.
@Override
public ReplyException decodeFromWire(int pos, Buffer buffer) {
int i = (int) buffer.getByte(pos);
ReplyFailure rf = ReplyFailure.fromInt(i);
pos++;
int failureCode = buffer.getInt(pos);
pos += 4;
boolean isNull = buffer.getByte(pos) == (byte) 0;
String message;
if (!isNull) {
pos++;
int strLength = buffer.getInt(pos);
pos += 4;
byte[] bytes = buffer.getBytes(pos, pos + strLength);
message = new String(bytes, CharsetUtil.UTF_8);
} else {
message = null;
}
return new ReplyException(rf, failureCode, message);
}
use of io.vertx.core.eventbus.ReplyException 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.ReplyException in project vertx-openshift-it by cescoffier.
the class DistributedTimeout method handle.
@Override
public void handle(RoutingContext rc) {
int loops = Integer.parseInt(rc.pathParam("loops"));
EventBus eventBus = vertx.eventBus();
AtomicInteger timeouts = new AtomicInteger();
for (int i = 0; i < loops; i++) {
boolean sendReply = i % 2 == 0;
eventBus.<String>send(address, sendReply, new DeliveryOptions().setSendTimeout(1000), ar -> {
if (ar.failed()) {
if (ar.cause() instanceof ReplyException) {
ReplyException replyException = (ReplyException) ar.cause();
if (replyException.failureType() == ReplyFailure.TIMEOUT) {
timeouts.incrementAndGet();
}
}
}
});
}
vertx.setTimer(2000, l -> {
rc.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json").end(String.valueOf(timeouts.get()));
});
}
use of io.vertx.core.eventbus.ReplyException in project georocket by georocket.
the class IndexedStore method delete.
@Override
public void delete(String search, String path, Handler<AsyncResult<Void>> handler) {
get(search, path, ar -> {
if (ar.failed()) {
Throwable cause = ar.cause();
if (cause instanceof ReplyException) {
// Cast to get access to the failure code
ReplyException ex = (ReplyException) cause;
if (ex.failureCode() == 404) {
handler.handle(Future.succeededFuture());
return;
}
}
handler.handle(Future.failedFuture(ar.cause()));
} else {
StoreCursor cursor = ar.result();
Queue<String> paths = new ArrayDeque<>();
AtomicLong remaining = new AtomicLong(cursor.getInfo().getTotalHits());
doDelete(cursor, paths, remaining, handler);
}
});
}
use of io.vertx.core.eventbus.ReplyException in project vert.x by eclipse.
the class OutboundDeliveryContext method written.
public void written(Throwable failure) {
// Metrics
if (metrics != null) {
boolean remote = (message instanceof ClusteredMessage) && ((ClusteredMessage<?, ?>) message).isToWire();
metrics.messageSent(message.address(), !message.send, !remote, remote);
}
// Tracing
VertxTracer tracer = ctx.tracer();
if (tracer != null) {
Object trace = message.trace;
if (trace != null) {
if (src) {
if (replyHandler != null) {
replyHandler.trace = message.trace;
} else {
tracer.receiveResponse(ctx, null, trace, failure, TagExtractor.empty());
}
}
}
}
// Fail fast reply handler
if (failure instanceof ReplyException) {
if (replyHandler != null) {
replyHandler.fail((ReplyException) failure);
}
}
// Notify promise finally
if (writePromise != null) {
if (failure == null) {
writePromise.complete();
} else {
writePromise.fail(failure);
}
}
}
Aggregations