Search in sources :

Example 1 with EventBus

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

the class EventBusTestBase method testSendFromExecuteBlocking.

@Test
public void testSendFromExecuteBlocking() throws Exception {
    String expectedBody = TestUtils.randomAlphaString(20);
    CountDownLatch receivedLatch = new CountDownLatch(1);
    startNodes(2);
    vertices[1].eventBus().<String>consumer(ADDRESS1, msg -> {
        assertEquals(expectedBody, msg.body());
        receivedLatch.countDown();
    }).completionHandler(ar -> {
        assertTrue(ar.succeeded());
        vertices[0].executeBlocking(fut -> {
            vertices[0].eventBus().send(ADDRESS1, expectedBody);
            try {
                awaitLatch(receivedLatch);
            } catch (InterruptedException e) {
                Thread.interrupted();
                fut.fail(e);
            }
            fut.complete();
        }, ar2 -> {
            if (ar2.succeeded()) {
                testComplete();
            } else {
                fail(ar2.cause());
            }
        });
    });
    await();
}
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) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with EventBus

use of io.vertx.core.eventbus.EventBus 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 3 with EventBus

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

the class EventBusTestBase method testReplyFromWorker.

@Test
public void testReplyFromWorker() throws Exception {
    String expectedBody = TestUtils.randomAlphaString(20);
    startNodes(2);
    CountDownLatch latch = new CountDownLatch(1);
    vertices[0].deployVerticle(new AbstractVerticle() {

        @Override
        public void start() throws Exception {
            vertices[1].eventBus().<String>consumer(ADDRESS1, msg -> {
                msg.reply(expectedBody);
            }).completionHandler(ar -> {
                assertTrue(ar.succeeded());
                latch.countDown();
            });
        }
    }, new DeploymentOptions().setWorker(true));
    awaitLatch(latch);
    vertices[0].eventBus().send(ADDRESS1, "whatever", reply -> {
        assertTrue(reply.succeeded());
        assertEquals(expectedBody, reply.result().body());
        testComplete();
    });
    await();
}
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) DeploymentOptions(io.vertx.core.DeploymentOptions) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractVerticle(io.vertx.core.AbstractVerticle) ReplyException(io.vertx.core.eventbus.ReplyException) Test(org.junit.Test)

Example 4 with EventBus

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

the class EventBusExamples method example12.

public void example12() {
    VertxOptions options = new VertxOptions();
    Vertx.clusteredVertx(options, res -> {
        if (res.succeeded()) {
            Vertx vertx = res.result();
            EventBus eventBus = vertx.eventBus();
            System.out.println("We now have a clustered event bus: " + eventBus);
        } else {
            System.out.println("Failed: " + res.cause());
        }
    });
}
Also used : EventBus(io.vertx.core.eventbus.EventBus) Vertx(io.vertx.core.Vertx) VertxOptions(io.vertx.core.VertxOptions)

Example 5 with EventBus

use of io.vertx.core.eventbus.EventBus 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();
}
Also used : io.vertx.core(io.vertx.core) DatagramSocket(io.vertx.core.datagram.DatagramSocket) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) io.vertx.core.net(io.vertx.core.net) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) io.vertx.core.http(io.vertx.core.http) CountDownLatch(java.util.concurrent.CountDownLatch) EventBus(io.vertx.core.eventbus.EventBus) Buffer(io.vertx.core.buffer.Buffer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) Ignore(org.junit.Ignore) BiConsumer(java.util.function.BiConsumer) io.vertx.core.spi.metrics(io.vertx.core.spi.metrics) DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) EventBus(io.vertx.core.eventbus.EventBus) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Aggregations

EventBus (io.vertx.core.eventbus.EventBus)17 CountDownLatch (java.util.concurrent.CountDownLatch)10 Test (org.junit.Test)10 DeliveryOptions (io.vertx.core.eventbus.DeliveryOptions)8 Buffer (io.vertx.core.buffer.Buffer)7 MessageConsumer (io.vertx.core.eventbus.MessageConsumer)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 Context (io.vertx.core.Context)5 DeploymentOptions (io.vertx.core.DeploymentOptions)5 Future (io.vertx.core.Future)5 Verticle (io.vertx.core.Verticle)5 ReplyFailure (io.vertx.core.eventbus.ReplyFailure)5 JsonObject (io.vertx.core.json.JsonObject)5 Consumer (java.util.function.Consumer)5 CharsetUtil (io.netty.util.CharsetUtil)4 AbstractVerticle (io.vertx.core.AbstractVerticle)4 Vertx (io.vertx.core.Vertx)4 VertxOptions (io.vertx.core.VertxOptions)4 DatagramSocket (io.vertx.core.datagram.DatagramSocket)4 io.vertx.core (io.vertx.core)3