Example usage for io.vertx.core.eventbus DeliveryOptions DeliveryOptions

List of usage examples for io.vertx.core.eventbus DeliveryOptions DeliveryOptions

Introduction

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

Prototype

public DeliveryOptions() 

Source Link

Document

Default constructor

Usage

From source file:co.runrightfast.vertx.core.eventbus.EventBusUtils.java

License:Apache License

/**
 * Adds the following headers://from  w  w  w .  j a  va  2  s.c  o  m
 *
 * <ul>
 * <li>{@link MessageHeader#MESSAGE_ID}
 * <li>{@link MessageHeader#MESSAGE_TIMESTAMP}
 * </ul>
 *
 * @return DeliveryOptions
 */
static DeliveryOptions deliveryOptions() {
    final DeliveryOptions options = new DeliveryOptions();
    options.addHeader(MESSAGE_ID.header, uuid());
    options.addHeader(MESSAGE_TIMESTAMP.header, DateTimeFormatter.ISO_INSTANT.format(Instant.now()));
    options.addHeader(FROM_JVM.header, JVM_ID);
    return options;
}

From source file:co.runrightfast.vertx.core.eventbus.ProtobufMessageProducer.java

License:Apache License

public void send(@NonNull final A msg) {
    eventBus.send(address, msg, addRunRightFastHeaders(new DeliveryOptions()));
    this.messageSent.mark();
    this.messageLastSent = Instant.now();
}

From source file:co.runrightfast.vertx.core.eventbus.ProtobufMessageProducer.java

License:Apache License

public <RESPONSE> void send(@NonNull final A msg,
        @NonNull final Handler<AsyncResult<io.vertx.core.eventbus.Message<RESPONSE>>> handler) {
    eventBus.send(address, msg, addRunRightFastHeaders(new DeliveryOptions()), handler);
    this.messageSent.mark();
    this.messageLastSent = Instant.now();
}

From source file:co.runrightfast.vertx.core.eventbus.ProtobufMessageProducer.java

License:Apache License

public void publish(@NonNull final A msg) {
    eventBus.publish(address, msg, addRunRightFastHeaders(new DeliveryOptions()));
    this.messagePublished.mark();
    this.messageLastPublished = Instant.now();
}

From source file:co.runrightfast.vertx.demo.testHarness.jmx.DemoMXBeanImpl.java

License:Apache License

@Override
public String verticleDeployments() {
    if (getVerticleDeploymentsMessageSender == null) {
        getVerticleDeploymentsMessageSender = new ProtobufMessageProducer(vertx.eventBus(),
                EventBusAddress.eventBusAddress(RunRightFastVerticleManager.VERTICLE_ID,
                        "get-verticle-deployments"),
                getProtobufMessageCodec(GetVerticleDeployments.Request.getDefaultInstance()).get(),
                metricRegistry);//from w  ww . ja  v a 2s  .co  m
    }

    final CompletableFuture<com.google.protobuf.Message> future = new CompletableFuture();

    getVerticleDeploymentsMessageSender.send(GetVerticleDeployments.Request.newBuilder().build(),
            new DeliveryOptions().setSendTimeout(2000L),
            responseHandler(future, GetVerticleDeployments.Response.class));

    final com.google.protobuf.Message response;
    try {
        response = future.get();
    } catch (final InterruptedException | ExecutionException ex) {
        throw new RuntimeException(ex);
    }

    // TODO: aggregate GetVerticleDeployments.Response from all RunRightFastVerticleManager instances deployed within the JVm
    // ping
    return JsonUtils.toVertxJsonObject(ProtobufUtils.protobuMessageToJson(response)).encodePrettily();
}

From source file:co.runrightfast.vertx.demo.testHarness.jmx.DemoMXBeanImpl.java

License:Apache License

@Override
public String createEventLogRecord(final String event) {
    Preconditions.checkArgument(isNotBlank(event));
    final CompletableFuture<CreateEvent.Response> createEventFuture = new CompletableFuture<>();
    vertx.eventBus().send(EventBusAddress.eventBusAddress(EventLogRepository.VERTICLE_ID, CreateEvent.class),
            CreateEvent.Request.newBuilder().setEvent(event).build(),
            new DeliveryOptions().setSendTimeout(2000L),
            responseHandler(createEventFuture, CreateEvent.Response.class));
    try {//from  w  ww  . j av a  2  s . c om
        final CreateEvent.Response createEventResponse = createEventFuture.get(2, TimeUnit.SECONDS);
        return ProtobufUtils.protobuMessageToJson(createEventResponse.getId()).toString();
    } catch (final InterruptedException | ExecutionException | TimeoutException ex) {
        log.logp(SEVERE, getClass().getName(), "createEventLogRecord", "failed", ex);
        throw new RuntimeException("Failed to create event log record : " + ex.getMessage());
    }
}

