Example usage for com.google.common.util.concurrent ListenableFuture get

List of usage examples for com.google.common.util.concurrent ListenableFuture get

Introduction

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

Prototype

V get() throws InterruptedException, ExecutionException;

Source Link

Document

Waits if necessary for the computation to complete, and then retrieves its result.

Usage

From source file:com.spotify.helios.cli.command.JobWatchCommand.java

@Override
int run(final Namespace options, final List<TargetAndClient> clients, final PrintStream out, final boolean json,
        final BufferedReader stdin) throws ExecutionException, InterruptedException, IOException {
    final boolean exact = options.getBoolean(exactArg.getDest());
    final List<String> prefixes = options.getList(prefixesArg.getDest());
    final String jobIdString = options.getString(jobsArg.getDest());
    final List<ListenableFuture<Map<JobId, Job>>> jobIdFutures = Lists.newArrayList();
    for (final TargetAndClient cc : clients) {
        jobIdFutures.add(cc.getClient().jobs(jobIdString));
    }//from   ww  w . ja  va 2  s  .c  o  m

    final Set<JobId> jobIds = Sets.newHashSet();
    for (final ListenableFuture<Map<JobId, Job>> future : jobIdFutures) {
        jobIds.addAll(future.get().keySet());
    }

    watchJobsOnHosts(out, exact, prefixes, jobIds, options.getInt(intervalArg.getDest()), clients);
    return 0;
}

From source file:com.github.nethad.clustermeister.provisioning.torque.commands.AddNodesCommand.java

@Override
public void execute(CommandLineArguments arguments) {
    if (isArgumentsCountFalse(arguments)) {
        return;//from w ww . j a v  a2  s. c om
    }

    Scanner scanner = arguments.asScanner();

    int numberOfNodes = scanner.nextInt();
    int numberOfCpusPerNode = scanner.nextInt();

    Collection<File> artifactsToPreload = DependencyConfigurationUtil
            .getConfiguredDependencies(getNodeManager().getConfiguration());

    final TorqueNodeConfiguration torqueNodeConfiguration = TorqueNodeConfiguration
            .configurationForNode(numberOfCpusPerNode, artifactsToPreload);

    ListenableFuture<Void> lastNode = null;
    for (int i = 0; i < numberOfNodes; i++) {
        lastNode = getNodeManager().addNode(torqueNodeConfiguration);
    }
    try {
        lastNode.get();
    } catch (InterruptedException ex) {
        logger.warn("Waited for last node to start up", ex);
    } catch (ExecutionException ex) {
        logger.warn("Waited for last node to start up", ex);
        //        } catch (TimeoutException ex) {
        //            logger.warn("Waited for last node to start up", ex);
    }
}

From source file:com.stratio.deep.cassandra.cql.DeepCqlRecordWriter.java

/**
 * Waits until all pending tasks completed.
 *///from ww  w . jav  a  2  s  .co m
private void waitForCompletion() {
    for (ListenableFuture<?> future : pendingTasks.values()) {
        try {
            future.get();
        } catch (InterruptedException | ExecutionException e) {
            LOG.error("[" + this + "] Error waiting for writes to complete: " + e.getMessage());
        }
    }
}

From source file:com.woollysammoth.nubitj.tools.WalletTool.java

