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

void reply(@Nullable Object message, DeliveryOptions options);

Source Link

Document

Link #reply(Object) but allows you to specify delivery options for the reply.

Usage

From source file:eventbusbridge.Main.java

License:Apache License

static void pingPong(final Message<JsonObject> m) {
    final int counter = m.body().getInteger("counter");
    System.out.println("ping-pong: count is " + counter);
    final JsonObject reply = new JsonObject();
    reply.put("counter", counter + 1);
    m.reply(reply, pingPongReply);
}

From source file:io.apiman.gateway.platforms.vertx3.services.InitializeIngestorServiceVertxProxyHandler.java

License:Apache License

@Override
public void handle(Message<JsonObject> msg) {
    JsonObject json = msg.body();/*www .  ja  v a  2 s.  com*/
    String action = msg.headers().get("action");
    if (action == null) {
        throw new IllegalStateException("action not specified");
    }
    accessed();
    switch (action) {
    case "createIngestor": {
        service.createIngestor((java.lang.String) json.getValue("uuid"), res -> {
            if (res.failed()) {
                msg.fail(-1, res.cause().getMessage());
            } else {
                String proxyAddress = UUID.randomUUID().toString();
                ProxyHelper.registerService(IngestorToPolicyService.class, vertx, res.result(), proxyAddress,
                        false, timeoutSeconds);
                msg.reply(null, new DeliveryOptions().addHeader("proxyaddr", proxyAddress));
            }
        });
        break;
    }

    default: {
        throw new IllegalStateException("Invalid action: " + action);
    }
    }
}

From source file:org.entcore.common.storage.impl.AbstractApplicationStorage.java

License:Open Source License

private void reply(Message<JsonObject> event, JsonObject response) {
    final String replyTo = event.body().getString("replyTo");
    if (isNotEmpty(replyTo)) {
        final String replyAction = event.body().getString("replyAction");
        if (isNotEmpty(replyAction)) {
            response.put("action", replyAction);
        }//from w ww .j  ava2 s . c  o  m
        vertx.eventBus().send(replyTo, response, handlerToAsyncHandler(this));
    } else {
        event.reply(response, handlerToAsyncHandler(this));
    }
}

From source file:org.entcore.infra.services.impl.AbstractAntivirusService.java

License:Open Source License

private void removeInfectedFile(final InfectedFile i, final Message<JsonObject> message) {
    ObjectMapper mapper = new ObjectMapper();
    try {//from ww w  .ja  v  a  2 s  . co m
        final JsonObject params = new JsonObject(mapper.writeValueAsString(i));
        log.info("Remove infected file : " + params.encode());
        final HttpServerRequest request = new JsonHttpServerRequest(new JsonObject());
        render.processTemplate(request, "text/infectedFile.txt", params, new Handler<String>() {
            @Override
            public void handle(String content) {
                storage.writeBuffer(i.getPath(), i.getId(), Buffer.buffer(content), "text/plain",
                        i.getName() + ".txt", new Handler<JsonObject>() {
                            @Override
                            public void handle(JsonObject event) {
                                if (timeline != null && i.getOwner() != null) {
                                    final List<String> recipients = new ArrayList<>();
                                    recipients.add(i.getOwner());
                                    timeline.notifyTimeline(request, "workspace.delete-virus", null, recipients,
                                            null, params);
                                }
                                if (message != null) {
                                    JsonObject m = new JsonObject().put("id", i.getId())
                                            .put("name", i.getName() + ".txt").put("contentType", "text/plain")
                                            .put("action", "updateInfos");
                                    message.reply(m, handlerToAsyncHandler(new Handler<Message<JsonObject>>() {
                                        @Override
                                        public void handle(Message<JsonObject> r) {
                                            if ("ok".equals(r.body().getString("status"))
                                                    && r.body().getInteger("count", -1) > 0) {
                                                log.info("File info " + i.getId() + " updated.");
                                            } else {
                                                log.error("Error updating file info " + i.getId());
                                            }
                                        }
                                    }));
                                }
                            }
                        });
            }
        });
    } catch (IOException | DecodeException e) {
        log.error("Error serializing infected file : " + i.getId(), e);
    }
}

From source file:pt.davidafsilva.slacker.api.AbstractSlackerExecutor.java

License:Open Source License

/**
 * Handles an incoming request from the event bus
 *
 * @param request the request message to be handled
 */// w  w  w .ja v  a  2  s . c om
private void handleExecutorEvent(final Message<SlackerRequest> request) {
    LOGGER.info("<=<= receiving incoming request <=<=");
    LOGGER.debug(request);

    // execute the request handling asynchronously
    context.runOnContext(a -> {
        final Future<SlackerResponse> future = futureFactory.future();
        execute(request.body(), future);
        future.setHandler(handler -> {
            if (handler.succeeded()) {
                LOGGER.info("=>=> successfully handled request =>=>");
                LOGGER.debug(handler.result());
                request.reply(handler.result(),
                        new DeliveryOptions().setCodecName(SlackerResponseMessageCodec.NAME));
            } else {
                request.fail(ResultCode.ERROR.ordinal(), handler.cause().getMessage());
                LOGGER.error("failed to handle request", handler.cause());
            }
        });
    });
}

From source file:pt.davidafsilva.slacker.server.EventServerVerticle.java

License:Open Source License

/**
 * Sends the requests to the executor and handles the reply
 *
 * @param address        the address of the executor
 * @param request        the request to be sent
 * @param requestMessage the original request message with the reply address
 *//*from   ww  w.  jav a2s.co m*/
private void sendRequestToExecutor(final String address, final SlackerRequest request,
        final Message<Object> requestMessage) {
    LOGGER.debug("forwarding request message to {0}..", address);
    vertx.eventBus().send(address, request, new DeliveryOptions().setCodecName(SlackerRequestMessageCodec.NAME),
            reply -> {
                if (reply.succeeded() && SlackerResponse.class.isInstance(reply.result().body())) {
                    requestMessage.reply(reply.result().body(),
                            new DeliveryOptions().setCodecName(SlackerResponseMessageCodec.NAME));
                } else {
                    LOGGER.error("failed to process request", reply.cause());
                    requestMessage.fail(2,
                            String.format("failed %s processing: %s", request.getCommand(),
                                    Optional.ofNullable(reply.cause()).map(Throwable::getMessage)
                                            .orElse("invalid response")));
                }
            });
}

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

License:Apache License

public void handle(Message<JsonObject> msg) {
    JsonObject json = msg.body();//w  w w  . j a  v a2s  . c  o  m
    String action = msg.headers().get("action");
    if (action == null) {
        throw new IllegalStateException("action not specified");
    }
    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);
    }
    }
}