Java Code Examples for io.vertx.core.CompositeFuture#join()

The following examples show how to use io.vertx.core.CompositeFuture#join() . 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: DataLoader.java    From vertx-dataloader with Apache License 2.0 6 votes vote down vote up
/**
 * Dispatches the queued load requests to the batch execution function and returns a composite future of the result.
 * <p>
 * If batching is disabled, or there are no queued requests, then a succeeded composite future is returned.
 *
 * @return the composite future of the queued load requests
 */
public CompositeFuture dispatch() {
    if (!loaderOptions.batchingEnabled() || loaderQueue.size() == 0) {
        return CompositeFuture.join(Collections.emptyList());
    }
    CompositeFuture batch = batchLoadFunction.load(loaderQueue.keySet());
    dispatchedQueues.put(batch, new LinkedHashMap<>(loaderQueue));
    batch.setHandler(rh -> {
        AtomicInteger index = new AtomicInteger(0);
        dispatchedQueues.get(batch).forEach((key, future) -> {
            if (batch.succeeded(index.get())) {
                future.complete(batch.resultAt(index.get()));
            } else {
                future.fail(batch.cause(index.get()));
            }
            index.incrementAndGet();
        });
        dispatchedQueues.remove(batch);
    });
    loaderQueue.clear();
    return batch;
}
 
Example 2
Source File: DataLoaderTest.java    From vertx-dataloader with Apache License 2.0 6 votes vote down vote up
@Test
public void should_Build_a_really_really_simple_data_loader() {
    AtomicBoolean success = new AtomicBoolean();
    DataLoader<Integer, Integer> identityLoader = new DataLoader<>(keys ->
        CompositeFuture.join(keys.stream()
                .map(Future::succeededFuture)
                .collect(Collectors.toCollection(ArrayList::new))));

    Future<Integer> future1 = identityLoader.load(1);
    future1.setHandler(rh -> {
        assertThat(rh.result(), equalTo(1));
        success.set(rh.succeeded());
    });
    identityLoader.dispatch();
    await().untilAtomic(success, is(true));
}
 
Example 3
Source File: DataLoaderTest.java    From vertx-dataloader with Apache License 2.0 6 votes vote down vote up
@Test
public void should_Support_loading_multiple_keys_in_one_call() {
    AtomicBoolean success = new AtomicBoolean();
    DataLoader<Integer, Integer> identityLoader = new DataLoader<>(keys ->
            CompositeFuture.join(keys.stream()
                    .map(Future::succeededFuture)
                    .collect(Collectors.toCollection(ArrayList::new))));

    CompositeFuture futureAll = identityLoader.loadMany(asList(1, 2));
    futureAll.setHandler(rh -> {
        assertThat(rh.result().size(), is(2));
        success.set(rh.succeeded());
    });
    identityLoader.dispatch();
    await().untilAtomic(success, is(true));
    assertThat(futureAll.list(), equalTo(asList(1, 2)));
}
 
Example 4
Source File: DataLoaderTest.java    From vertx-dataloader with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <K, V> DataLoader<K, V> idLoader(DataLoaderOptions options, List<Collection> loadCalls) {
    return new DataLoader<>(keys -> {
        loadCalls.add(new ArrayList(keys));
        List<Future> futures = keys.stream().map(Future::succeededFuture).collect(Collectors.toList());
        return CompositeFuture.join(futures);
    }, options);
}
 
Example 5
Source File: DataLoaderTest.java    From vertx-dataloader with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <K, V> DataLoader<K, V> idLoaderAllErrors(
        DataLoaderOptions options, List<Collection> loadCalls) {
    return new DataLoader<>(keys -> {
        loadCalls.add(new ArrayList(keys));
        List<Future> futures = keys.stream()
                .map(key -> Future.failedFuture(new IllegalStateException("Error")))
                .collect(Collectors.toList());
        return CompositeFuture.join(futures);
    }, options);
}
 
Example 6
Source File: DataLoaderTest.java    From vertx-dataloader with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static DataLoader<Integer, Integer> idLoaderWithErrors(
        DataLoaderOptions options, List<Collection> loadCalls) {
    return new DataLoader<>(keys -> {
        loadCalls.add(new ArrayList(keys));
        List<Future> futures = keys.stream()
                .map(key -> key % 2 == 0 ? Future.succeededFuture(key) :
                        Future.failedFuture(new IllegalStateException("Error")))
                .collect(Collectors.toList());
        return CompositeFuture.join(futures);
    }, options);
}
 
Example 7
Source File: StatefulSetOperator.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a future that completes when all the pods [0..replicas-1] in the given statefulSet are ready.
 */
protected Future<?> podReadiness(String namespace, StatefulSet desired, long pollInterval, long operationTimeoutMs) {
    final int replicas = desired.getSpec().getReplicas();
    List<Future> waitPodResult = new ArrayList<>(replicas);
    for (int i = 0; i < replicas; i++) {
        String podName = getPodName(desired, i);
        waitPodResult.add(podOperations.readiness(namespace, podName, pollInterval, operationTimeoutMs));
    }
    return CompositeFuture.join(waitPodResult);
}
 
Example 8
Source File: PostgresClient.java    From raml-module-builder with Apache License 2.0 5 votes vote down vote up
/**
 * Close all SQL clients stored in the connection pool.
 */