From source file:co.runrightfast.vertx.demo.testHarness.jmx.DemoMXBeanImpl.java

License:Apache License

@Override
public String browseEventLogRecords(int skip, int limit) {
    final CompletableFuture<GetEvents.Response> future = new CompletableFuture<>();
    vertx.eventBus().send(EventBusAddress.eventBusAddress(EventLogRepository.VERTICLE_ID, GetEvents.class),
            GetEvents.Request.newBuilder().setSkip(skip).setLimit(limit).build(),
            new DeliveryOptions().setSendTimeout(2000L), responseHandler(future, GetEvents.Response.class));
    try {/*from   w w  w  . ja va 2s .  c  om*/
        final GetEvents.Response response = future.get(2, TimeUnit.SECONDS);
        return JsonUtils.toVertxJsonObject(Json.createObjectBuilder().add("count", response.getEventsCount())
                .add("records", ProtobufUtils.protobuMessageToJson(response)).build()).encodePrettily();
    } catch (final InterruptedException | ExecutionException | TimeoutException ex) {
        log.logp(SEVERE, getClass().getName(), "createEventLogRecord", "failed", ex);
        throw new RuntimeException("Failed to create event log record : " + ex.getMessage());
    }
}

From source file:com.diabolicallabs.process.manager.service.KnowledgeServiceFactoryVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    try {/*from  w w  w .  j av  a  2s .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 "getKnowledgeService": {
            service.getKnowledgeService(res -> {
                if (res.failed()) {
                    if (res.cause() instanceof ServiceException) {
                        msg.reply(res.cause());
                    } else {
                        msg.reply(new ServiceException(-1, res.cause().getMessage()));
                    }
                } else {
                    String proxyAddress = UUID.randomUUID().toString();
                    ProxyHelper.registerService(KnowledgeService.class, vertx, res.result(), proxyAddress,
                            false, timeoutSeconds);
                    msg.reply(null, new DeliveryOptions().addHeader("proxyaddr", proxyAddress));
                }
            });
            break;
        }
        default: {
            throw new IllegalStateException("Invalid action: " + action);
        }
        }
    } catch (Throwable t) {
        msg.reply(new ServiceException(500, t.getMessage()));
        throw t;
    }
}

From source file:com.diabolicallabs.process.manager.service.KnowledgeServiceVertxProxyHandler.java

License:Apache License

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

        case "addClassPathResource": {
            service.addClassPathResource((java.lang.String) json.getValue("resourceName"), createHandler(msg));
            break;
        }
        case "addFileResource": {
            service.addFileResource((java.lang.String) json.getValue("fileName"), createHandler(msg));
            break;
        }
        case "processDefinitions": {
            service.processDefinitions(createHandler(msg));
            break;
        }
        case "getProcessService": {
            service.getProcessService(res -> {
                if (res.failed()) {
                    if (res.cause() instanceof ServiceException) {
                        msg.reply(res.cause());
                    } else {
                        msg.reply(new ServiceException(-1, res.cause().getMessage()));
                    }
                } else {
                    String proxyAddress = UUID.randomUUID().toString();
                    ProxyHelper.registerService(ProcessService.class, vertx, res.result(), proxyAddress, false,
                            timeoutSeconds);
                    msg.reply(null, new DeliveryOptions().addHeader("proxyaddr", proxyAddress));
                }
            });
            break;
        }
        case "getRuleService": {
            service.getRuleService(res -> {
                if (res.failed()) {
                    if (res.cause() instanceof ServiceException) {
                        msg.reply(res.cause());
                    } else {
                        msg.reply(new ServiceException(-1, res.cause().getMessage()));
                    }
                } else {
                    String proxyAddress = UUID.randomUUID().toString();
                    ProxyHelper.registerService(RuleService.class, vertx, res.result(), proxyAddress, false,
                            timeoutSeconds);
                    msg.reply(null, new DeliveryOptions().addHeader("proxyaddr", proxyAddress));
                }
            });
            break;
        }
        case "getTaskService": {
            service.getTaskService(res -> {
                if (res.failed()) {
                    if (res.cause() instanceof ServiceException) {
                        msg.reply(res.cause());
                    } else {
                        msg.reply(new ServiceException(-1, res.cause().getMessage()));
                    }
                } else {
                    String proxyAddress = UUID.randomUUID().toString();
                    ProxyHelper.registerService(TaskService.class, vertx, res.result(), proxyAddress, false,
                            timeoutSeconds);
                    msg.reply(null, new DeliveryOptions().addHeader("proxyaddr", proxyAddress));
                }
            });
            break;
        }
        case "getTaskServiceAddress": {
            service.getTaskServiceAddress(createHandler(msg));
            break;
        }
        case "close": {
            service.close();
            close();
            break;
        }
        default: {
            throw new IllegalStateException("Invalid action: " + action);
        }
        }
    } catch (Throwable t) {
        msg.reply(new ServiceException(500, t.getMessage()));
        throw t;
    }
}

From source file:com.diabolicallabs.process.manager.service.ProcessServiceVertxProxyHandler.java

License:Apache License

public void handle(Message<JsonObject> msg) {
    try {//w  w  w  .j av 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 "abort": {
            service.abort(json.getValue("processInstanceId") == null ? null
                    : (json.getLong("processInstanceId").longValue()), createHandler(msg));
            break;
        }
        case "create": {
            service.create((java.lang.String) json.getValue("processId"), res -> {
                if (res.failed()) {
                    if (res.cause() instanceof ServiceException) {
                        msg.reply(res.cause());
                    } else {
                        msg.reply(new ServiceException(-1, res.cause().getMessage()));
                    }
                } else {
                    String proxyAddress = UUID.randomUUID().toString();
                    ProxyHelper.registerService(ProcessInstanceService.class, vertx, res.result(), proxyAddress,
                            false, timeoutSeconds);
                    msg.reply(null, new DeliveryOptions().addHeader("proxyaddr", proxyAddress));
                }
            });
            break;
        }
        case "createWithVariables": {
            service.createWithVariables((java.lang.String) json.getValue("processId"),
                    (io.vertx.core.json.JsonObject) json.getValue("variables"), res -> {
                        if (res.failed()) {
                            if (res.cause() instanceof ServiceException) {
                                msg.reply(res.cause());
                            } else {
                                msg.reply(new ServiceException(-1, res.cause().getMessage()));
                            }
                        } else {
                            String proxyAddress = UUID.randomUUID().toString();
                            ProxyHelper.registerService(ProcessInstanceService.class, vertx, res.result(),
                                    proxyAddress, false, timeoutSeconds);
                            msg.reply(null, new DeliveryOptions().addHeader("proxyaddr", proxyAddress));
                        }
                    });
            break;
        }
        case "signalEvent": {
            service.signalEvent((java.lang.String) json.getValue("eventName"),
                    (io.vertx.core.json.JsonObject) json.getValue("data"), createHandler(msg));
            break;
        }
        case "signalEventForProcess": {
            service.signalEventForProcess((java.lang.String) json.getValue("eventName"),
                    json.getValue("processInstanceId") == null ? null
                            : (json.getLong("processInstanceId").longValue()),
                    (io.vertx.core.json.JsonObject) json.getValue("data"), createHandler(msg));
            break;
        }
        case "startProcess": {
            service.startProcess((java.lang.String) json.getValue("processId"), res -> {
                if (res.failed()) {
                    if (res.cause() instanceof ServiceException) {
                        msg.reply(res.cause());
                    } else {
                        msg.reply(new ServiceException(-1, res.cause().getMessage()));
                    }
                } else {
                    String proxyAddress = UUID.randomUUID().toString();
                    ProxyHelper.registerService(ProcessInstanceService.class, vertx, res.result(), proxyAddress,
                            false, timeoutSeconds);
                    msg.reply(null, new DeliveryOptions().addHeader("proxyaddr", proxyAddress));
                }
            });
            break;
        }
        case "startProcessWithVariables": {
            service.startProcessWithVariables((java.lang.String) json.getValue("processId"),
                    (io.vertx.core.json.JsonObject) json.getValue("jsonObject"), res -> {
                        if (res.failed()) {
                            if (res.cause() instanceof ServiceException) {
                                msg.reply(res.cause());
                            } else {
                                msg.reply(new ServiceException(-1, res.cause().getMessage()));
                            }
                        } else {
                            String proxyAddress = UUID.randomUUID().toString();
                            ProxyHelper.registerService(ProcessInstanceService.class, vertx, res.result(),
                                    proxyAddress, false, timeoutSeconds);
                            msg.reply(null, new DeliveryOptions().addHeader("proxyaddr", proxyAddress));
                        }
                    });
            break;
        }
        default: {
            throw new IllegalStateException("Invalid action: " + action);
        }
        }
    } catch (Throwable t) {
        msg.reply(new ServiceException(500, t.getMessage()));
        throw t;
    }
}