Example usage for com.google.common.util.concurrent Futures successfulAsList

List of usage examples for com.google.common.util.concurrent Futures successfulAsList

Introduction

In this page you can find the example usage for com.google.common.util.concurrent Futures successfulAsList.

Prototype

@Beta
@CheckReturnValue
public static <V> ListenableFuture<List<V>> successfulAsList(
        Iterable<? extends ListenableFuture<? extends V>> futures) 

Source Link

Document

Creates a new ListenableFuture whose value is a list containing the values of all its successful input futures.

Usage

From source file:org.glassfish.jersey.examples.rx.agent.ListenableFutureAgentResource.java

private ListenableFuture<AgentResponse> recommended(final AgentResponse response) {
    // Destinations.
    final ListenableFuture<List<Destination>> recommended = RxListenableFuture.from(destination)
            .path("recommended").request()
            // Identify the user.
            .header("Rx-User", "Guava")
            // Reactive invoker.
            .rx()//from   www.j a v a  2 s  .c o  m
            // Return a list of destinations.
            .get(new GenericType<List<Destination>>() {
            });
    final ListenableFuture<List<Recommendation>> recommendations = Futures.transform(recommended,
            new AsyncFunction<List<Destination>, List<Recommendation>>() {
                @Override
                public ListenableFuture<List<Recommendation>> apply(final List<Destination> input)
                        throws Exception {
                    final List<Recommendation> recommendations = Lists
                            .newArrayList(Lists.transform(input, new Function<Destination, Recommendation>() {
                                @Override
                                public Recommendation apply(final Destination input) {
                                    return new Recommendation(input.getDestination(), null, 0);
                                }
                            }));
                    return Futures.immediateFuture(recommendations);
                }
            });

    final ListenableFuture<List<List<Recommendation>>> syncedFuture = Futures.successfulAsList(Arrays.asList(
            // Add Forecasts to Recommendations.
            forecasts(recommendations),
            // Add Forecasts to Recommendations.
            calculations(recommendations)));

    return Futures.transform(syncedFuture, new AsyncFunction<List<List<Recommendation>>, AgentResponse>() {
        @Override
        public ListenableFuture<AgentResponse> apply(final List<List<Recommendation>> input) throws Exception {
            response.setRecommended(input.get(0));
            return Futures.immediateFuture(response);
        }
    });
}

From source file:org.apache.crunch.io.impl.FileTargetImpl.java

@Override
public void handleOutputs(Configuration conf, Path workingPath, int index) throws IOException {
    FileSystem srcFs = workingPath.getFileSystem(conf);
    Path src = getSourcePattern(workingPath, index);
    Path[] srcs = FileUtil.stat2Paths(srcFs.globStatus(src), src);
    FileSystem dstFs = path.getFileSystem(conf);
    if (!dstFs.exists(path)) {
        dstFs.mkdirs(path);/*from   w  w  w  .  ja va2 s.co m*/
    }
    boolean sameFs = isCompatible(srcFs, path);
    List<ListenableFuture<Boolean>> renameFutures = Lists.newArrayList();
    ListeningExecutorService executorService = MoreExecutors.listeningDecorator(
            Executors.newFixedThreadPool(conf.getInt(RuntimeParameters.FILE_TARGET_MAX_THREADS, 1)));
    for (Path s : srcs) {
        Path d = getDestFile(conf, s, path, s.getName().contains("-m-"));
        renameFutures.add(executorService.submit(new WorkingPathFileMover(conf, s, d, srcFs, dstFs, sameFs)));
    }
    LOG.debug("Renaming " + renameFutures.size() + " files.");

    ListenableFuture<List<Boolean>> future = Futures.successfulAsList(renameFutures);
    List<Boolean> renameResults = null;
    try {
        renameResults = future.get();
    } catch (InterruptedException | ExecutionException e) {
        Throwables.propagate(e);
    } finally {
        executorService.shutdownNow();
    }
    if (renameResults != null && !renameResults.contains(false)) {
        dstFs.create(getSuccessIndicator(), true).close();
        LOG.debug("Renamed " + renameFutures.size() + " files.");
    }
}

From source file:com.google.gerrit.server.index.ChangeBatchIndexer.java

