Search in sources :

Example 11 with EventBus

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

the class EventBusExamples method example14.

public void example14() {
    VertxOptions options = new VertxOptions().setEventBusOptions(new EventBusOptions().setClusterPublicHost("whatever").setClusterPublicPort(1234));
    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 : EventBusOptions(io.vertx.core.eventbus.EventBusOptions) EventBus(io.vertx.core.eventbus.EventBus) Vertx(io.vertx.core.Vertx) VertxOptions(io.vertx.core.VertxOptions)

Example 12 with EventBus

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

the class EventBusExamples method example2.

public void example2(Vertx vertx) {
    EventBus eb = vertx.eventBus();
    MessageConsumer<String> consumer = eb.consumer("news.uk.sport");
    consumer.handler(message -> {
        System.out.println("I have received a message: " + message.body());
    });
}
Also used : EventBus(io.vertx.core.eventbus.EventBus)

Example 13 with EventBus

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

the class MetricsTest method testReplyFailureRecipientFailure.

@Test
public void testReplyFailureRecipientFailure() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    EventBus eb = vertx.eventBus();
    FakeEventBusMetrics metrics = FakeMetricsBase.getMetrics(eb);
    AtomicReference<String> replyAddress = new AtomicReference<>();
    CountDownLatch regLatch = new CountDownLatch(1);
    eb.consumer("foo", msg -> {
        replyAddress.set(msg.replyAddress());
        msg.fail(0, "whatever");
    }).completionHandler(onSuccess(v -> {
        regLatch.countDown();
    }));
    awaitLatch(regLatch);
    eb.send("foo", "bar", new DeliveryOptions(), ar -> {
        assertTrue(ar.failed());
        latch.countDown();
    });
    awaitLatch(latch);
    assertEquals(Collections.singletonList(replyAddress.get()), metrics.getReplyFailureAddresses());
    assertEquals(Collections.singletonList(ReplyFailure.RECIPIENT_FAILURE), metrics.getReplyFailures());
}
Also used : DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) Arrays(java.util.Arrays) io.vertx.core(io.vertx.core) DatagramSocket(io.vertx.core.datagram.DatagramSocket) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) EventBus(io.vertx.core.eventbus.EventBus) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Is.is(org.hamcrest.core.Is.is) ReplyFailure(io.vertx.core.eventbus.ReplyFailure) io.vertx.test.fakemetrics(io.vertx.test.fakemetrics) Test(org.junit.Test) io.vertx.core.http(io.vertx.core.http) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) PoolMetrics(io.vertx.core.spi.metrics.PoolMetrics) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) FileSystem(io.vertx.core.file.FileSystem) Optional(java.util.Optional) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) Collections(java.util.Collections) NetSocket(io.vertx.core.net.NetSocket) AtomicReference(java.util.concurrent.atomic.AtomicReference) EventBus(io.vertx.core.eventbus.EventBus) CountDownLatch(java.util.concurrent.CountDownLatch) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) Test(org.junit.Test)

Example 14 with EventBus

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

the class MetricsTest method testReplyFailureNoHandlers.

@Test
public void testReplyFailureNoHandlers() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    EventBus eb = vertx.eventBus();
    eb.send(ADDRESS1, "bar", new DeliveryOptions().setSendTimeout(10), ar -> {
        assertTrue(ar.failed());
        latch.countDown();
    });
    awaitLatch(latch);
    FakeEventBusMetrics metrics = FakeMetricsBase.getMetrics(eb);
    assertEquals(Collections.singletonList(ADDRESS1), metrics.getReplyFailureAddresses());
    assertEquals(Collections.singletonList(ReplyFailure.NO_HANDLERS), metrics.getReplyFailures());
}
Also used : EventBus(io.vertx.core.eventbus.EventBus) CountDownLatch(java.util.concurrent.CountDownLatch) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) Test(org.junit.Test)

Example 15 with EventBus

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

the class MetricsContextTest method testEventBusLifecycle.

@Test
public void testEventBusLifecycle() {
    AtomicBoolean closeCalled = 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 close() {
                    closeCalled.set(true);
                }
            };
        }
    };
    Vertx vertx = vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory)));
    vertx.eventBus();
    executeInVanillaThread(() -> {
        vertx.close(onSuccess(v -> {
            assertTrue(closeCalled.get());
            testComplete();
        }));
    });
    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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MetricsOptions(io.vertx.core.metrics.MetricsOptions) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) EventBus(io.vertx.core.eventbus.EventBus) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) Test(org.junit.Test)

Aggregations

EventBus (io.vertx.core.eventbus.EventBus)24 Test (org.junit.Test)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 Context (io.vertx.core.Context)8 Vertx (io.vertx.core.Vertx)8 Buffer (io.vertx.core.buffer.Buffer)8 DeliveryOptions (io.vertx.core.eventbus.DeliveryOptions)8 MessageConsumer (io.vertx.core.eventbus.MessageConsumer)7 JsonObject (io.vertx.core.json.JsonObject)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 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 FileSystem (io.vertx.core.file.FileSystem)5 Consumer (java.util.function.Consumer)5 CharsetUtil (io.netty.util.CharsetUtil)4 VertxOptions (io.vertx.core.VertxOptions)4 DatagramSocket (io.vertx.core.datagram.DatagramSocket)4 io.vertx.core (io.vertx.core)3