Search in sources :

Example 6 with ReplyException

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);
}
Also used : ReplyFailure(io.vertx.core.eventbus.ReplyFailure) ReplyException(io.vertx.core.eventbus.ReplyException)

Example 7 with ReplyException

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);
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Verticle(io.vertx.core.Verticle) Context(io.vertx.core.Context) Future(io.vertx.core.Future) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) EventBus(io.vertx.core.eventbus.EventBus) MessageCodec(io.vertx.core.eventbus.MessageCodec) Buffer(io.vertx.core.buffer.Buffer) DeploymentOptions(io.vertx.core.DeploymentOptions) AbstractVerticle(io.vertx.core.AbstractVerticle) CharsetUtil(io.netty.util.CharsetUtil) ReplyFailure(io.vertx.core.eventbus.ReplyFailure) JsonObject(io.vertx.core.json.JsonObject) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) ReplyException(io.vertx.core.eventbus.ReplyException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Verticle(io.vertx.core.Verticle) AbstractVerticle(io.vertx.core.AbstractVerticle) Future(io.vertx.core.Future) EventBus(io.vertx.core.eventbus.EventBus) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractVerticle(io.vertx.core.AbstractVerticle) ReplyException(io.vertx.core.eventbus.ReplyException) Test(org.junit.Test)

Example 8 with ReplyException

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()));
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EventBus(io.vertx.core.eventbus.EventBus) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) ReplyException(io.vertx.core.eventbus.ReplyException)

Example 9 with ReplyException

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);
        }
    });
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) StoreCursor(io.georocket.storage.StoreCursor) ReplyException(io.vertx.core.eventbus.ReplyException) ArrayDeque(java.util.ArrayDeque)

Example 10 with ReplyException

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);
        }
    }
}
Also used : VertxTracer(io.vertx.core.spi.tracing.VertxTracer) ClusteredMessage(io.vertx.core.eventbus.impl.clustered.ClusteredMessage) ReplyException(io.vertx.core.eventbus.ReplyException)

Aggregations

ReplyException (io.vertx.core.eventbus.ReplyException)12 DeliveryOptions (io.vertx.core.eventbus.DeliveryOptions)4 JsonArray (io.vertx.core.json.JsonArray)4 JsonObject (io.vertx.core.json.JsonObject)4 EventBus (io.vertx.core.eventbus.EventBus)3 ReplyFailure (io.vertx.core.eventbus.ReplyFailure)3 CharsetUtil (io.netty.util.CharsetUtil)2 AbstractVerticle (io.vertx.core.AbstractVerticle)2 Context (io.vertx.core.Context)2 DeploymentOptions (io.vertx.core.DeploymentOptions)2 Future (io.vertx.core.Future)2 Verticle (io.vertx.core.Verticle)2 Buffer (io.vertx.core.buffer.Buffer)2 MessageCodec (io.vertx.core.eventbus.MessageCodec)2 MessageConsumer (io.vertx.core.eventbus.MessageConsumer)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Consumer (java.util.function.Consumer)2 CoreMatchers (org.hamcrest.CoreMatchers)2 Test (org.junit.Test)2