Example usage for io.vertx.core.eventbus Message reply

List of usage examples for io.vertx.core.eventbus Message reply

Introduction

In this page you can find the example usage for io.vertx.core.eventbus Message reply.

Prototype

default void reply(@Nullable Object message) 

Source Link

Document

Reply to this message.

Usage

From source file:vertigo.core.dht.IData.java

/**
 *
 * @param <K> The key type of the DHT
 * @param <T> The value type of the DHT
 * @param <R> The result type of the function parameter "function"
 * @param key The key of the inital node to terminate traverse if inital node is reached
 * @param start Beginning of key space for traverse. Traverse key space between [start, end]
 * @param end Termination of key space for traverse. Traverse key space between [start, end]
 * @param identity Default return value if traverse is terminated because inital node is reached
 * @param function Function to apply within key space
 * @param handler Result handler after traverse is completed
 * @return The traverse function//from   w  w w .j ava  2 s  . co  m
 */
public static <K extends Comparable<K> & Serializable, T extends Serializable, R extends Serializable> AsyncFunction<Pair<ExecutionContext<DataNode<K, T>, Message<byte[]>, R>, Message<byte[]>>, R> tranverse(
        K key, K start, K end, R identity,
        AsyncFunction<Pair<ExecutionContext<Void, DataNode<K, T>, R>, DataNode<K, T>>, R> function,
        SerializableConsumer<R> handler) {
    return (pair, cb) -> {
        ExecutionContext<DataNode<K, T>, Message<byte[]>, R> c = pair.getValue0();
        Message<byte[]> msg = pair.getValue1();

        if ((!start.equals(end) && IDht.isResponsible(start, end, c.context().myKey))
                || IDht.isResponsible(c.context(), start) || IDht.isResponsible(c.context(), end)) {
            Pair<ExecutionContext<Void, DataNode<K, T>, R>, DataNode<K, T>> p = new Pair<>(
                    new ExecutionContext<Void, DataNode<K, T>, R>(function), pair.getValue0().context());
            function.apply(p, (R result) -> {
                msg.reply(result);
            });
        }

        if (!c.context().myKey.equals(key)) {
            String addr = IDht.toAddress(c.context().prefix, c.context().nextKey);
            c.context().vertx.eventBus().send(addr, c.serialize(), ar -> {
                if (ar.succeeded()) {
                    msg.reply(ar.result().body());
                } else {
                    msg.reply(ar.cause());
                }
            });
        } else {
            msg.reply(identity);
        }
    };
}

From source file:vertx.example.verticle.DatabaseVerticle.java

@Override
public void start() throws Exception {
    super.start();
    eventBus = vertx.eventBus();//from   ww w  . j  a v  a 2s  .  c om
    client = JDBCClient.createShared(vertx,
            new JsonObject().put("url", "jdbc:postgresql://localhost:5432/postgres")
                    .put("driver_class", "org.postgresql.Driver").put("user", "postgres")
                    .put("password", "post").put("max_pool_size", 30));

    eventBus.consumer("database", (Message<String> event) -> {
        String body = event.body();
        if ("get".equals(body)) {
            client.getConnection((AsyncResult<SQLConnection> conn) -> {
                if (conn.failed()) {
                    System.err.println(conn.cause().getMessage());
                    return;
                }

                // query some data with arguments
                query(conn.result(), "select * from test", (ResultSet rs) -> {
                    event.reply(rs.getResults().toString());

                    // and close the connection
                    conn.result().close((AsyncResult<Void> done) -> {
                        if (done.failed()) {
                            throw new RuntimeException(done.cause());
                        }
                    });
                });
            });

        }
    });

}

From source file:vertx_react.verticles.EventsVerticle.java

@Override
public void start(Future<Void> startFuture) throws Exception {
    EventBus eb = this.getVertx().eventBus();
    eb.consumer("incoming", (Message<JsonObject> handler) -> {
        JsonObject m = handler.body();//  w  w w  .  ja  v  a  2s  .  c  om
        JsonArray ja = new JsonArray(getRandomArray());
        JsonObject j = new JsonObject().put("table", m.getInteger("table")).put("openSeats", ja);
        handler.reply(j);
        log.info(Json.encodePrettily(m.encodePrettily()));
    });
    super.start(startFuture);
}