Search in sources :

Example 6 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 7 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 8 with EventBus

use of io.vertx.core.eventbus.EventBus in project vertx-tcp-eventbus-bridge by vert-x3.

the class TcpEventBusBridgeEchoServer method main.

public static void main(String[] args) {
    Vertx vertx = Vertx.vertx();
    EventBus eb = vertx.eventBus();
    eb.consumer("hello", (Message<JsonObject> msg) -> {
        msg.reply(new JsonObject().put("value", "Hello " + msg.body().getString("value")));
    });
    eb.consumer("echo", (Message<JsonObject> msg) -> msg.reply(msg.body()));
    eb.consumer("echo2", (Message<JsonObject> msg) -> {
        if ("send".equals(msg.body().getString("response_type"))) {
            eb.send("echo2_response", msg.body());
        } else {
            eb.publish("echo2_response", msg.body());
        }
    });
    TcpEventBusBridge bridge = TcpEventBusBridge.create(vertx, new BridgeOptions().addInboundPermitted(new PermittedOptions().setAddress("hello")).addInboundPermitted(new PermittedOptions().setAddress("echo")).addOutboundPermitted(new PermittedOptions().setAddress("echo")).addInboundPermitted(new PermittedOptions().setAddress("echo2")).addOutboundPermitted(new PermittedOptions().setAddress("echo2_response")));
    bridge.listen(7000, res -> System.out.println("Ready"));
}
Also used : Message(io.vertx.core.eventbus.Message) TcpEventBusBridge(io.vertx.ext.eventbus.bridge.tcp.TcpEventBusBridge) JsonObject(io.vertx.core.json.JsonObject) BridgeOptions(io.vertx.ext.bridge.BridgeOptions) EventBus(io.vertx.core.eventbus.EventBus) PermittedOptions(io.vertx.ext.bridge.PermittedOptions) Vertx(io.vertx.core.Vertx)

Example 9 with EventBus

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

the class EventBusExamples method example13.

public void example13() {
    VertxOptions options = new VertxOptions().setEventBusOptions(new EventBusOptions().setSsl(true).setKeyStoreOptions(new JksOptions().setPath("keystore.jks").setPassword("wibble")).setTrustStoreOptions(new JksOptions().setPath("keystore.jks").setPassword("wibble")).setClientAuth(ClientAuth.REQUIRED));
    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 : JksOptions(io.vertx.core.net.JksOptions) EventBusOptions(io.vertx.core.eventbus.EventBusOptions) EventBus(io.vertx.core.eventbus.EventBus) Vertx(io.vertx.core.Vertx) VertxOptions(io.vertx.core.VertxOptions)

Example 10 with EventBus

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

the class EventBusExamples method example1.

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

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