private static void sendPaymentRequest(String location, boolean verifyPki) {
    if (location.startsWith("http") || location.startsWith("nubit")) {
        try {/*from   w w  w .  j a v a 2  s . c o  m*/
            ListenableFuture<PaymentSession> future;
            if (location.startsWith("http")) {
                future = PaymentSession.createFromUrl(location, verifyPki);
            } else {
                NubitURI paymentRequestURI = new NubitURI(location);
                future = PaymentSession.createFromNubitUri(paymentRequestURI, verifyPki);
            }
            PaymentSession session = future.get();
            if (session != null) {
                send(session);
            } else {
                System.err.println("Server returned null session");
                System.exit(1);
            }
        } catch (PaymentRequestException e) {
            System.err.println("Error creating payment session " + e.getMessage());
            System.exit(1);
        } catch (NubitURIParseException e) {
            System.err.println("Invalid nubit uri: " + e.getMessage());
            System.exit(1);
        } catch (InterruptedException e) {
            // Ignore.
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    } else {
        // Try to open the payment request as a file.
        FileInputStream stream = null;
        try {
            File paymentRequestFile = new File(location);
            stream = new FileInputStream(paymentRequestFile);
        } catch (Exception e) {
            System.err.println("Failed to open file: " + e.getMessage());
            System.exit(1);
        }
        try {
            paymentRequest = org.nubit.protocols.payments.Protos.PaymentRequest.newBuilder().mergeFrom(stream)
                    .build();
        } catch (IOException e) {
            System.err.println("Failed to parse payment request from file " + e.getMessage());
            System.exit(1);
        }
        PaymentSession session = null;
        try {
            session = new PaymentSession(paymentRequest, verifyPki);
        } catch (PaymentRequestException e) {
            System.err.println("Error creating payment session " + e.getMessage());
            System.exit(1);
        }
        send(session);
    }
}

From source file:com.google.vertcoin.tools.WalletTool.java

private static void sendPaymentRequest(String location, boolean verifyPki) {
    if (location.startsWith("http") || location.startsWith("vertcoin")) {
        try {//ww  w.  java2 s.c o m
            ListenableFuture<PaymentSession> future;
            if (location.startsWith("http")) {
                future = PaymentSession.createFromUrl(location, verifyPki);
            } else {
                BitcoinURI paymentRequestURI = new BitcoinURI(location);
                future = PaymentSession.createFromBitcoinUri(paymentRequestURI, verifyPki);
            }
            PaymentSession session = future.get();
            if (session != null) {
                send(session);
            } else {
                System.err.println("Server returned null session");
                System.exit(1);
            }
        } catch (PaymentRequestException e) {
            System.err.println("Error creating payment session " + e.getMessage());
            System.exit(1);
        } catch (BitcoinURIParseException e) {
            System.err.println("Invalid vertcoin uri: " + e.getMessage());
            System.exit(1);
        } catch (InterruptedException e) {
            // Ignore.
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    } else {
        // Try to open the payment request as a file.
        FileInputStream stream = null;
        try {
            File paymentRequestFile = new File(location);
            stream = new FileInputStream(paymentRequestFile);
        } catch (Exception e) {
            System.err.println("Failed to open file: " + e.getMessage());
            System.exit(1);
        }
        try {
            paymentRequest = org.bitcoin.protocols.payments.Protos.PaymentRequest.newBuilder().mergeFrom(stream)
                    .build();
        } catch (IOException e) {
            System.err.println("Failed to parse payment request from file " + e.getMessage());
            System.exit(1);
        }
        PaymentSession session = null;
        try {
            session = new PaymentSession(paymentRequest, verifyPki);
        } catch (PaymentRequestException e) {
            System.err.println("Error creating payment session " + e.getMessage());
            System.exit(1);
        }
        send(session);
    }
}

From source file:io.v.moments.model.AdConverterMoment.java

private void getFullImage(final MomentIfcClient client, final Moment moment) {
    try {/*  ww w .j  av  a2s  .  co  m*/
        ListenableFuture<byte[]> data = client.getFullImage(mV23Manager.contextWithTimeout(Deadline.FULL));
        moment.setPhoto(Kind.REMOTE, Style.FULL, data.get());
        signalChange(moment);
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
}

From source file:org.apache.hive.ptest.execution.Phase.java

private <T extends RemoteCommandResult> List<T> flatten(
        List<ListenableFuture<List<ListenableFuture<T>>>> futures) throws Exception {
    List<T> results = Lists.newArrayList();
    for (ListenableFuture<List<ListenableFuture<T>>> future : futures) {
        List<ListenableFuture<T>> result = future.get();
        if (result != null) {
            results.addAll(toListOfResults(result));
        }//  w w w. j ava2  s  . co m
    }
    return results;
}

From source file:org.robotninjas.barge.netty.GroupOfCounters.java

public void changeCluster(Collection<Replica> replicas) throws Exception {
    Optional<SimpleCounterMachine> leader = getLeader();
    if (!leader.isPresent()) {
        throw new IllegalStateException("No leader; can't reconfigure");
    }/*from   ww w . j  a va2s . com*/
    SimpleCounterMachine currentLeader = leader.get();

    RaftMembership oldMembership = currentLeader.getClusterMembership();
    RaftMembership newMembership = oldMembership.buildProposal(replicas);
    ListenableFuture<Boolean> future = currentLeader.setConfiguration(oldMembership, newMembership);
    Boolean result = future.get();
    if (!result) {
        throw new IllegalStateException("Failed to changed cluster");
    }
}

From source file:com.google.zetacoin.tools.WalletTool.java

private static void sendPaymentRequest(String location, boolean verifyPki) {
    if (location.startsWith("http") || location.startsWith("zetacoin")) {
        try {//ww  w  .  j a va2  s .co  m
            ListenableFuture<PaymentSession> future;
            if (location.startsWith("http")) {
                future = PaymentSession.createFromUrl(location, verifyPki);
            } else {
                BitcoinURI paymentRequestURI = new BitcoinURI(location);
                future = PaymentSession.createFromBitcoinUri(paymentRequestURI, verifyPki);
            }
            PaymentSession session = future.get();
            if (session != null) {
                send(session);
            } else {
                System.err.println("Server returned null session");
                System.exit(1);
            }
        } catch (PaymentRequestException e) {
            System.err.println("Error creating payment session " + e.getMessage());
            System.exit(1);
        } catch (BitcoinURIParseException e) {
            System.err.println("Invalid zetacoin uri: " + e.getMessage());
            System.exit(1);
        } catch (InterruptedException e) {
            // Ignore.
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    } else {
        // Try to open the payment request as a file.
        FileInputStream stream = null;
        try {
            File paymentRequestFile = new File(location);
            stream = new FileInputStream(paymentRequestFile);
        } catch (Exception e) {
            System.err.println("Failed to open file: " + e.getMessage());
            System.exit(1);
        }
        try {
            paymentRequest = org.zetacoin.protocols.payments.Protos.PaymentRequest.newBuilder()
                    .mergeFrom(stream).build();
        } catch (IOException e) {
            System.err.println("Failed to parse payment request from file " + e.getMessage());
            System.exit(1);
        }
        PaymentSession session = null;
        try {
            session = new PaymentSession(paymentRequest, verifyPki);
        } catch (PaymentRequestException e) {
            System.err.println("Error creating payment session " + e.getMessage());
            System.exit(1);
        }
        send(session);
    }
}

From source file:com.google.betacoin.tools.WalletTool.java

private static void sendPaymentRequest(String location, boolean verifyPki) {
    if (location.startsWith("http") || location.startsWith("betacoin")) {
        try {/*from w  w w .  j  ava2 s.c o m*/
            ListenableFuture<PaymentSession> future;
            if (location.startsWith("http")) {
                future = PaymentSession.createFromUrl(location, verifyPki);
            } else {
                BitcoinURI paymentRequestURI = new BitcoinURI(location);
                future = PaymentSession.createFromBitcoinUri(paymentRequestURI, verifyPki);
            }
            PaymentSession session = future.get();
            if (session != null) {
                send(session);
            } else {
                System.err.println("Server returned null session");
                System.exit(1);
            }
        } catch (PaymentRequestException e) {
            System.err.println("Error creating payment session " + e.getMessage());
            System.exit(1);
        } catch (BitcoinURIParseException e) {
            System.err.println("Invalid betacoin uri: " + e.getMessage());
            System.exit(1);
        } catch (InterruptedException e) {
            // Ignore.
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    } else {
        // Try to open the payment request as a file.
        FileInputStream stream = null;
        try {
            File paymentRequestFile = new File(location);
            stream = new FileInputStream(paymentRequestFile);
        } catch (Exception e) {
            System.err.println("Failed to open file: " + e.getMessage());
            System.exit(1);
        }
        try {
            paymentRequest = org.betacoin.protocols.payments.Protos.PaymentRequest.newBuilder()
                    .mergeFrom(stream).build();
        } catch (IOException e) {
            System.err.println("Failed to parse payment request from file " + e.getMessage());
            System.exit(1);
        }
        PaymentSession session = null;
        try {
            session = new PaymentSession(paymentRequest, verifyPki);
        } catch (PaymentRequestException e) {
            System.err.println("Error creating payment session " + e.getMessage());
            System.exit(1);
        }
        send(session);
    }
}