コード例 #1
0
  private ExecutionResult<MessageResponse> validateAndInitialize() {
    if (message == null || !(message.body() instanceof JsonObject)) {
      LOGGER.error("Invalid message received, either null or body of message is not JsonObject ");
      return new ExecutionResult<>(
          MessageResponseFactory.createInvalidRequestResponse(),
          ExecutionResult.ExecutionStatus.FAILED);
    }

    userId = ((JsonObject) message.body()).getString(MessageConstants.MSG_USER_ID);
    if (!validateUser(userId)) {
      LOGGER.error("Invalid user id passed. Not authorized.");
      return new ExecutionResult<>(
          MessageResponseFactory.createForbiddenResponse(), ExecutionResult.ExecutionStatus.FAILED);
    }

    prefs = ((JsonObject) message.body()).getJsonObject(MessageConstants.MSG_KEY_PREFS);
    request = ((JsonObject) message.body()).getJsonObject(MessageConstants.MSG_HTTP_BODY);

    if (prefs == null || prefs.isEmpty()) {
      LOGGER.error("Invalid preferences obtained, probably not authorized properly");
      return new ExecutionResult<>(
          MessageResponseFactory.createForbiddenResponse(), ExecutionResult.ExecutionStatus.FAILED);
    }

    if (request == null) {
      LOGGER.error("Invalid JSON payload on Message Bus");
      return new ExecutionResult<>(
          MessageResponseFactory.createInvalidRequestResponse(),
          ExecutionResult.ExecutionStatus.FAILED);
    }

    // All is well, continue processing
    return new ExecutionResult<>(null, ExecutionResult.ExecutionStatus.CONTINUE_PROCESSING);
  }
コード例 #2
0
  public void syncResultExample(Vertx vertx) {

    EventBus eb = vertx.eventBus();

    // Send a message and get the reply synchronously

    Message<String> reply = awaitResult(h -> eb.send("someaddress", "ping", h));

    System.out.println("Received reply " + reply.body());
  }
コード例 #3
0
  public void streamExample(Vertx vertx) {

    EventBus eb = vertx.eventBus();

    HandlerReceiverAdaptor<Message<String>> adaptor = streamAdaptor();

    eb.<String>consumer("some-address").handler(adaptor);

    // Receive 10 messages from the consumer:
    for (int i = 0; i < 10; i++) {

      Message<String> received1 = adaptor.receive();

      System.out.println("got message: " + received1.body());
    }
  }
コード例 #4
0
 public void handle(Message<JsonObject> msg) {
   try {
     JsonObject json = msg.body();
     String action = msg.headers().get("action");
     if (action == null) {
       throw new IllegalStateException("action not specified");
     }
     accessed();
     switch (action) {
       case "checkin":
         {
           service.checkin(
               (java.lang.String) json.getValue("name"),
               json.getValue("lat") == null ? null : (json.getDouble("lat").doubleValue()),
               json.getValue("lon") == null ? null : (json.getDouble("lon").doubleValue()),
               createHandler(msg));
           break;
         }
       case "getCheckins":
         {
           service.getCheckins(
               res -> {
                 if (res.failed()) {
                   msg.fail(-1, res.cause().getMessage());
                 } else {
                   msg.reply(
                       new JsonArray(
                           res.result()
                               .stream()
                               .map(Checkin::toJson)
                               .collect(Collectors.toList())));
                 }
               });
           break;
         }
       default:
         {
           throw new IllegalStateException("Invalid action: " + action);
         }
     }
   } catch (Throwable t) {
     msg.fail(-1, t.getMessage());
     throw t;
   }
 }
コード例 #5
0
  public void fiberHandlerExample(Vertx vertx) {

    EventBus eb = vertx.eventBus();

    vertx
        .createHttpServer()
        .requestHandler(
            fiberHandler(
                req -> {

                  // Send a message to address and wait for a reply
                  Message<String> reply = awaitResult(h -> eb.send("some-address", "blah", h));

                  System.out.println("Got reply: " + reply.body());

                  // Now end the response
                  req.response().end("blah");
                }))
        .listen(8080, "localhost");
  }
コード例 #6
0
  /**
   * Handles an incoming request from the event bus
   *
   * @param request the request message to be handled
   */
  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());
                }
              });
        });
  }
 public void handle(Message<JsonObject> msg) {
   try {
     JsonObject json = msg.body();
     String action = msg.headers().get("action");
     if (action == null) {
       throw new IllegalStateException("action not specified");
     }
     accessed();
     switch (action) {
       case "m":
         {
           service.m();
           break;
         }
       default:
         {
           throw new IllegalStateException("Invalid action: " + action);
         }
     }
   } catch (Throwable t) {
     msg.reply(new ServiceException(500, t.getMessage()));
     throw t;
   }
 }