Java Code Examples for io.vertx.core.Future#map()

The following examples show how to use io.vertx.core.Future#map() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CheckoutServiceImpl.java    From vertx-blueprint-microservice with Apache License 2.0 6 votes vote down vote up
private Future<JsonObject> getInventory(ProductTuple product, HttpClient client) {
  Future<Integer> future = Future.future();
  client.get("/" + product.getProductId(), response -> {
    if (response.statusCode() == 200) {
      response.bodyHandler(buffer -> {
        try {
          int inventory = Integer.valueOf(buffer.toString());
          future.complete(inventory);
        } catch (NumberFormatException ex) {
          future.fail(ex);
        }
      });
    } else {
      future.fail("not_found:" + product.getProductId());
    }
  })
    .exceptionHandler(future::fail)
    .end();
  return future.map(inv -> new JsonObject()
    .put("id", product.getProductId())
    .put("inventory", inv)
    .put("amount", product.getAmount()));
}
 
Example 2
Source File: DelegatingRegistrationAmqpEndpoint.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
private Future<Message> processAssertRequest(final Message request, final ResourceIdentifier targetAddress,
        final SpanContext spanContext) {

    final String tenantId = targetAddress.getTenantId();
    final String deviceId = MessageHelper.getDeviceId(request);
    final String gatewayId = MessageHelper.getGatewayId(request);

    final Span span = TracingHelper.buildServerChildSpan(tracer,
            spanContext,
            SPAN_NAME_ASSERT_DEVICE_REGISTRATION,
            getClass().getSimpleName()
    ).start();

    TracingHelper.setDeviceTags(span, tenantId, deviceId);

    final Future<Message> resultFuture;
    if (tenantId == null || deviceId == null) {
        TracingHelper.logError(span, "missing tenant and/or device");
        resultFuture = Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
    } else {

        final Future<RegistrationResult> result;
        if (gatewayId == null) {
            log.debug("asserting registration of device [tenant: {}, device-id: {}]", tenantId, deviceId);
            result = getService().assertRegistration(tenantId, deviceId, span);
        } else {
            TracingHelper.TAG_GATEWAY_ID.set(span, gatewayId);
            log.debug("asserting registration of device [tenant: {}, device-id: {}] for gateway [{}]",
                    tenantId, deviceId, gatewayId);
            result = getService().assertRegistration(tenantId, deviceId, gatewayId, span);
        }
        resultFuture = result.map(res -> RegistrationConstants.getAmqpReply(
                RegistrationConstants.REGISTRATION_ENDPOINT,
                tenantId,
                request,
                res
        ));
    }
    return finishSpanOnFutureCompletion(span, resultFuture);
}
 
Example 3
Source File: UserAccountVerticle.java    From vertx-blueprint-microservice with Apache License 2.0 5 votes vote down vote up
private Future<Void> deployRestVerticle() {
  Future<String> future = Future.future();
  vertx.deployVerticle(new RestUserAccountAPIVerticle(accountService),
    new DeploymentOptions().setConfig(config()),
    future.completer());
  return future.map(r -> null);
}
 
Example 4
Source File: StoreVerticle.java    From vertx-blueprint-microservice with Apache License 2.0 5 votes vote down vote up
private Future<Void> deployRestVerticle(StoreCRUDService service) {
  Future<String> future = Future.future();
  vertx.deployVerticle(new RestStoreAPIVerticle(service),
    new DeploymentOptions().setConfig(config()),
    future.completer());
  return future.map(r -> null);
}
 
Example 5
Source File: ProductVerticle.java    From vertx-blueprint-microservice with Apache License 2.0 5 votes vote down vote up
private Future<Void> deployRestService(ProductService service) {
  Future<String> future = Future.future();
  vertx.deployVerticle(new RestProductAPIVerticle(service),
    new DeploymentOptions().setConfig(config()),
    future.completer());
  return future.map(r -> null);
}
 
Example 6
Source File: ProductVerticle.java    From vertx-blueprint-microservice with Apache License 2.0 5 votes vote down vote up
private Future<Void> initProductDatabase(ProductService service) {
  Future<Void> initFuture = Future.future();
  service.initializePersistence(initFuture.completer());
  return initFuture.map(v -> {
    ExampleHelper.initData(vertx, config());
    return null;
  });
}
 
Example 7
Source File: OrderVerticle.java    From vertx-blueprint-microservice with Apache License 2.0 5 votes vote down vote up
private Future<Void> deployRestVerticle() {
  Future<String> future = Future.future();
  vertx.deployVerticle(new RestOrderAPIVerticle(orderService),
    new DeploymentOptions().setConfig(config()),
    future.completer());
  return future.map(r -> null);
}
 
Example 8
Source File: OrderVerticle.java    From vertx-blueprint-microservice with Apache License 2.0 5 votes vote down vote up
private Future<Void> prepareDispatcher() {
  Future<String> future = Future.future();
  vertx.deployVerticle(new RawOrderDispatcher(orderService),
    new DeploymentOptions().setConfig(config()),
    future.completer());
  return future.map(r -> null);
}
 
