Example usage for com.google.common.util.concurrent ListeningExecutorService invokeAll

List of usage examples for com.google.common.util.concurrent ListeningExecutorService invokeAll

Introduction

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

Prototype

@Override
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException;

Source Link

Document

All elements in the returned list must be ListenableFuture instances.

Usage

From source file:org.apache.crunch.hadoop.mapreduce.lib.jobcontrol.CrunchJobControl.java

synchronized private void executeReadySeqDoFns() {
    Set<Target> unfinished = getUnfinishedTargets();
    Set<PipelineCallable<?>> oldPipelineCallables = activePipelineCallables;
    this.activePipelineCallables = Sets.newHashSet();
    List<Callable<PipelineCallable.Status>> callablesToRun = Lists.newArrayList();
    for (final PipelineCallable<?> pipelineCallable : oldPipelineCallables) {
        if (Sets.intersection(allPipelineCallables.get(pipelineCallable), unfinished).isEmpty()) {
            if (pipelineCallable.runSingleThreaded()) {
                try {
                    if (pipelineCallable.call() != PipelineCallable.Status.SUCCESS) {
                        failedCallables.add(pipelineCallable);
                    }//  www .  ja  v a 2 s . co m
                } catch (Throwable t) {
                    pipelineCallable.setMessage(t.getLocalizedMessage());
                    failedCallables.add(pipelineCallable);
                }
            } else {
                callablesToRun.add(pipelineCallable);
            }
        } else {
            // Still need to run this one
            activePipelineCallables.add(pipelineCallable);
        }
    }

    ListeningExecutorService es = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
    try {
        List<Future<PipelineCallable.Status>> res = es.invokeAll(callablesToRun);
        for (int i = 0; i < res.size(); i++) {
            if (res.get(i).get() != PipelineCallable.Status.SUCCESS) {
                failedCallables.add((PipelineCallable) callablesToRun.get(i));
            }
        }
    } catch (Throwable t) {
        t.printStackTrace();
        failedCallables.addAll((List) callablesToRun);
    } finally {
        es.shutdownNow();
    }
}

From source file:com.teradata.benchto.driver.execution.BenchmarkExecutionDriver.java

@SuppressWarnings("unchecked")
private List<QueryExecutionResult> executeQueries(Benchmark benchmark, int runs, boolean reportStatus) {
    ListeningExecutorService executorService = executorServiceFactory.create(benchmark.getConcurrency());
    try {//from w  w  w. j  ava2 s.  co m
        List<Callable<QueryExecutionResult>> queryExecutionCallables = buildQueryExecutionCallables(benchmark,
                runs, reportStatus);
        List<ListenableFuture<QueryExecutionResult>> executionFutures = (List) executorService
                .invokeAll(queryExecutionCallables);
        return Futures.allAsList(executionFutures).get();
    } catch (InterruptedException | ExecutionException e) {
        throw new BenchmarkExecutionException("Could not execute benchmark", e);
    } finally {
        executorService.shutdown();
    }
}

From source file:org.apache.crunch.impl.spark.SparkRuntime.java

