private <T> Handler<AsyncResult<Set<T>>> createSetHandler(Message msg) {
   return 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(new JsonArray(new ArrayList<>(res.result())));
     }
   };
 }
 private Handler<AsyncResult<Set<Character>>> createSetCharHandler(Message msg) {
   return res -> {
     if (res.failed()) {
       if (res.cause() instanceof ServiceException) {
         msg.reply(res.cause());
       } else {
         msg.reply(new ServiceException(-1, res.cause().getMessage()));
       }
     } else {
       JsonArray arr = new JsonArray();
       for (Character chr : res.result()) {
         arr.add((int) chr);
       }
       msg.reply(arr);
     }
   };
 }
 private <T> Handler<AsyncResult<T>> createHandler(Message msg) {
   return res -> {
     if (res.failed()) {
       if (res.cause() instanceof ServiceException) {
         msg.reply(res.cause());
       } else {
         msg.reply(new ServiceException(-1, res.cause().getMessage()));
       }
     } else {
       if (res.result() != null && res.result().getClass().isEnum()) {
         msg.reply(((Enum) res.result()).name());
       } else {
         msg.reply(res.result());
       }
     }
   };
 }
Esempio n. 4
0
  public void getFr(Message<JsonObject> message) {

    DaoFactory.getDao()
        .getFrLangText(
            langText -> {
              message.reply(buildLangTextJson(langText));
            });
  }
 private <T> Handler<AsyncResult<Set<T>>> createSetHandler(Message msg) {
   return res -> {
     if (res.failed()) {
       msg.fail(-1, res.cause().getMessage());
     } else {
       msg.reply(new JsonArray(new ArrayList<>(res.result())));
     }
   };
 }
 private <T> Handler<AsyncResult<T>> createHandler(Message msg) {
   return res -> {
     if (res.failed()) {
       msg.fail(-1, res.cause().getMessage());
     } else {
       msg.reply(res.result());
     }
   };
 }
Esempio n. 7
0
  @Override
  public void process(Message<String> message) {
    Defer<Buffer> defer = Promises.<Buffer>defer();

    defer
        .promise()
        .then(buffer -> message.reply(buffer))
        .error(e -> ExceptionUtil.fail(message, e));
  }
 private Handler<AsyncResult<List<Character>>> createListCharHandler(Message msg) {
   return res -> {
     if (res.failed()) {
       msg.fail(-1, res.cause().getMessage());
     } else {
       JsonArray arr = new JsonArray();
       for (Character chr : res.result()) {
         arr.add((int) chr);
       }
       msg.reply(arr);
     }
   };
 }
 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;
   }
 }
  /**
   * 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;
   }
 }
  /** Iterate and deploy verticles */
  private void deployVerticle(final Message<JsonObject> event) {

    // iterate over all candidates to be deployed
    Set<String> candidates = this.workingCopy.fieldNames();

    // detach from underlying json
    Map<String, JsonObject> initiants = new HashMap<>();
    candidates.forEach(
        id -> {
          JsonObject info = this.workingCopy.getJsonObject(id);
          JsonArray dependsOn = info.getJsonArray("dependsOn");
          if (dependsOn != null && deployed.getList().containsAll(dependsOn.getList())
              || dependsOn == null
              || dependsOn.isEmpty()) {
            initiants.put(id, info);
          }
        });

    // remove the initiants
    initiants.keySet().forEach(id -> this.workingCopy.remove(id));

    // setup latch for the reply
    CountDownLatch latch = new CountDownLatch(initiants.size());
    if (initiants.isEmpty()) {
      event.reply(Boolean.TRUE);
      return;
    }

    // run over all dependencies
    initiants.forEach(
        (id, info) -> {

          // get the name of the verticle
          String name = info.getString("name");
          final JsonObject localConfig = new JsonObject();
          localConfig.mergeIn(globalConfig);
          localConfig.mergeIn(info.getJsonObject("config", new JsonObject()));

          Handler<AsyncResult<String>> handler =
              innerEvent -> {
                if (innerEvent.succeeded()) {
                  // add service to deployed-list
                  deployed.add(id);

                  // re-emit
                  vertx
                      .eventBus()
                      .send(
                          LOOPBACK,
                          workingCopy,
                          (AsyncResult<Message<Boolean>> recursiveReply) -> {
                            // always decrease latch
                            latch.countDown();

                            if (recursiveReply.succeeded() && recursiveReply.result().body()) {
                              if (latch.getCount() == 0) {
                                event.reply(recursiveReply.result().body() & Boolean.TRUE);
                              }
                            } else {
                              event.fail(500, this.getFailure(id, recursiveReply));
                            }
                          });

                } else {
                  event.fail(500, id + " >> " + innerEvent.cause().getMessage());
                }
              };

          LOG.log(Level.INFO, "Deploying: ''{0}''", new Object[] {id});
          DeploymentOptions deploymentOptions = new DeploymentOptions(info);
          vertx.deployVerticle(name, deploymentOptions.setConfig(localConfig), handler);
        });
  }
Esempio n. 13
0
 /**
  * Something has happened, so handle it.
  *
  * @param msg the event to handle
  */
 @Override
 public void handle(Message<Void> msg) {
   msg.reply(dependency.getClass().getName());
 }