Example usage for java.util.concurrent Future get

List of usage examples for java.util.concurrent Future get

Introduction

In this page you can find the example usage for java.util.concurrent Future 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.vmware.bdd.utils.CommonUtil.java

public static Object waitForThreadResult(Future<?> result, int waitTime) {
    for (int i = 0; i < waitTime; i++) {
        try {/*from  ww  w  .j a  v a2 s .c o  m*/
            if (result.isDone()) {
                return result.get();
            }

            // sleep 1 second here
            Thread.sleep(1000);
        } catch (Exception e) {
            logger.error("Unexpected error occurred with threading.");
        }
    }

    // time out now
    return null;
}

From source file:com.mirth.connect.server.util.javascript.JavaScriptUtil.java

public static <T> T execute(JavaScriptTask<T> task) throws JavaScriptExecutorException, InterruptedException {
    Future<T> future = executor.submit(task);

    try {/*  w w  w  . j  a v  a  2 s .  c o  m*/
        return future.get();
    } catch (ExecutionException e) {
        throw new JavaScriptExecutorException(e.getCause());
    } catch (InterruptedException e) {
        // synchronize with JavaScriptTask.executeScript() so that it will not initialize the context while we are halting the task
        synchronized (task) {
            future.cancel(true);
            Context context = task.getContext();

            if (context != null && context instanceof MirthContext) {
                ((MirthContext) context).setRunning(false);
            }
        }

        // TODO wait for the task thread to complete before exiting?
        Thread.currentThread().interrupt();
        throw e;
    }
}

From source file:com.bigdata.dastor.service.StorageProxy.java