private void runCallables(Set<Target> unfinished) {
    Set<PipelineCallable<?>> oldCallables = activePipelineCallables;
    activePipelineCallables = Sets.newHashSet();
    List<PipelineCallable<?>> callablesToRun = Lists.newArrayList();
    List<PipelineCallable<?>> failedCallables = Lists.newArrayList();
    for (PipelineCallable<?> pipelineCallable : oldCallables) {
        if (Sets.intersection(allPipelineCallables.get(pipelineCallable), unfinished).isEmpty()) {
            if (pipelineCallable.runSingleThreaded()) {
                try {
                    if (pipelineCallable.call() != PipelineCallable.Status.SUCCESS) {
                        failedCallables.add(pipelineCallable);
                    }/*from  ww w . ja  v a2s  .c o  m*/
                } catch (Throwable t) {
                    pipelineCallable.setMessage(t.getLocalizedMessage());
                    failedCallables.add(pipelineCallable);
                }
            } else {
                callablesToRun.add(pipelineCallable);
            }
        } else {
            // Still need to run this one
            activePipelineCallables.add(pipelineCallable);
        }
    }

    ListeningExecutorService es = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
    try {
        List<Future<PipelineCallable.Status>> res = es.invokeAll(callablesToRun);
        for (int i = 0; i < res.size(); i++) {
            if (res.get(i).get() != PipelineCallable.Status.SUCCESS) {
                failedCallables.add((PipelineCallable) callablesToRun.get(i));
            }
        }
    } catch (Throwable t) {
        t.printStackTrace();
        failedCallables.addAll((List) callablesToRun);
    } finally {
        es.shutdownNow();
    }

    if (!failedCallables.isEmpty()) {
        LOG.error("{} callable failure(s) occurred:", failedCallables.size());
        for (PipelineCallable<?> c : failedCallables) {
            LOG.error("{} : {}", c.getName(), c.getMessage());
        }
        status.set(Status.FAILED);
        set(PipelineResult.EMPTY);
        doneSignal.countDown();
    }
}

From source file:com.facebook.buck.maven.Resolver.java

private ImmutableSetMultimap<Path, Prebuilt> downloadArtifacts(final MutableDirectedGraph<Artifact> graph)
        throws ExecutionException, InterruptedException {
    ListeningExecutorService exec = MoreExecutors
            .listeningDecorator(Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors(),
                    new MostExecutors.NamedThreadFactory("artifact download")));

    @SuppressWarnings("unchecked")
    List<ListenableFuture<Map.Entry<Path, Prebuilt>>> results = (List<ListenableFuture<Map.Entry<Path, Prebuilt>>>) (List<?>) exec
            .invokeAll(graph.getNodes().stream().map(
                    artifact -> (Callable<Map.Entry<Path, Prebuilt>>) () -> downloadArtifact(artifact, graph))
                    .collect(MoreCollectors.toImmutableList()));

    try {/*from w  w w .ja  v a 2s  .  co m*/
        return ImmutableSetMultimap.<Path, Prebuilt>builder().orderValuesBy(Ordering.natural())
                .putAll(Futures.allAsList(results).get()).build();
    } finally {
        exec.shutdown();
    }
}

From source file:org.excalibur.driver.google.compute.GoogleCompute.java

@Override
public Instances createInstances(InstanceTemplate template) {
    Instances instances = new Instances();
    CountDownLatch latch = new CountDownLatch(template.getMaxCount());

    ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(this.executorService);
    List<DeployInstanceTask> tasks = Lists.newArrayList();

    final Instance model = createInstanceFromTemplate(template);

    for (int i = 0; i < template.getMaxCount(); i++) {
        Instance instance = model.clone();

        if (template.getMaxCount() > 1) {
            instance.setName(String.format("%s-%s", model.getName(), i + 1));
            instance.getDisks().get(0).getInitializeParams().setDiskName(instance.getName());
            instance.getDisks().get(0).setDeviceName(instance.getName());
        }/*from  ww  w .ja va 2  s .  c  o  m*/
        tasks.add(new DeployInstanceTask(instance, latch));
    }

    try {
        List<Future<VirtualMachine>> submittedTasks = listeningExecutorService.invokeAll(tasks);
        latch.await(5, TimeUnit.MINUTES);

        for (Future<VirtualMachine> f : submittedTasks) {
            instances.addInstance(f.get());
        }
    } catch (InterruptedException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (ExecutionException e) {
        LOGGER.error(e.getMessage(), e);
    }

    listeningExecutorService.shutdown();

    LOGGER.debug("Waiting the instances' ready state....");
    ThreadUtils.sleep(30 * 1000);

    LOGGER.debug("Created [{}] instance(s) of [{}/{}]", instances.size(), template.getMinCount(),
            template.getMaxCount());

    return instances;
}