public Result indexAll(ChangeIndex index, Iterable<Project.NameKey> projects, int numProjects, int numChanges,
        OutputStream progressOut, OutputStream verboseOut) {
    if (progressOut == null) {
        progressOut = NullOutputStream.INSTANCE;
    }//  w w w  .  j  a  va  2  s. co  m
    PrintWriter verboseWriter = verboseOut != null ? new PrintWriter(verboseOut) : null;

    Stopwatch sw = Stopwatch.createStarted();
    final MultiProgressMonitor mpm = new MultiProgressMonitor(progressOut, "Reindexing changes");
    final Task projTask = mpm.beginSubTask("projects",
            numProjects >= 0 ? numProjects : MultiProgressMonitor.UNKNOWN);
    final Task doneTask = mpm.beginSubTask(null, numChanges >= 0 ? numChanges : MultiProgressMonitor.UNKNOWN);
    final Task failedTask = mpm.beginSubTask("failed", MultiProgressMonitor.UNKNOWN);

    final List<ListenableFuture<?>> futures = Lists.newArrayList();
    final AtomicBoolean ok = new AtomicBoolean(true);

    for (final Project.NameKey project : projects) {
        if (!updateMergeable(project)) {
            ok.set(false);
        }
        final ListenableFuture<?> future = executor.submit(
                reindexProject(indexerFactory.create(index), project, doneTask, failedTask, verboseWriter));
        futures.add(future);
        future.addListener(new Runnable() {
            @Override
            public void run() {
                try {
                    future.get();
                } catch (InterruptedException e) {
                    fail(project, e);
                } catch (ExecutionException e) {
                    fail(project, e);
                } catch (RuntimeException e) {
                    failAndThrow(project, e);
                } catch (Error e) {
                    failAndThrow(project, e);
                } finally {
                    projTask.update(1);
                }
            }

            private void fail(Project.NameKey project, Throwable t) {
                log.error("Failed to index project " + project, t);
                ok.set(false);
            }

            private void failAndThrow(Project.NameKey project, RuntimeException e) {
                fail(project, e);
                throw e;
            }

            private void failAndThrow(Project.NameKey project, Error e) {
                fail(project, e);
                throw e;
            }
        }, MoreExecutors.sameThreadExecutor());
    }

    try {
        mpm.waitFor(Futures.transform(Futures.successfulAsList(futures), new AsyncFunction<List<?>, Void>() {
            @Override
            public ListenableFuture<Void> apply(List<?> input) {
                mpm.end();
                return Futures.immediateFuture(null);
            }
        }));
    } catch (ExecutionException e) {
        log.error("Error in batch indexer", e);
        ok.set(false);
    }
    return new Result(sw, ok.get(), doneTask.getCount(), failedTask.getCount());
}

From source file:com.dogecoin.dogecoinj.net.discovery.TorDiscovery.java

private List<Circuit> getCircuits(long timeoutValue, TimeUnit timeoutUnit, Set<Router> routers)
        throws InterruptedException {
    checkArgument(routers.size() >= MINIMUM_ROUTER_LOOKUP_COUNT,
            "Set of {} routers is smaller than required minimum {}", routers.size(),
            MINIMUM_ROUTER_LOOKUP_COUNT);
    createThreadPool(routers.size());// www  .  ja  v  a  2  s  .  c  o m

    try {
        List<ListenableFuture<Circuit>> circuitFutures = Lists.newArrayList();
        final CountDownLatch doneSignal = new CountDownLatch(MINIMUM_ROUTER_LOOKUP_COUNT);
        for (final Router router : routers) {
            ListenableFuture<Circuit> openCircuit = threadPool.submit(new Callable<Circuit>() {
                @Override
                public Circuit call() throws Exception {
                    return torClient.getCircuitManager().openInternalCircuitTo(Lists.newArrayList(router));
                }
            });
            Futures.addCallback(openCircuit, new FutureCallback<Circuit>() {
                public void onSuccess(Circuit circuit) {
                    doneSignal.countDown();
                }

                public void onFailure(Throwable thrown) {
                    doneSignal.countDown();
                }
            });
            circuitFutures.add(openCircuit);
        }

        boolean countedDown = doneSignal.await(timeoutValue, timeoutUnit);

        try {
            List<Circuit> circuits = new ArrayList<Circuit>(Futures.successfulAsList(circuitFutures).get());
            // Any failures will result in null entries.  Remove them.
            circuits.removeAll(singleton(null));
            int failures = routers.size() - circuits.size();
            if (failures > 0)
                log.warn("{} failures " + (countedDown ? "" : "(including timeout) ")
                        + "opening DNS lookup circuits", failures);
            return circuits;
        } catch (ExecutionException e) {
            // Cannot happen, successfulAsList accepts failures
            throw new RuntimeException(e);
        }
    } finally {
        shutdownThreadPool();
    }
}

From source file:org.glassfish.jersey.examples.rx.agent.ListenableFutureAgentResource.java

