Example usage for java.util.concurrent ForkJoinTask cancel

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

Introduction

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

Prototype

public boolean cancel(boolean mayInterruptIfRunning) 

Source Link

Document

Attempts to cancel execution of this task.

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   ww  w  .  j  av a 2s.  co  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);
    }
}