public static void closeAllClients() {
  @SuppressWarnings("rawtypes")
  List<Future> list = new ArrayList<>(connectionPool.size());
  // copy of values() because closeClient will delete them from connectionPool
  for (PostgresClient client : connectionPool.values().toArray(new PostgresClient [0])) {
    Promise<Object> promise = Promise.promise();
    list.add(promise.future());
    client.closeClient(f -> promise.complete());
  }

  CompositeFuture.join(list);
}
 
Example 9
Source File: Main.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
static CompositeFuture run(Vertx vertx, KubernetesClient client, PlatformFeaturesAvailability pfa, ClusterOperatorConfig config) {
    Util.printEnvInfo();

    ResourceOperatorSupplier resourceOperatorSupplier = new ResourceOperatorSupplier(vertx, client, pfa, config.getOperationTimeoutMs());

    OpenSslCertManager certManager = new OpenSslCertManager();
    PasswordGenerator passwordGenerator = new PasswordGenerator(12,
            "abcdefghijklmnopqrstuvwxyz" +
                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
            "abcdefghijklmnopqrstuvwxyz" +
                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
                    "0123456789");
    KafkaAssemblyOperator kafkaClusterOperations = new KafkaAssemblyOperator(vertx, pfa,
            certManager, passwordGenerator, resourceOperatorSupplier, config);
    KafkaConnectAssemblyOperator kafkaConnectClusterOperations = new KafkaConnectAssemblyOperator(vertx, pfa,
            resourceOperatorSupplier, config);

    KafkaConnectS2IAssemblyOperator kafkaConnectS2IClusterOperations = null;
    if (pfa.supportsS2I()) {
        kafkaConnectS2IClusterOperations = new KafkaConnectS2IAssemblyOperator(vertx, pfa, resourceOperatorSupplier, config);
    } else {
        log.info("The KafkaConnectS2I custom resource definition can only be used in environment which supports OpenShift build, image and apps APIs. These APIs do not seem to be supported in this environment.");
    }

    KafkaMirrorMaker2AssemblyOperator kafkaMirrorMaker2AssemblyOperator =
            new KafkaMirrorMaker2AssemblyOperator(vertx, pfa, resourceOperatorSupplier, config);

    KafkaMirrorMakerAssemblyOperator kafkaMirrorMakerAssemblyOperator =
            new KafkaMirrorMakerAssemblyOperator(vertx, pfa, certManager, passwordGenerator, resourceOperatorSupplier, config);

    KafkaBridgeAssemblyOperator kafkaBridgeAssemblyOperator =
            new KafkaBridgeAssemblyOperator(vertx, pfa, certManager, passwordGenerator, resourceOperatorSupplier, config);

    KafkaRebalanceAssemblyOperator kafkaRebalanceAssemblyOperator =
            new KafkaRebalanceAssemblyOperator(vertx, pfa, resourceOperatorSupplier);

    List<Future> futures = new ArrayList<>(config.getNamespaces().size());
    for (String namespace : config.getNamespaces()) {
        Promise<String> prom = Promise.promise();
        futures.add(prom.future());
        ClusterOperator operator = new ClusterOperator(namespace,
                config.getReconciliationIntervalMs(),
                client,
                kafkaClusterOperations,
                kafkaConnectClusterOperations,
                kafkaConnectS2IClusterOperations,
                kafkaMirrorMakerAssemblyOperator,
                kafkaMirrorMaker2AssemblyOperator,
                kafkaBridgeAssemblyOperator,
                kafkaRebalanceAssemblyOperator,
                resourceOperatorSupplier.metricsProvider);
        vertx.deployVerticle(operator,
            res -> {
                if (res.succeeded()) {
                    log.info("Cluster Operator verticle started in namespace {}", namespace);
                } else {
                    log.error("Cluster Operator verticle in namespace {} failed to start", namespace, res.cause());
                    System.exit(1);
                }
                prom.handle(res);
            });
    }
    return CompositeFuture.join(futures);
}
 
Example 10
Source File: TopicOperator.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T> CompositeFuture join(List<T> futures) {
    return CompositeFuture.join((List) futures);
}
 
Example 11
Source File: CommandSubscriptionsManager.java    From hono with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Closes the command consumers and removes all the subscription entries.
 *
 * @param onConsumerRemovedFunction The function to be invoked if not {@code null} during removal of a subscription.
 *                                  The first parameter is the tenant id, the second parameter the device id.
 *                                  To be returned is a future indicating the outcome of the function.
 * @param spanContext The span context (may be {@code null}).
 * @return A future indicating the outcome of the operation.            
 **/
public CompositeFuture removeAllSubscriptions(
        final BiFunction<String, String, Future<Void>> onConsumerRemovedFunction, final SpanContext spanContext) {
    @SuppressWarnings("rawtypes")
    final List<Future> removalFutures = subscriptions.keySet().stream()
            .map(topic -> removeSubscription(topic, onConsumerRemovedFunction, spanContext)).collect(Collectors.toList());
    return CompositeFuture.join(removalFutures);
}
 
Example 12
Source File: DataLoader.java    From vertx-dataloader with Apache License 2.0 2 votes vote down vote up
/**
 * Requests to load the list of data provided by the specified keys asynchronously, and returns a composite future
 * of the resulting values.
 * <p>
 * If batching is enabled (the default), you'll have to call {@link DataLoader#dispatch()} at a later stage to
 * start batch execution. If you forget this call the future will never be completed (unless already completed,
 * and returned from cache).
 *
 * @param keys the list of keys to load
 * @return the composite future of the list of values
 */
public CompositeFuture loadMany(List<K> keys) {
    return CompositeFuture.join(keys.stream().map(this::load).collect(Collectors.toList()));
}