private ListenableFuture<List<Recommendation>> forecasts(
        final ListenableFuture<List<Recommendation>> recommendations) {
    return Futures.transform(recommendations, new AsyncFunction<List<Recommendation>, List<Recommendation>>() {
        @Override// ww w .ja v  a2s .  c  o  m
        public ListenableFuture<List<Recommendation>> apply(final List<Recommendation> input) throws Exception {
            final RxWebTarget<RxListenableFutureInvoker> rxForecast = RxListenableFuture.from(forecast);
            return Futures.successfulAsList(
                    Lists.transform(input, new Function<Recommendation, ListenableFuture<Recommendation>>() {
                        @Override
                        public ListenableFuture<Recommendation> apply(final Recommendation r) {
                            return Futures.transform(
                                    rxForecast.resolveTemplate("destination", r.getDestination()).request().rx()
                                            .get(Forecast.class),
                                    new AsyncFunction<Forecast, Recommendation>() {
                                        @Override
                                        public ListenableFuture<Recommendation> apply(final Forecast f)
                                                throws Exception {
                                            r.setForecast(f.getForecast());
                                            return Futures.immediateFuture(r);
                                        }
                                    });
                        }
                    }));
        }
    });
}

From source file:org.jclouds.digitalocean2.compute.strategy.CreateKeyPairsThenCreateNodes.java

private void registerAutoGeneratedKeyPairCleanupCallbacks(Map<?, ListenableFuture<Void>> responses,
        final Set<Integer> generatedSshKeyIds) {
    // The Futures.allAsList fails immediately if some of the futures fail. The Futures.successfulAsList, however,
    // returns a list containing the results or 'null' for those futures that failed. We want to wait for all them
    // (even if they fail), so better use the latter form.
    ListenableFuture<List<Void>> aggregatedResponses = Futures.successfulAsList(responses.values());

    // Key pairs must be cleaned up after all futures completed (even if some failed).
    Futures.addCallback(aggregatedResponses, new FutureCallback<List<Void>>() {
        @Override//  ww  w  .  jav  a 2 s.c o  m
        public void onSuccess(List<Void> result) {
            cleanupAutoGeneratedKeyPairs(generatedSshKeyIds);
        }

        @Override
        public void onFailure(Throwable t) {
            cleanupAutoGeneratedKeyPairs(generatedSshKeyIds);
        }

        private void cleanupAutoGeneratedKeyPairs(Set<Integer> generatedSshKeyIds) {
            logger.debug(">> cleaning up auto-generated key pairs...");
            for (Integer sshKeyId : generatedSshKeyIds) {
                try {
                    api.keyApi().delete(sshKeyId);
                } catch (Exception ex) {
                    logger.warn(">> could not delete key pair %s: %s", sshKeyId, ex.getMessage());
                }
            }
        }

    }, userExecutor);
}

From source file:com.google.bitcoin.net.discovery.TorDiscovery.java

private Collection<InetSocketAddress> lookupAddresses(long timeoutValue, TimeUnit timeoutUnit,
        List<Circuit> circuits) throws InterruptedException {
    createThreadPool(circuits.size() * hostNames.length);

    try {//from ww  w . j  a v  a  2  s  .  com
        List<ListenableFuture<Lookup>> lookupFutures = Lists.newArrayList();
        for (final Circuit circuit : circuits) {
            for (final String seed : hostNames) {
                lookupFutures.add(threadPool.submit(new Callable<Lookup>() {
                    public Lookup call() throws Exception {
                        return new Lookup(circuit.getFinalCircuitNode().getRouter(), lookup(circuit, seed));
                    }
                }));
            }
        }

        threadPool.awaitTermination(timeoutValue, timeoutUnit);
        int timeouts = 0;
        for (ListenableFuture<Lookup> future : lookupFutures) {
            if (!future.isDone()) {
                timeouts++;
                future.cancel(true);
            }
        }
        if (timeouts > 0)
            log.warn("{} DNS lookup circuits timed out", timeouts);

        try {
            List<Lookup> lookups = new ArrayList<Lookup>(Futures.successfulAsList(lookupFutures).get());
            // Any failures will result in null entries.  Remove them.
            lookups.removeAll(singleton(null));

            // Use a map to enforce one result per exit node
            // TODO: randomize result selection better
            Map<HexDigest, InetSocketAddress> lookupMap = Maps.newHashMap();

            for (Lookup lookup : lookups) {
                InetSocketAddress address = new InetSocketAddress(lookup.address, netParams.getPort());
                lookupMap.put(lookup.router.getIdentityHash(), address);
            }

            return lookupMap.values();
        } catch (ExecutionException e) {
            // Cannot happen, successfulAsList accepts failures
            throw new RuntimeException(e);
        }
    } finally {
        shutdownThreadPool();
    }
}

From source file:com.google.sha1coin.net.discovery.TorDiscovery.java

