List of usage examples for io.vertx.core.eventbus Message body
@CacheReturn T body();
From source file:org.jberet.vertx.cluster.VertxPartitionHandler.java
License:Open Source License
public VertxPartitionHandler(final StepContextImpl stepContext, final Vertx vertx) { this.vertx = vertx; this.eventBus = vertx.eventBus(); Handler<Message<Buffer>> receivingResultHandler = new Handler<Message<Buffer>>() { public void handle(Message<Buffer> message) { Buffer body = message.body(); final Serializable partitionCollectorData; try { partitionCollectorData = BatchUtil.bytesToSerializableObject(body.getBytes(), stepContext.getJobContext().getClassLoader()); } catch (Exception e) { throw VertxClusterMessages.MESSAGES.failedToReceivePartitionCollectorData(e); }/*from ww w . j a v a 2s. c o m*/ if (partitionCollectorData instanceof PartitionExecutionImpl) { if (completedPartitionThreads != null) { completedPartitionThreads.offer(Boolean.TRUE); } final PartitionExecutionImpl partitionExecution = (PartitionExecutionImpl) partitionCollectorData; final int partitionId = partitionExecution.getPartitionId(); VertxClusterLogger.LOGGER.receivedPartitionResult(stepContext.getJobContext().getExecutionId(), stepContext.getStepExecutionId(), partitionId, partitionExecution.getBatchStatus()); //put the partition execution from remote node into its enclosing step execution. //The original partition execution stored in step execution now are obsolete. final List<PartitionExecutionImpl> partitionExecutions = stepContext.getStepExecution() .getPartitionExecutions(); for (int i = 0; i < partitionExecutions.size(); i++) { if (partitionExecutions.get(i).getPartitionId() == partitionId) { partitionExecutions.remove(i); partitionExecutions.add(partitionExecution); } } } try { collectorDataQueue.put(partitionCollectorData); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } }; final long stepExecutionId = stepContext.getStepExecutionId(); eventBus.consumer(PartitionInfo.getCollectorQueueName(stepExecutionId), receivingResultHandler); }
From source file:org.mycontroller.standalone.eventbus.MessageStatusHandler.java
License:Apache License
@Override public void handle(Message<MessageStatus> event) { statusMessage = event.body(); _logger.debug("Event received: [address:{}, body:{}]", event.address(), event.body()); }
From source file:org.sub.bug.BugCRUDServiceVertxProxyHandler.java
License:Apache License
public void handle(Message<JsonObject> msg) { try {/*from w w w . j av a2 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 "saveBug": { service.saveBug(json.getJsonObject("bug") == null ? null : new org.sub.bug.entity.Bug(json.getJsonObject("bug")), createHandler(msg)); break; } case "retrieveBug": { service.retrieveBug((java.lang.String) json.getValue("bugId"), res -> { if (res.failed()) { if (res.cause() instanceof ServiceException) { msg.reply(res.cause()); } else { msg.reply(new ServiceException(-1, res.cause().getMessage())); } } else { msg.reply(res.result() == null ? null : res.result().toJson()); } }); break; } case "removeBug": { service.removeBug((java.lang.String) json.getValue("bugId"), createHandler(msg)); break; } default: { throw new IllegalStateException("Invalid action: " + action); } } } catch (Throwable t) { msg.reply(new ServiceException(500, t.getMessage())); throw t; } }
From source file:org.vertx.java.resourceadapter.OutboundTestVerticle.java
License:Apache License
public void start() { this.vertx.eventBus().consumer("outbound-address").handler((Message<Object> msg) -> { String string = (String) msg.body(); if (string != null && string.length() > 0) { msg.reply("Hello " + string + " from Outbound"); }/* www . j av a 2s .co m*/ }); }
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 . j a v a 2 s . c o m*/ 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
/** * Handles a executor register request message event by trying to register the executor with the * received information.//from w w w .j ava2s.com * The registry might fail due to incompatible versions * * @param message the request message event */ private void handlerRegisterEvent(final Message<Object> message) { LOGGER.debug("received register event message: {0}", message.body()); // validate the received event if (message.body() == null || !JsonObject.class.isInstance(message.body())) { message.fail(1, "invalid register event received"); return; } // try to register the executor final JsonObject executorRequest = (JsonObject) message.body(); executorRegistry.register(executorRequest, address -> message.reply(new JsonObject().put("a", address)), reason -> message.fail(1, String.format("unable to register executor: %s", reason))); }
From source file:pt.davidafsilva.slacker.server.EventServerVerticle.java
License:Open Source License
/** * Handles a request message event by delivering the request to the appropriate executor, if * any is registered to handle that particular type of request. * * @param message the request message event *//*ww w . j a v a 2s . c o m*/ private void handlerRequestEvent(final Message<Object> message) { LOGGER.debug("received request event message: {0}", message.body()); // validate the received event if (message.body() == null || !SlackerRequest.class.isInstance(message.body())) { LOGGER.error("invalid event body"); message.fail(1, "invalid request event received"); return; } // handle the request final SlackerRequest request = (SlackerRequest) message.body(); executorRegistry.lookup(request.getCommand(), address -> sendRequestToExecutor(address, request, message), v -> message.fail(1, String.format("no executor available for the command: %s", request.getCommand()))); }
From source file:pt.davidafsilva.ushortx.persistence.DatabaseVerticle.java
License:Open Source License
/** * Queries the database for an url entry with the identifier specified in the message * * @param message the message from where to extract the identifier and to reply from *//*from ww w. j a va 2s. c om*/ private void findById(final Message<JsonObject> message) { LOGGER.info("incoming find request: " + message.body()); connect(connection -> { // validate the identifier final Optional<Long> id = Optional.ofNullable(message.body().getLong("id")); if (!id.isPresent()) { connection.close(); message.fail(2, "invalid identifier"); return; } // create the query parameters final JsonArray queryParams = new JsonArray().add(id.get()); // execute the query connection.queryWithParams(FIND_BY_ID_QUERY, queryParams, FIND_QUERY_RESULT_HANDLER.apply(message, connection)); }, Optional.of(cause -> message.fail(1, "unavailable resources"))); }
From source file:pt.davidafsilva.ushortx.persistence.DatabaseVerticle.java
License:Open Source License
/** * Saves at the database the url specified in the message, if non-existent. Otherwise the same * entry is used.// w w w .j a va 2s . c o m * * @param message the message from where to extract the url data and to reply from */ private void saveUrl(final Message<JsonObject> message) { LOGGER.info("incoming save request: " + message.body()); connect(connection -> { // validate the url final Optional<String> url = Optional.ofNullable(message.body().getString("url")); if (!url.isPresent()) { connection.close(); message.fail(2, "invalid url"); return; } // create the update parameters final JsonArray updateParams = new JsonArray().add(url.get()); // execute the update connection.updateWithParams(INSERT_URL_STATEMENT, updateParams, INSERT_URL_RESULT_HANDLER.apply(message, connection)); }, Optional.of(cause -> message.fail(1, "unavailable resources"))); }
From source file:scp.targets.vertx.CommunicationManagerImpl.java
License:Open Source License
@Override public <T extends Serializable> Status registerSubscriber(@NonNull SubscriberConfiguration<T> configuration) { final SubscriberInvoker<T> _subscriberInvoker = new SubscriberInvoker<>(configuration); this.eventBus.consumer(configuration.topic, (Message<byte[]> msg) -> _subscriberInvoker.processData(getData(msg.body()))); return Status.SUCCESS; }