Example usage for io.vertx.core.eventbus EventBus publish

List of usage examples for io.vertx.core.eventbus EventBus publish

Introduction

In this page you can find the example usage for io.vertx.core.eventbus EventBus publish.

Prototype

@Fluent
EventBus publish(String address, @Nullable Object message);

Source Link

Document

Publish a message.

The message will be delivered to all handlers registered to the address.

Usage

From source file:com.mycompany.sharedwhiteboard.Server.java

@Override
public void start() throws Exception {

    Router router = Router.router(vertx);

    // Allow events for the designated addresses in/out of the event bus bridge
    BridgeOptions opts = new BridgeOptions()
            .addInboundPermitted(new PermittedOptions().setAddress("chat.to.server"))
            .addOutboundPermitted(new PermittedOptions().setAddress("chat.to.client"));

    // Create the event bus bridge and add it to the router.
    SockJSHandler ebHandler = SockJSHandler.create(vertx).bridge(opts);
    router.route("/eventbus/*").handler(ebHandler);

    // Create a router endpoint for the static content.
    router.route().handler(StaticHandler.create());

    // Start the web server and tell it to use the router to handle requests.
    vertx.createHttpServer().requestHandler(router::accept).listen(8080);

    EventBus eb = vertx.eventBus();

    // Register to listen for messages coming IN to the server
    eb.consumer("chat.to.server").handler(message -> {
        // Send the message back out to all clients with the timestamp prepended.
        eb.publish("chat.to.client", message.body());
    });//from  w  w  w .j a v a 2  s.c o m

}

From source file:com.tad.vertx.events.CDIEventBusBridge.java

License:Apache License

@Override
public void onVertxStart() {
    EventBus eb = vertx.eventBus();
    eb.consumer("some-address", m -> beanManager.fireEvent(m.body()));
    vertx.setPeriodic(1000, v -> eb.publish("some-address", "Some text here " + v));
    vertx.setPeriodic(1500, v -> eb.publish("some-address", new JsonObject().put("foo", "bar " + v)));
}

From source file:com.zanclus.distributed.chat.service.Main.java

License:Apache License

@Override
public void start() throws Exception {

    Router router = Router.router(vertx);

    final EventBus eb = vertx.eventBus();

    eb.consumer("chat.to.server").handler(message -> {
        String timestamp = getDateTimeInstance(SHORT, MEDIUM).format(Date.from(now()));
        eb.publish("chat.to.client", timestamp + ": " + message.body());
    });/*from w  ww  . ja  va2  s.  co  m*/

    BridgeOptions opts = new BridgeOptions()
            .addInboundPermitted(new PermittedOptions().setAddress("chat.to.server"))
            .addOutboundPermitted(new PermittedOptions().setAddress("chat.to.client"));

    SockJSHandler ebHandler = SockJSHandler.create(vertx).bridge(opts);
    router.route("/eventbus/*").handler(ebHandler);

    router.route().handler(StaticHandler.create("webroot/").setIndexPage("chat.html"));

    vertx.createHttpServer().requestHandler(router::accept).listen(8000);
}

From source file:eventbusbridge.Main.java

License:Apache License

public static void main(String[] args) {
    final Vertx vertx = Vertx.vertx();
    final EventBus eb = vertx.eventBus();
    TcpEventBusBridge bridge = TcpEventBusBridge.create(vertx,
            new BridgeOptions().addOutboundPermitted(new PermittedOptions().setAddressRegex("test.*"))
                    .addInboundPermitted(new PermittedOptions().setAddressRegex("test.*")));

    final int port = Integer.parseInt(args[0]);
    final File semaphoreFile;
    if (args.length > 1) {
        semaphoreFile = new File(args[1]);
    } else {//from  www.j av a 2s  .c o m
        semaphoreFile = null;
    }

    bridge.listen(port, res -> {
        System.out.println("Vert.x bridge started on " + port);
        if (semaphoreFile != null) {
            try {
                semaphoreFile.createNewFile();
            } catch (IOException e) {
                System.err.println(e);
            }
        }
        vertx.setPeriodic(100, timer -> {
            //System.out.println("Sending the time...");
            eb.publish("test.time", new JsonObject().put("now", System.currentTimeMillis()));
            eb.send("test.time-send", new JsonObject().put("now", System.currentTimeMillis()));
        });

        vertx.eventBus().consumer("test.echo", m -> {
            //System.out.println("echo: " + m.body());
            JsonObject reply = new JsonObject();
            JsonObject headers = new JsonObject();
            m.headers().forEach(e -> headers.put(e.getKey(), e.getValue()));

            reply.put("original-body", m.body()).put("original-headers", headers);
            //System.out.println("REPLY: " + m.headers() + " | " + reply);
            m.reply(reply);
            // send a copy to another address as well to test
            // non-replyable messages
            if (m.isSend()) {
                eb.send("test.echo.responses", reply);
            } else {
                eb.publish("test.echo.responses", reply);
            }
        });

        vertx.eventBus().consumer("test.ping-pong", Main::pingPong);
    });
}

From source file:examples.EventBusExamples.java

License:Open Source License

public void example5(EventBus eventBus) {
    eventBus.publish("news.uk.sport", "Yay! Someone kicked a ball");
}

From source file:org.entcore.common.appregistry.ApplicationUtils.java

License:Open Source License

public static void publishModifiedUserGroup(EventBus eb, JsonArray a) {
    eb.publish(APP_REGISTRY_PUBLISH_ADDRESS, new JsonObject().put("type", USER_GROUP_UPDATED).put("users", a));
}

From source file:org.entcore.common.appregistry.ApplicationUtils.java

License:Open Source License

public static void afterImport(EventBus eb) {
    eb.publish(APP_REGISTRY_PUBLISH_ADDRESS, new JsonObject().put("type", IMPORT_SUCCEEDED));
}

From source file:org.entcore.feeder.dictionary.structures.Transition.java

License:Open Source License

public static void publishDeleteGroups(EventBus eb, Logger logger, JsonArray groups) {
    logger.info("Delete groups : " + groups.encode());
    eb.publish(Feeder.USER_REPOSITORY,
            new JsonObject().put("action", "delete-groups").put("old-groups", groups));
}

From source file:org.wisdom.framework.vertx.Socket.java

License:Apache License

/**
 * Sends a text frame on the socket.//  w  ww  .j a  va  2 s  .c o m
 *
 * @param message the message
 * @param bus     the Vert.x event bus.
 */
public void publish(String message, EventBus bus) {
    bus.publish(getWriteHandlerId(), message);
}

From source file:org.wisdom.framework.vertx.Socket.java

License:Apache License

/**
 * Sends a binary frame on the socket.//from   ww  w . j  a  v a  2s  .c  o m
 *
 * @param message the message
 * @param bus     the Vert.x event bus.
 */
public void publish(byte[] message, EventBus bus) {
    bus.publish(getBinaryWriteHandlerId(), Buffer.buffer(message));
}