Example 9
Source File: CheckoutServiceImpl.java    From vertx-blueprint-microservice with Apache License 2.0 5 votes vote down vote up
/**
 * Check inventory for the current cart.
 *
 * @param cart shopping cart data object
 * @return async result
 */
private Future<JsonObject> checkAvailableInventory(ShoppingCart cart) {
  Future<List<JsonObject>> allInventories = getInventoryEndpoint().compose(client -> {
    List<Future<JsonObject>> futures = cart.getProductItems()
      .stream()
      .map(product -> getInventory(product, client))
      .collect(Collectors.toList());
    return Functional.allOfFutures(futures)
      .map(r -> {
        ServiceDiscovery.releaseServiceObject(discovery, client);
        return r;
      });
  });
  return allInventories.map(inventories -> {
    JsonObject result = new JsonObject();
    // get the list of products whose inventory is lower than the demand amount
    List<JsonObject> insufficient = inventories.stream()
      .filter(item -> item.getInteger("inventory") - item.getInteger("amount") < 0)
      .collect(Collectors.toList());
    // insufficient inventory exists
    if (insufficient.size() > 0) {
      String insufficientList = insufficient.stream()
        .map(item -> item.getString("id"))
        .collect(Collectors.joining(", "));
      result.put("message", String.format("Insufficient inventory available for product %s.", insufficientList))
        .put("res", false);
    } else {
      result.put("res", true);
    }
    return result;
  });
}
 
Example 10
Source File: CartVerticle.java    From vertx-blueprint-microservice with Apache License 2.0 5 votes vote down vote up
private Future<Void> deployRestVerticle() {
  Future<String> future = Future.future();
  vertx.deployVerticle(new RestShoppingAPIVerticle(shoppingCartService, checkoutService),
    new DeploymentOptions().setConfig(config()),
    future.completer());
  return future.map(r -> null);
}
 
Example 11
Source File: VaadinVerticle.java    From vertx-vaadin with MIT License 5 votes vote down vote up
private Future<Router> startupHttpServer(VertxVaadin vertxVaadin) {
    String mountPoint = vertxVaadin.config().mountPoint();
    HttpServerOptions serverOptions = new HttpServerOptions().setCompressionSupported(true);

    Router router = Router.router(vertx);
    router.mountSubRouter(mountPoint, vertxVaadin.router());

    httpServer = vertx.createHttpServer(serverOptions).requestHandler(router);
    Promise<HttpServer> promise = Promise.promise();
    Future<HttpServer> future = promise.future();
    future.setHandler(event -> {
        if (event.succeeded()) {
            log.info("Started vaadin verticle " + getClass().getName() + " on port " + event.result().actualPort());
        } else {
            log.error("Cannot start http server", event.cause());
        }
    });

    httpPort().setHandler(event -> {
        if (event.succeeded()) {
            httpServer.listen(event.result(), promise);
        } else {
            promise.fail(event.cause());
        }
    });

    return future.map(router);
}
 
Example 12
Source File: KafkaAssemblyOperator.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
Future<ReconciliationState> withZkDiff(Future<ReconcileResult<StatefulSet>> r) {
    return r.map(rr -> {
        this.zkDiffs = rr;
        return this;
    });
}
 
Example 13
Source File: KafkaAssemblyOperator.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
Future<ReconciliationState> withVoid(Future<?> r) {
    return r.map(this);
}
 
Example 14
Source File: KafkaAssemblyOperator.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
Future<ReconciliationState> withKafkaDiff(Future<ReconcileResult<StatefulSet>> r) {
    return r.map(rr -> {
        this.kafkaDiffs = rr;
        return this;
    });
}
 
Example 15
Source File: RestAPIVerticle.java    From vertx-blueprint-microservice with Apache License 2.0 3 votes vote down vote up
/**
 * Create http server for the REST service.
 *
 * @param router router instance
 * @param host   http host
 * @param port   http port
 * @return async result of the procedure
 */
protected Future<Void> createHttpServer(Router router, String host, int port) {
  Future<HttpServer> httpServerFuture = Future.future();
  vertx.createHttpServer()
    .requestHandler(router::accept)
    .listen(port, host, httpServerFuture.completer());
  return httpServerFuture.map(r -> null);
}
 
Example 16
Source File: HystrixDashboardProxyEurekaTest.java    From standalone-hystrix-dashboard with MIT License 3 votes vote down vote up
private static Future<Void> deployFakeEurekaVerticle() {
  final Future<String> handler = Future.future();

  vertx.deployVerticle(fakeEurekaVerticle(), handler.completer());

  return handler.map(i -> null);
}
 
Example 17
Source File: HystrixDashboardProxyEurekaTest.java    From standalone-hystrix-dashboard with MIT License 3 votes vote down vote up
private static Future<Void> deployDashboard() {
  final Future<String> future = Future.future();

  vertx.deployVerticle(HystrixDashboardVerticle.class.getName(), future.completer());

  return future.map(i -> null);
}