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

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

Introduction

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

Prototype

@CacheReturn
T body();

Source Link

Document

The body of the message.

Usage

From source file:scp.targets.vertx.CommunicationManagerImpl.java

License:Open Source License

@Override
public <T extends Serializable> Status registerReceiver(@NonNull ReceiverConfiguration<T> configuration) {
    final ReceiverInvoker<T> _receiverInvoker = new ReceiverInvoker<>(configuration);
    this.eventBus.consumer(configuration.identifier, (Message<byte[]> msg) -> {
        final ReceiverInvokerStatus _status = _receiverInvoker.receive(getData(msg.body()));
        msg.reply(CommunicationManagerImpl.getBytes(_status));
    });//w ww. j  a  v  a2  s.c om
    return Status.SUCCESS;
}

From source file:scp.targets.vertx.CommunicationManagerImpl.java

License:Open Source License

@Override
public <T extends Serializable> Status registerExecutor(@NonNull ExecutorConfiguration<T> configuration) {
    final ExecutorInvoker<T> _receiverInvoker = new ExecutorInvoker<>(configuration);
    this.eventBus.consumer(configuration.identifier, (Message<byte[]> msg) -> {
        final ExecutorInvokerStatus _status = _receiverInvoker.execute(getData(msg.body()));
        msg.reply(CommunicationManagerImpl.getBytes(_status));
    });//from ww  w.jav  a  2s. c o m
    return Status.SUCCESS;
}

From source file:se.liquidbytes.jel.database.DatabaseConnectionVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    JsonObject json = msg.body();
    String action = msg.headers().get("action");
    if (action == null) {
        throw new IllegalStateException("action not specified");
    }/*w  ww  . j a  v a 2  s  .c o  m*/
    switch (action) {
    case "getSite": {
        service.getSite((java.lang.String) json.getValue("id"), createHandler(msg));
        break;
    }
    case "getSites": {
        service.getSites(createHandler(msg));
        break;
    }
    case "addSite": {
        service.addSite((io.vertx.core.json.JsonObject) json.getValue("document"), createHandler(msg));
        break;
    }
    case "updateSite": {
        service.updateSite((io.vertx.core.json.JsonObject) json.getValue("document"), createHandler(msg));
        break;
    }
    case "removeSite": {
        service.removeSite((java.lang.String) json.getValue("id"), createHandler(msg));
        break;
    }
    case "getUser": {
        service.getUser((java.lang.String) json.getValue("id"), createHandler(msg));
        break;
    }
    case "getUserByUsername": {
        service.getUserByUsername((java.lang.String) json.getValue("username"), createHandler(msg));
        break;
    }
    case "getUsers": {
        service.getUsers(createHandler(msg));
        break;
    }
    case "addUser": {
        service.addUser((io.vertx.core.json.JsonObject) json.getValue("document"), createHandler(msg));
        break;
    }
    case "updateUser": {
        service.updateUser((io.vertx.core.json.JsonObject) json.getValue("document"), createHandler(msg));
        break;
    }
    case "removeUser": {
        service.removeUser((java.lang.String) json.getValue("id"), createHandler(msg));
        break;
    }
    default: {
        throw new IllegalStateException("Invalid action: " + action);
    }
    }
}

From source file:se.liquidbytes.jel.database.DatabaseServiceVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    JsonObject json = msg.body();
    String action = msg.headers().get("action");
    if (action == null) {
        throw new IllegalStateException("action not specified");
    }//from www .  j  ava  2  s.  com
    switch (action) {

    case "getConnection": {
        service.getConnection(res -> {
            if (res.failed()) {
                msg.fail(-1, res.cause().getMessage());
            } else {
                String proxyAddress = UUID.randomUUID().toString();
                ProxyHelper.registerService(DatabaseConnection.class, vertx, res.result(), proxyAddress);
                msg.reply(null, new DeliveryOptions().addHeader("proxyaddr", proxyAddress));
            }
        });
        break;
    }
    case "start": {
        service.start();
        break;
    }
    case "stop": {
        service.stop();
        break;
    }
    default: {
        throw new IllegalStateException("Invalid action: " + action);
    }
    }
}

From source file:se.liquidbytes.jel.owfs.OwfsAdapter.java

License:Apache License

/**
 * Read value from device with specified hwId.
 *
 * @param message eventbus message./*from   ww  w . j a v  a2 s  .co m*/
 * @throws DeviceMissingException throws exception if specified device does not exist.
 */
private void getDeviceValue(Message message) throws DeviceMissingException {
    // Validate and extract action-specific parameters.
    if (message.body() == null) {
        message.fail(400, "Missing parameters.");
        return;
    }

    JsonObject params = (JsonObject) message.body();
    String hwId = params.getString("hwId");

    if (hwId == null || hwId.isEmpty()) {
        message.fail(400, "Missing parameter 'hwId'.");
        return;
    }

    message.reply(this.constructReply(this.getDeviceValue(hwId)));
}

From source file:se.liquidbytes.jel.owfs.OwfsAdapter.java

License:Apache License

/**
 * Set value on device with specified hwId.
 *
 * @param message eventbus message.//from ww w  .  ja v a 2  s. co  m
 * @throws DeviceMissingException throws exception if specified device does not exist.
 * @throws OwServerConnectionException throws exception if command fails for any reason.
 */
private void setDeviceValue(Message message) throws DeviceMissingException, OwServerConnectionException {
    // Validate and extract action-specific parameters.
    if (message.body() == null) {
        message.fail(400, "Missing parameters.");
        return;
    }

    JsonObject params = (JsonObject) message.body();
    String hwId = params.getString("hwId");
    String value = params.getString("value");

    if (hwId == null || hwId.isEmpty()) {
        message.fail(400, "Missing parameter 'hwId'.");
        return;
    }
    if (value == null || value.isEmpty()) {
        message.fail(400, "Missing parameter 'value'.");
        return;
    }

    this.setDeviceValue(hwId, value);
}

From source file:servicefactories.BasicServiceVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    try {//from  w  w  w.  j a v a  2 s.c o m
        JsonObject json = msg.body();
        String action = msg.headers().get("action");
        if (action == null) {
            throw new IllegalStateException("action not specified");
        }
        accessed();
        switch (action) {

        case "find": {
            service.find((io.vertx.core.json.JsonObject) json.getValue("document"), createHandler(msg));
            break;
        }
        case "hello": {
            service.hello((java.lang.String) json.getValue("key"), createHandler(msg));
            break;
        }
        case "call": {
            service.call((java.lang.String) json.getValue("key"));
            break;
        }
        default: {
            throw new IllegalStateException("Invalid action: " + action);
        }
        }
    } catch (Throwable t) {
        msg.reply(new ServiceException(500, t.getMessage()));
        throw t;
    }
}

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

@Override
public void start() throws Exception {
    super.start();
    eventBus = vertx.eventBus();//from  w w w.j  ava2 s. 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();
        JsonArray ja = new JsonArray(getRandomArray());
        JsonObject j = new JsonObject().put("table", m.getInteger("table")).put("openSeats", ja);
        handler.reply(j);// www  .  j  a  v  a  2s. c o  m
        log.info(Json.encodePrettily(m.encodePrettily()));
    });
    super.start(startFuture);
}