Example usage for java.util.concurrent ForkJoinTask get

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

Introduction

In this page you can find the example usage for java.util.concurrent ForkJoinTask get.

Prototype

public final V get(long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException 

Source Link

Document

Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.

Usage

From source file:edu.usu.sdl.openstorefront.report.ExternalLinkValidationReport.java

private void checkLinks() {
    int timeOutTime = MAX_CONNECTION_TIME_MILLIS;
    if (report.getReportOption() != null) {
        if (report.getReportOption().getMaxWaitSeconds() != null) {
            timeOutTime = report.getReportOption().getMaxWaitSeconds() * 1000;
        }//from www .  j  ava2 s .c  o m
    }

    ForkJoinPool forkJoinPool = new ForkJoinPool(MAX_CHECKPOOL_SIZE);

    Map<String, LinkCheckModel> linkMap = new HashMap();
    List<ForkJoinTask<LinkCheckModel>> tasks = new ArrayList<>();
    for (LinkCheckModel link : links) {
        linkMap.put(link.getId(), link);
        tasks.add(forkJoinPool.submit(new CheckLinkTask(link, timeOutTime)));
    }

    int completedCount = 0;
    for (ForkJoinTask<LinkCheckModel> task : tasks) {
        try {
            LinkCheckModel processed;
            try {
                processed = task.get(timeOutTime, TimeUnit.MILLISECONDS);
                if (processed != null) {
                    LinkCheckModel reportModel = linkMap.get(processed.getId());
                    reportModel.setStatus(processed.getStatus());
                    reportModel.setCheckResults(processed.getCheckResults());
                    reportModel.setHttpStatus(processed.getHttpStatus());
                } else {
                    //This shouldn't occur, however if it does at least show a message.
                    log.log(Level.WARNING, MessageFormat.format(
                            "A link check task failed to return results.  Status at Completed Abnormally? {0}",
                            task.isCompletedAbnormally()));
                }
            } catch (TimeoutException e) {
                task.cancel(true);
            }

            completedCount++;
        } catch (InterruptedException | ExecutionException ex) {
            log.log(Level.WARNING, "Check task  was interrupted.  Report results may be not complete.", ex);
        }
        log.log(Level.FINE, MessageFormat.format("Complete Checking Link Count: {0} out of {1}",
                new Object[] { completedCount, links.size() }));
    }

    for (LinkCheckModel checkModel : links) {
        if (StringUtils.isBlank(checkModel.getStatus())) {
            checkModel.setStatus("Unable to verify.  Timed out while waiting.");
        }
    }

    forkJoinPool.shutdownNow();
    try {
        forkJoinPool.awaitTermination(1000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ex) {
        log.log(Level.WARNING,
                "Check task shutdown was interrupted.  The application will recover and continue.", ex);
    }
}

From source file:org.codice.ddf.catalog.ui.query.monitor.impl.WorkspaceQueryService.java

private Pair<WorkspaceMetacardImpl, Long> getTaskResult(
        ForkJoinTask<Pair<WorkspaceMetacardImpl, Long>> workspaceTask, long timeout, TimeUnit timeoutUnit) {
    try {//from w w  w .  j  a  v  a 2  s . co  m
        return workspaceTask.get(timeout, timeoutUnit);
    } catch (TimeoutException e) {
        LOGGER.warn("Timeout", e);
    } catch (ExecutionException | InterruptedException e) {
        LOGGER.warn("ForkJoinPool error", e);
    }
    return null;
}

From source file:org.codice.ddf.catalog.ui.query.monitor.impl.WorkspaceQueryServiceImpl.java

private Pair<WorkspaceMetacardImpl, Long> getTaskResult(
        ForkJoinTask<Pair<WorkspaceMetacardImpl, Long>> workspaceTask, long timeout, TimeUnit timeoutUnit) {
    try {/*  w w w .  j  a  v a  2s.co  m*/
        return workspaceTask.get(timeout, timeoutUnit);
    } catch (TimeoutException e) {
        LOGGER.warn("Timeout", e);
    } catch (ExecutionException | InterruptedException e) {
        LOGGER.warn("ForkJoinPool error", e);

        Thread.currentThread().interrupt();
    }
    return null;
}