Example usage for java.util.concurrent CompletableFuture allOf

List of usage examples for java.util.concurrent CompletableFuture allOf

Introduction

In this page you can find the example usage for java.util.concurrent CompletableFuture allOf.

Prototype

public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs) 

Source Link

Document

Returns a new CompletableFuture that is completed when all of the given CompletableFutures complete.

Usage

From source file:org.eclipse.smarthome.binding.mqtt.generic.internal.handler.GenericThingHandler.java

/**
 * Subscribe on all channel static topics on all {@link ChannelState}s.
 * If subscribing on all channels worked, the thing is put ONLINE, else OFFLINE.
 *
 * @param connection A started broker connection
 *///from  ww w.  j a  va2 s .c  o m
@Override
protected CompletableFuture<@Nullable Void> start(MqttBrokerConnection connection) {
    List<CompletableFuture<@Nullable Void>> futures = channelStateByChannelUID.values().stream()
            .map(c -> c.start(connection, scheduler, 0)).collect(Collectors.toList());
    return CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])).thenRun(() -> {
        updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE);
    });
}

From source file:org.eclipse.smarthome.io.transport.mqtt.MqttBrokerConnection.java

/**
 * Unsubscribe from all topics/*from   ww w.  j a  va 2 s .com*/
 *
 * @return Returns a future that completes as soon as all subscriptions have been canceled.
 */
public CompletableFuture<Void> unsubscribeAll() {
    MqttAsyncClient client = this.client;
    List<CompletableFuture<Boolean>> futures = new ArrayList<>();
    if (client != null) {
        subscribers.forEach((topic, subList) -> {
            futures.add(unsubscribeRaw(client, topic));
        });
        subscribers.clear();
    }
    return CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()]));
}

From source file:org.eclipse.winery.accountability.AccountabilityManagerImpl.java

@Override
public CompletableFuture<Map<String, String>> storeState(Map<String, InputStream> files) {
    Objects.requireNonNull(files);
    Map<String, CompletableFuture<String>> allFuturesMap = new HashMap<>();

    for (Map.Entry<String, InputStream> entry : files.entrySet()) {
        allFuturesMap.put(entry.getKey(), storageProvider.store(entry.getValue()));
    }/*from   www  .j ava 2s  .  c  om*/

    return CompletableFuture
            // execute all futures on parallel
            .allOf(allFuturesMap.values().toArray(new CompletableFuture[allFuturesMap.size()]))
            // when all done, collect the results
            .thenApply((Void) -> {
                Map<String, String> result = new HashMap<>();

                for (Map.Entry<String, CompletableFuture<String>> entry : allFuturesMap.entrySet()) {
                    result.put(entry.getKey(), entry.getValue().join());
                }

                return result;
            });
}

From source file:org.hyperledger.fabric.sdkintegration.End2endMTIT.java

public void runFabricTest(final SampleStore sampleStore) throws Exception {

    ////////////////////////////
    // Setup client

    //Create instance of client.
    HFClient client = HFClient.createNewInstance();

    client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());

    ////////////////////////////
    //Construct and run the channels
    final Collection<CompletableFuture<BlockEvent.TransactionEvent>> futures = new ArrayList<>(2);
    //  CompletableFuture<BlockEvent.TransactionEvent>[] ii = new CompletableFuture[2];
    SampleOrg sampleOrg1 = testConfig.getIntegrationTestsSampleOrg("peerOrg1");
    Channel fooChannel = constructChannel(FOO_CHANNEL_NAME, client, sampleOrg1);
    futures.add(installInstantiate(fooChannel, client, sampleOrg1));

    SampleOrg sampleOrg2 = testConfig.getIntegrationTestsSampleOrg("peerOrg2");
    Channel barChannel = constructChannel(BAR_CHANNEL_NAME, client, sampleOrg2);
    futures.add(installInstantiate(barChannel, client, sampleOrg2));

    final CompletableFuture<Void> voidCompletableFuture = CompletableFuture
            .allOf(futures.toArray(new CompletableFuture[futures.size()]));
    voidCompletableFuture.thenApply(avoid -> {

        ArrayList<Thread> threads = new ArrayList<>();
        TestPair[] testPairs = { new TestPair(fooChannel, sampleOrg1), new TestPair(barChannel, sampleOrg2) };

        for (int i = 0; i < WORKER_COUNT; ++i) {

            Thread thread = new Thread(new Worker(i, client, testPairs));
            thread.setName("TCW_" + i);
            thread.setDaemon(true);/*from  w  w w. j a v  a 2  s  . c o  m*/
            thread.start();

            threads.add(thread);

            try {
                Thread.sleep(random.nextInt(3000)); // stage them to be doing different tasks
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

        threads.forEach(t -> {
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        return null;

    }).get();

    //        voidCompletableFuture.thenApply(() -> futures.stream()
    //                .map(CompletableFuture::join)
    //                .collect(Collectors.toList())
    //        );
    //
    //        CompletableFuture<BlockEvent.TransactionEvent>.allOf (futures.toArray())
    //                .thenApply(() -> futures.stream()
    //                        .map(CompletableFuture::join)
    //                        .collect(Collectors.toList())
    //                );

    //        //let bar channel just shutdown so we have both scenarios.
    //
    //        out("\nTraverse the blocks for chain %s ", barChannel.getName());
    //
    //        blockWalker(client, barChannel);
    //
    //        assertFalse(barChannel.isShutdown());
    //        assertTrue(barChannel.isInitialized());

    out("That's all folks!");

}

From source file:org.onosproject.store.primitives.impl.CopycatTransportClient.java

@Override
public CompletableFuture<Void> close() {
    return CompletableFuture
            .allOf(connections.stream().map(Connection::close).toArray(CompletableFuture[]::new));
}

From source file:ru.histone.v2.evaluator.Evaluator.java

private CompletableFuture<List<EvalNode>> iterate(ExpAstNode expNode, Context context, MapEvalNode objToIterate,
        EvalNode keyVarName, EvalNode valueVarName) {
    List<CompletableFuture<EvalNode>> futures = new ArrayList<>(objToIterate.getValue().size());
    int i = 0;/*from w ww.j  a v  a  2  s  .c  o  m*/
    for (Map.Entry<String, Object> entry : objToIterate.getValue().entrySet()) {
        Context iterableContext = getIterableContext(context, objToIterate, keyVarName, valueVarName, i, entry);
        futures.add(evaluateNode(expNode.getNode(3), iterableContext));
        i++;
    }
    return CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()]))
            .thenApply(v -> futures.stream().map(CompletableFuture::join).collect(Collectors.toList()));
}