Search in sources :

Example 1 with ReplyException

use of io.vertx.core.eventbus.ReplyException 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();
}
Also used : Context(io.vertx.core.Context) 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) DeploymentOptions(io.vertx.core.DeploymentOptions) ReplyException(io.vertx.core.eventbus.ReplyException) AbstractVerticle(io.vertx.core.AbstractVerticle) ReplyException(io.vertx.core.eventbus.ReplyException) Test(org.junit.Test)

Example 2 with ReplyException

use of io.vertx.core.eventbus.ReplyException in project vert.x by eclipse.

the class FaultToleranceVerticle method ping.

private void ping(Message<JsonArray> message) {
    JsonArray jsonArray = message.body();
    for (int i = 0; i < jsonArray.size(); i++) {
        int node = jsonArray.getInteger(i);
        for (int j = 0; j < numAddresses; j++) {
            vertx.eventBus().send(createAddress(node, j), "ping", ar -> {
                if (ar.succeeded()) {
                    vertx.eventBus().send("control", "pong");
                } else {
                    Throwable cause = ar.cause();
                    if (cause instanceof ReplyException) {
                        ReplyException replyException = (ReplyException) cause;
                        if (replyException.failureType() == ReplyFailure.NO_HANDLERS) {
                            vertx.eventBus().send("control", "noHandlers");
                            return;
                        }
                    }
                    log.error("Unexpected error during ping (id=" + id + ")", cause);
                }
            });
        }
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray) ReplyException(io.vertx.core.eventbus.ReplyException)

Example 3 with ReplyException

use of io.vertx.core.eventbus.ReplyException in project chili-core by codingchili.

the class BusRouter method send.

protected void send(Request request, String target) {
    DeliveryOptions options = new DeliveryOptions().setSendTimeout(request.timeout());
    core.bus().send(target, request.data(), options, send -> {
        if (send.succeeded()) {
            request.write(send.result().body());
        } else {
            Throwable exception = send.cause();
            if (exception instanceof ReplyException) {
                ReplyFailure status = ((ReplyException) exception).failureType();
                exceptionHandlers.get(status).accept(request);
            } else {
                request.error(send.cause());
            }
        }
    });
}
Also used : ReplyFailure(io.vertx.core.eventbus.ReplyFailure) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) ReplyException(io.vertx.core.eventbus.ReplyException)

Example 4 with ReplyException

use of io.vertx.core.eventbus.ReplyException in project vertx-openshift-it by cescoffier.

the class SenderVerticle method setupRouter.

private Router setupRouter() {
    Router router = Router.router(vertx);
    router.get("/deliver-to-functional-pod").handler(rc -> {
        vertx.eventBus().<String>send("foobar", "ping", ar -> {
            if (ar.succeeded()) {
                rc.response().putHeader(HttpHeaders.CONTENT_TYPE, "text/plain").end(ar.result().body());
            } else {
                ar.cause().printStackTrace();
                rc.fail(500);
            }
        });
    });
    router.get("/deliver-to-failing-pod").handler(rc -> {
        vertx.eventBus().<String>send("foobar", "ping", ar -> {
            if (ar.failed() && ar.cause() instanceof ReplyException) {
                ReplyException replyException = (ReplyException) ar.cause();
                if (replyException.failureType() == ReplyFailure.NO_HANDLERS) {
                    rc.response().putHeader(HttpHeaders.CONTENT_TYPE, "text/plain").end("OK");
                    return;
                }
            }
            rc.fail(500);
        });
    });
    router.get("/health").handler(rc -> {
        rc.response().putHeader(HttpHeaders.CONTENT_TYPE, "text/plain").end("OK");
    });
    return router;
}
Also used : Router(io.vertx.ext.web.Router) ReplyException(io.vertx.core.eventbus.ReplyException)

Example 5 with ReplyException

use of io.vertx.core.eventbus.ReplyException in project vert.x by eclipse.

the class FaultToleranceVerticle method ping.

private void ping(Message<JsonArray> message) {
    JsonArray jsonArray = message.body();
    for (int i = 0; i < jsonArray.size(); i++) {
        int node = jsonArray.getInteger(i);
        for (int j = 0; j < numAddresses; j++) {
            vertx.eventBus().request(createAddress(node, j), "ping", ar -> {
                if (ar.succeeded()) {
                    vertx.eventBus().send("control", "pong");
                } else {
                    Throwable cause = ar.cause();
                    if (cause instanceof ReplyException) {
                        ReplyException replyException = (ReplyException) cause;
                        if (replyException.failureType() == ReplyFailure.NO_HANDLERS) {
                            vertx.eventBus().send("control", "noHandlers");
                            return;
                        }
                    }
                    log.error("Unexpected error during ping (id=" + id + ")", cause);
                }
            });
        }
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray) ReplyException(io.vertx.core.eventbus.ReplyException)

Aggregations

ReplyException (io.vertx.core.eventbus.ReplyException)12 DeliveryOptions (io.vertx.core.eventbus.DeliveryOptions)4 ReplyFailure (io.vertx.core.eventbus.ReplyFailure)4 JsonArray (io.vertx.core.json.JsonArray)4 EventBus (io.vertx.core.eventbus.EventBus)3 JsonObject (io.vertx.core.json.JsonObject)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