private static List<Row> weakRead(List<ReadCommand> commands)
        throws IOException, UnavailableException, TimeoutException {
    List<Row> rows = new ArrayList<Row>();

    // send off all the commands asynchronously
    List<Future<Object>> localFutures = null;
    List<IAsyncResult> remoteResults = null;
    for (ReadCommand command : commands) {
        InetAddress endPoint = StorageService.instance.findSuitableEndPoint(command.table, command.key);
        if (endPoint.equals(FBUtilities.getLocalAddress())) {
            if (logger.isDebugEnabled())
                logger.debug("weakread reading " + command + " locally");

            if (localFutures == null)
                localFutures = new ArrayList<Future<Object>>();
            Callable<Object> callable = new weakReadLocalCallable(command);
            localFutures.add(StageManager.getStage(StageManager.READ_STAGE).submit(callable));
        } else {//from  w w  w  .java  2s  .  com
            if (remoteResults == null)
                remoteResults = new ArrayList<IAsyncResult>();
            Message message = command.makeReadMessage();
            if (logger.isDebugEnabled())
                logger.debug(
                        "weakread reading " + command + " from " + message.getMessageId() + "@" + endPoint);
            if (DatabaseDescriptor.getConsistencyCheck())
                message.setHeader(ReadCommand.DO_REPAIR, ReadCommand.DO_REPAIR.getBytes());
            remoteResults.add(MessagingService.instance.sendRR(message, endPoint));
        }
    }

    // wait for results
    if (localFutures != null) {
        for (Future<Object> future : localFutures) {
            Row row;
            try {
                row = (Row) future.get();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            rows.add(row);
        }
    }
    if (remoteResults != null) {
        for (IAsyncResult iar : remoteResults) {
            byte[] body;
            body = iar.get(DatabaseDescriptor.getRpcTimeout(), TimeUnit.MILLISECONDS);
            ByteArrayInputStream bufIn = new ByteArrayInputStream(body);
            ReadResponse response = ReadResponse.serializer().deserialize(new DataInputStream(bufIn));
            if (response.row() != null)
                rows.add(response.row());
        }
    }

    return rows;
}

From source file:com.hdfstoftp.main.HdfsToFtp.java

/**
 * ?/*from  w  ww . j av  a 2s.  com*/
 * 
 * @param srcFS
 *            
 * @param src
 *            ?
 * @param dst
 *            
 * @param queryStr
 *            
 * @param deleteSource
 *            ??
 * @param overwrite
 *            ????
 * @return boolean
 * @throws Exception
 */
private static boolean copyFromHDFSToFTP(Config config) throws Exception {
    // ?hdfs
    Configuration conf = new Configuration();
    FileSystem srcFS = FileSystem.get(conf);
    long start = System.currentTimeMillis();
    boolean isRename = config.isRenameUploaded();
    int retryTimes = config.getRetryTimes();
    // ?
    String dstPath = config.getDestDir();
    Path src = new Path(config.getSouceDir());
    FileStatus fileStatus = srcFS.getFileStatus(src);
    String subDir = null;
    if (fileStatus.isDirectory()) {// 
        if (isRename) {// ??rename
            subDir = Config.RENAME_DIR;
            srcFS.mkdirs(new Path(fileStatus.getPath(), subDir));
        }
        int threadNum = config.getThreadNum();
        // 
        ExecutorService threadPool = Executors.newFixedThreadPool(threadNum);
        // ?ftp
        FTPClientPool ftpPool = new FTPClientPool(threadNum, new FtpClientFactory(config.getFTPClientConfig()));
        FTPClient ftpClient = ftpPool.borrowObject();
        // ?
        ftpClient.makeDirectory(dstPath);
        ftpPool.returnObject(ftpClient);
        // ??
        FileStatus contents[] = srcFS.listStatus(src);
        long beginFilter = 0;
        long endFileter = 0;

        if (config.getCommandLine().hasOption("d") || config.getCommandLine().hasOption("h")
                || config.getCommandLine().hasOption("t")) {// ?"["
            beginFilter = System.currentTimeMillis();
            Long[] timeRange = parseTimeRange(config.getCommandLine());
            contents = getNewContents(timeRange, contents);
            endFileter = System.currentTimeMillis();
        }
        // ?
        if (config.getCommandLine().hasOption("r")) {// "["??
            beginFilter = System.currentTimeMillis();
            contents = getFilterContents(config.getCommandLine().getOptionValue("r").trim(), contents);
            endFileter = System.currentTimeMillis();
        }
        logger.info("total file count:" + contents.length);
        Map<String, String> fileNameMap = null;
        long beginSkip = 0;
        long endSkip = 0;
        boolean overwrite = true;
        if (config.getCommandLine().hasOption("o")) {
            overwrite = "true".equals(config.getCommandLine().getOptionValue("o").trim());
        }
        if (!overwrite) {// ?????
            beginSkip = System.currentTimeMillis();
            fileNameMap = getFileNameMap(dstPath, ftpPool);
            endSkip = System.currentTimeMillis();
        }
        int skiped = 0;

        List<Future<?>> futureList = new ArrayList<Future<?>>();
        for (int i = 0; i < contents.length; i++) {
            if (!overwrite && fileNameMap.containsKey(contents[i].getPath().getName())) {
                // 
                skiped++;
                Log.info("skiped filename:" + contents[i].getPath().getName());
                continue;
            }
            if (contents[i].isDirectory()) {
                continue;
            }
            // ???
            Future<?> future = threadPool.submit(new UploadFileTask(srcFS, contents[i].getPath(),
                    new Path(dstPath, contents[i].getPath().getName()), ftpPool, false, isRename, subDir,
                    retryTimes));
            futureList.add(future);
        }
        int transfered = 0;
        int failed = 0;
        for (Future<?> future : futureList) {
            Boolean computeResult = (Boolean) future.get();
            if (computeResult) {
                transfered++;
                if (transfered % 50 == 0 || transfered == contents.length) {
                    logger.info("have transfered:" + transfered + " files");
                }
            } else {
                failed++;
                logger.error("failed transter:" + failed + " files");
            }
        }
        // 
        threadPool.shutdown();
        // FTPCient
        ftpPool.close();
        // ****************
        logger.info("filter time:" + (endFileter - beginFilter) + " ms");
        if (!overwrite) {
            logger.info("skip time:" + (endSkip - beginSkip) + " ms");
        }
        logger.info("total file count:" + contents.length);
        logger.info("total transtered: " + transfered + ",total failed:" + failed + ",total skiped:" + skiped);

    } else {// 

        BufferedReader reader = null;
        FtpClientFactory facotry = new FtpClientFactory(config.getFTPClientConfig());
        FTPClient ftpClient = null;
        InputStream in = null;
        try {
            Path path = fileStatus.getPath();
            if (!path.getName().contains("log")) {

            }
            reader = new BufferedReader(new FileReader(new File(path.toUri().getPath())));
            String str = null;

            ftpClient = facotry.makeObject();

            while ((str = reader.readLine()) != null) {
                String[] feilds = str.split("&");
                Path filePath = null;
                if (feilds.length == 2 && feilds[1] != "") {
                    filePath = new Path(feilds[1]);
                    in = srcFS.open(filePath);
                    boolean result = ftpClient.storeFile(dstPath, in);
                    System.out.println(ftpClient.getReplyCode());
                    if (result) {
                        logger.info(filePath.toString());
                    } else {
                        logger_failed.info(filePath.toString());
                    }
                } else {
                    continue;
                }

            }
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            in.close();
            reader.close();
            facotry.destroyObject(ftpClient);
        }

    }
    long end = System.currentTimeMillis();
    logger.info("finished transfer,total time:" + (end - start) / 1000 + "s");
    return true;
}

From source file:com.glaf.core.util.http.HttpUtils.java

/**
 * ?http?//w  w w  .  j  a va  2s . co  m
 * 
 * @param requestUrl
 *            ?
 * @param requestMethod
 *            ?GET?POST
 * @param outputStr
 *            ???
 * @return
 */
public static byte[] download(String requestUrl, String requestMethod, String outputStr, boolean isSSL) {
    byte[] buff = null;
    AsyncHttpClient client = null;
    InputStream inputStream = null;
    try {
        if (isSSL) {
            client = HttpConnectionFactory.getHttpClient();
        } else {
            client = HttpConnectionFactory.getHttpsClient();
        }
        Future<Response> future = null;
        if ("GET".equalsIgnoreCase(requestMethod)) {
            future = client.prepareGet(requestUrl).execute();
        } else {
            if (StringUtils.isNotEmpty(outputStr)) {
                future = client.preparePost(requestUrl).setBody(outputStr.getBytes("UTF-8")).execute();
            } else {
                future = client.preparePost(requestUrl).execute();
            }
        }

        Response response = (Response) future.get();
        inputStream = response.getResponseBodyAsStream();
        buff = FileUtils.getBytes(inputStream);
        // ?
        IOUtils.closeQuietly(inputStream);
    } catch (Exception ex) {
        ex.printStackTrace();
        log.error("http request error:{}", ex);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
    return buff;
}

From source file:edu.umass.cs.gigapaxos.testing.TESTPaxosClient.java

protected static void sendTestRequests(int numReqs, TESTPaxosClient[] clients, double load)
        throws JSONException, IOException, InterruptedException, ExecutionException {
    System.out.print("\nTesting " + "[#requests=" + numReqs + ", request_size=" + gibberish.length()
            + "B, #clients=" + clients.length + ", #groups=" + NUM_GROUPS_CLIENT + ", #total_groups="
            + Config.getGlobalInt(TC.NUM_GROUPS) + ", group_size=" + Config.getGlobalInt(TC.GROUP_SIZE)
            + ", load=" + Util.df(load) + "/s" + "]"
            + (Config.getGlobalBoolean(TC.PROBE_CAPACITY) ? "" : "\n"));

    Future<?>[] futures = new Future<?>[Config.getGlobalInt(TC.NUM_CLIENTS)];
    // assert (executor.getCorePoolSize() == SEND_POOL_SIZE);
    if (!Config.getGlobalBoolean(TC.PROBE_CAPACITY))
        initResponseTracker(clients, numReqs);
    long initTime = System.currentTimeMillis();
    // if (TOTAL_LOAD > LOAD_THRESHOLD)
    {//from  w  w w  .j  ava2s .c om
        int SEND_POOL_SIZE = Config.getGlobalInt(TC.NUM_CLIENTS);
        if (SEND_POOL_SIZE > 0) {
            for (int i = 0; i < SEND_POOL_SIZE; i++) {
                final int j = i;
                assert (!executor.isShutdown());
                futures[j] = executor.submit(new Runnable() {
                    public void run() {
                        try {
                            sendTestRequests(
                                    // to account for integer division
                                    j < SEND_POOL_SIZE - 1 ? numReqs / SEND_POOL_SIZE
                                            : numReqs - numReqs / SEND_POOL_SIZE * (SEND_POOL_SIZE - 1),
                                    clients, false, load / SEND_POOL_SIZE);
                        } catch (JSONException | IOException | InterruptedException | ExecutionException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
            for (Future<?> future : futures)
                future.get();
        } else
            sendTestRequests(numReqs, clients, false, load);
    }
    // all done sending if here
    mostRecentSentRate = numReqs * 1000.0 / (System.currentTimeMillis() - initTime);
    System.out.println((Config.getGlobalBoolean(TC.PROBE_CAPACITY) ? "..." : "") + "done sending " + numReqs
            + " requests in " + Util.df((System.currentTimeMillis() - initTime) / 1000.0)
            + " secs; estimated average_sent_rate = " + Util.df(mostRecentSentRate) + "/s" + " \n "
            + reqCounts);

}

From source file:com.glaf.core.util.http.HttpUtils.java

/**
 * ?http?/*from w  w  w .j  ava  2s .  com*/
 * 
 * @param requestUrl
 *            ?
 * @param requestMethod
 *            ?GET?POST
 * @param outputStr
 *            ???
 * @return
 */
public static String process(String requestUrl, String requestMethod, String outputStr, boolean isSSL) {
    StringBuffer buffer = new StringBuffer();
    AsyncHttpClient client = null;
    InputStream inputStream = null;
    BufferedReader bufferedReader = null;
    InputStreamReader inputStreamReader = null;
    try {
        if (isSSL) {
            client = HttpConnectionFactory.getHttpClient();
        } else {
            client = HttpConnectionFactory.getHttpsClient();
        }

        Future<Response> future = null;
        if ("GET".equalsIgnoreCase(requestMethod)) {
            future = client.prepareGet(requestUrl).execute();
        } else {
            if (StringUtils.isNotEmpty(outputStr)) {
                future = client.preparePost(requestUrl).setBody(outputStr.getBytes("UTF-8")).execute();
            } else {
                future = client.preparePost(requestUrl).execute();
            }
        }

        Response response = (Response) future.get();
        inputStream = response.getResponseBodyAsStream();
        inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
        bufferedReader = new BufferedReader(inputStreamReader);
        String str = null;
        while ((str = bufferedReader.readLine()) != null) {
            buffer.append(str);
        }
        // ?
        IOUtils.closeQuietly(bufferedReader);
        IOUtils.closeQuietly(inputStreamReader);
        IOUtils.closeQuietly(inputStream);

    } catch (Exception ex) {
        ex.printStackTrace();
        log.error("http request error:{}", ex);
    } finally {
        IOUtils.closeQuietly(bufferedReader);
        IOUtils.closeQuietly(inputStreamReader);
        IOUtils.closeQuietly(inputStream);
    }
    return buffer.toString();
}

From source file:com.microsoft.azure.keyvault.extensions.test.KeyVaultExtensionsIntegrationTestBase.java

private static AuthenticationResult getAccessToken(String authorization, String resource) throws Exception {

    String clientId = System.getenv("arm.clientid");
    if (clientId == null) {
        throw new Exception("Please inform arm.clientid in the environment settings.");
    }/*from   www. java  2s .  c o  m*/

    String clientKey = System.getenv("arm.clientkey");
    String username = System.getenv("arm.username");
    String password = System.getenv("arm.password");

    AuthenticationResult result = null;
    ExecutorService service = null;
    try {
        service = Executors.newFixedThreadPool(1);
        AuthenticationContext context = new AuthenticationContext(authorization, false, service);

        Future<AuthenticationResult> future = null;

        if (clientKey != null && password == null) {
            ClientCredential credentials = new ClientCredential(clientId, clientKey);
            future = context.acquireToken(resource, credentials, null);
        }

        if (password != null && clientKey == null) {
            future = context.acquireToken(resource, clientId, username, password, null);
        }

        if (future == null) {
            throw new Exception(
                    "Missing or ambiguous credentials - please inform exactly one of arm.clientkey or arm.password in the environment settings.");
        }

        result = future.get();
    } finally {
        service.shutdown();
    }

    if (result == null) {
        throw new RuntimeException("authentication result was null");
    }
    return result;
}

From source file:main.ScorePipeline.java

/**
 *
 * This method calculates similarities for MSRobin for each spectra on
 * yeast_human_spectra on the first data set against all yeast spectra on
 * the second data set/*from   ww w . j  av a 2  s  .com*/
 *
 * thydMSnSpectra-yeast-human, tsolMSnSpectra-yeast,
 *
 * @param tsolMSnSpectra
 * @param thydMSnSpectra
 * @param bw
 * @param fragTol
 * @param precTol
 * @throws IllegalArgumentException
 * @throws ClassNotFoundException
 * @throws IOException
 * @throws MzMLUnmarshallerException
 * @throws NumberFormatException
 * @throws InterruptedException
 */
private static void calculate_MSRobins(ArrayList<MSnSpectrum> tsolMSnSpectra,
        ArrayList<MSnSpectrum> thydMSnSpectra, BufferedWriter bw, double fragTol, double precTol,
        int calculationOptionIntensityMSRobin, int msRobinCalculationOption)
        throws IllegalArgumentException, ClassNotFoundException, IOException, MzMLUnmarshallerException,
        NumberFormatException, InterruptedException {
    ExecutorService excService = Executors
            .newFixedThreadPool(ConfigHolder.getInstance().getInt("thread.numbers"));
    List<Future<SimilarityResult>> futureList = new ArrayList<>();
    for (MSnSpectrum thydMSnSpectrum : thydMSnSpectra) {
        Calculate_Similarity similarity = new Calculate_Similarity(thydMSnSpectrum, tsolMSnSpectra, fragTol,
                precTol, calculationOptionIntensityMSRobin, msRobinCalculationOption);
        Future future = excService.submit(similarity);
        futureList.add(future);
    }
    for (Future<SimilarityResult> future : futureList) {
        try {
            SimilarityResult get = future.get();
            String tmp_charge = get.getSpectrumChargeAsString(), spectrum = get.getSpectrumName();
            double tmp_precursor_mz = get.getSpectrumPrecursorMZ(),
                    msrobin = get.getScores().get(SimilarityMethods.MSRobin);
            if (msrobin == Double.MIN_VALUE) {
                LOGGER.info("The similarity for the spectrum " + spectrum
                        + " is too small to keep the record, therefore score is not computed.");
                // Means that score has not been calculated!
                //                    bw.write(tmp_Name + "\t" + tmp_charge + "\t" + tmpPrecMZ + "\t");
                //                    bw.write("NA" + "\n");
            } else {
                bw.write(spectrum + "\t" + tmp_charge + "\t" + tmp_precursor_mz + "\t"
                        + get.getSpectrumToCompare() + "\t" + msrobin + "\n");
            }
        } catch (InterruptedException | ExecutionException e) {
            LOGGER.error(e);
        }
    }
    excService.shutdown();

}

From source file:org.eclipse.mylyn.commons.http.HttpUtil.java

/**
 * @since 3.6//w  w w  .j  av a  2s.c o m
 */
public static <T> T execute(IProgressMonitor monitor, WebRequest<T> request) throws Throwable {
    // check for legacy reasons
    SubMonitor subMonitor = (monitor instanceof SubMonitor) ? (SubMonitor) monitor : SubMonitor.convert(null);

    Future<T> future = CommonsHttpPlugin.getExecutorService().submit(request);
    while (true) {
        if (monitor.isCanceled()) {
            request.abort();

            // wait for executor to finish
            future.cancel(false);
            try {
                if (!future.isCancelled()) {
                    future.get();
                }
            } catch (CancellationException e) {
                // ignore
            } catch (InterruptedException e) {
                // ignore
            } catch (ExecutionException e) {
                // ignore
            }
            throw new OperationCanceledException();
        }

        try {
            return future.get(POLL_INTERVAL, TimeUnit.MILLISECONDS);
        } catch (ExecutionException e) {
            throw e.getCause();
        } catch (TimeoutException ignored) {
        }

        subMonitor.setWorkRemaining(20);
        subMonitor.worked(1);
    }
}