private Collection<InetSocketAddress> lookupAddresses(long timeoutValue, TimeUnit timeoutUnit,
        List<Circuit> circuits) throws InterruptedException {
    createThreadPool(circuits.size() * hostNames.length);

    try {//from w ww .  j  a  va2 s .  c o  m
        List<ListenableFuture<Lookup>> lookupFutures = Lists.newArrayList();
        for (final Circuit circuit : circuits) {
            for (final String seed : hostNames) {
                lookupFutures.add(threadPool.submit(new Callable<Lookup>() {
                    @Override
                    public Lookup call() throws Exception {
                        return new Lookup(circuit.getFinalCircuitNode().getRouter(), lookup(circuit, seed));
                    }
                }));
            }
        }

        threadPool.awaitTermination(timeoutValue, timeoutUnit);
        int timeouts = 0;
        for (ListenableFuture<Lookup> future : lookupFutures) {
            if (!future.isDone()) {
                timeouts++;
                future.cancel(true);
            }
        }
        if (timeouts > 0)
            log.warn("{} DNS lookup circuits timed out", timeouts);

        try {
            List<Lookup> lookups = new ArrayList<Lookup>(Futures.successfulAsList(lookupFutures).get());
            // Any failures will result in null entries.  Remove them.
            lookups.removeAll(singleton(null));

            // Use a map to enforce one result per exit node
            // TODO: randomize result selection better
            Map<HexDigest, InetSocketAddress> lookupMap = Maps.newHashMap();

            for (Lookup lookup : lookups) {
                InetSocketAddress address = new InetSocketAddress(lookup.address, netParams.getPort());
                lookupMap.put(lookup.router.getIdentityHash(), address);
            }

            return lookupMap.values();
        } catch (ExecutionException e) {
            // Cannot happen, successfulAsList accepts failures
            throw new RuntimeException(e);
        }
    } finally {
        shutdownThreadPool();
    }
}

From source file:org.jclouds.digitalocean.compute.strategy.CreateKeyPairsThenCreateNodes.java

private void registerAutoGeneratedKeyPairCleanupCallbacks(Map<?, ListenableFuture<Void>> responses,
        final Set<Integer> generatedSshKeyIds) {
    // The Futures.allAsList fails immediately if some of the futures fail. The Futures.successfulAsList, however,
    // returns a list containing the results or 'null' for those futures that failed. We want to wait for all them
    // (even if they fail), so better use the latter form.
    ListenableFuture<List<Void>> aggregatedResponses = Futures.successfulAsList(responses.values());

    // Key pairs must be cleaned up after all futures completed (even if some failed).
    Futures.addCallback(aggregatedResponses, new FutureCallback<List<Void>>() {
        @Override//from  w ww  . jav a2 s  .  c om
        public void onSuccess(List<Void> result) {
            cleanupAutoGeneratedKeyPairs(generatedSshKeyIds);
        }

        @Override
        public void onFailure(Throwable t) {
            cleanupAutoGeneratedKeyPairs(generatedSshKeyIds);
        }

        private void cleanupAutoGeneratedKeyPairs(Set<Integer> generatedSshKeyIds) {
            logger.debug(">> cleaning up auto-generated key pairs...");
            for (Integer sshKeyId : generatedSshKeyIds) {
                try {
                    api.getKeyPairApi().delete(sshKeyId);
                } catch (Exception ex) {
                    logger.warn(">> could not delete key pair %s: %s", sshKeyId, ex.getMessage());
                }
            }
        }

    }, userExecutor);
}

From source file:com.continuuity.loom.common.zookeeper.lib.SynchronizedZKMap.java

private void reloadCacheIfNeeded() {
    NodeChildren nodeChildren = Futures.getUnchecked(ZKClientExt.getChildrenOrNull(zkClient, ENTRIES_PATH));
    if (nodeChildren == null) {
        if (currentView.size() > 0) {
            currentView = Collections.emptyMap();
        }/*ww w  . ja v a  2  s .  com*/

        return;
    }

    // we use children version to detect if we need to update local view
    int trueVersion = nodeChildren.getStat().getCversion();

    if (currentViewVersion == trueVersion) {
        return;
    }

    List<String> nodes = nodeChildren.getChildren();
    final Map<String, ListenableFuture<NodeData>> nodeAndDataFutures = Maps.newHashMap();
    List<OperationFuture<NodeData>> dataFutures = Lists.newArrayList();
    for (String node : nodes) {
        OperationFuture<NodeData> dataFuture = zkClient.getData(getNodePath(node));
        dataFutures.add(dataFuture);
        nodeAndDataFutures.put(node, dataFuture);
    }

    Futures.getUnchecked(Futures.successfulAsList(dataFutures));

    ImmutableMap.Builder<String, T> builder = ImmutableMap.builder();
    for (Entry<String, ListenableFuture<NodeData>> nodeAndData : nodeAndDataFutures.entrySet()) {
        T value = serializer.deserialize(Futures.getUnchecked(nodeAndData.getValue()).getData());
        builder.put(nodeAndData.getKey(), value);
    }

    currentView = builder.build();
    currentViewVersion = trueVersion;
}