Example usage for java.util.concurrent FutureTask isDone

List of usage examples for java.util.concurrent FutureTask isDone

Introduction

In this page you can find the example usage for java.util.concurrent FutureTask isDone.

Prototype

public boolean isDone() 

Source Link

Usage

From source file:com.taobao.tair.comm.TairClientFactory.java

public void close() {
    for (FutureTask<TairClient> task : clients.values()) {
        if (task.isDone() || !task.cancel(true)) {
            TairClient client = null;//from  w w w  .ja  va2 s .c  o m
            try {
                client = task.get();
            } catch (InterruptedException e) {
                LOGGER.warn(e);
            } catch (ExecutionException e) {
                LOGGER.warn(e);
            } catch (CancellationException e) {
            }
            client.close();
        }
    }
    clients.clear();
}

From source file:de.uni_rostock.goodod.owl.OntologyCache.java

public synchronized void flushCache() {
    for (Entry<URI, FutureTask<OWLOntology>> f : futures.entrySet()) {
        FutureTask<OWLOntology> future = f.getValue();
        if (false == future.isDone()) {
            future.cancel(true);/*w  w  w  . j av a 2  s  . c o m*/
        }
    }
    futures.clear();
}

From source file:ubic.gemma.core.loader.util.fetcher.HttpFetcher.java

protected Collection<LocalFile> doTask(FutureTask<Boolean> future, String seekFile, String outputFileName) {
    Executors.newSingleThreadExecutor().execute(future);
    try {/*  ww  w  .  j  av a  2s.c o m*/

        while (!future.isDone()) {
            try {
                Thread.sleep(AbstractFetcher.INFO_UPDATE_INTERVAL);
            } catch (InterruptedException ignored) {

            }
            AbstractFetcher.log.info((new File(outputFileName).length() + " bytes read"));
        }
        if (future.get()) {
            return this.listFiles(seekFile, outputFileName);
        }
    } catch (ExecutionException | IOException e) {
        throw new RuntimeException("Couldn't fetch file for " + seekFile, e);
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted: Couldn't fetch file for " + seekFile, e);
    }
    throw new RuntimeException("Couldn't fetch file for " + seekFile);
}

From source file:de.uni_rostock.goodod.owl.OntologyCache.java

public synchronized void removeOntologyAtURI(URI u) {
    FutureTask<OWLOntology> future = futures.get(u);
    if (null == future) {
        return;//from w w  w. ja  v a2 s .  c o  m
    } else if (false == future.isDone()) {
        future.cancel(true);
    }
    futures.remove(u);
}

From source file:org.apache.hadoop.corona.TestMiniCoronaRunJob.java

private void checkTaskNotDone(FutureTask<Boolean> task, int seconds) throws Exception {
    for (int i = 0; i < seconds; ++i) {
        if (task.isDone()) {
            // Job should not finish because of the memory limit
            Assert.fail();/*w w w.  j  a va 2  s  .c  o m*/
        }
        Thread.sleep(1L);
    }
}

From source file:dk.dbc.opensearch.datadock.DatadockPool.java

/**
 * Checks the jobs submitted for execution
 * //from ww w  . ja  va2 s  .  com
 * if a Job throws an exception it is written to the log and the
 * datadock continues.
 *
 * @throws InterruptedException if the job.get() call is interrupted (by kill or otherwise).
 */
public void checkJobs() throws InterruptedException {
    log.debug("DatadockPool method checkJobs called");

    log.debug(String.format("job size = %s", jobs.size()));

    Set<IIdentifier> finishedJobs = new HashSet<IIdentifier>();
    for (IIdentifier id : jobs.keySet()) {
        FutureTask<Boolean> job = jobs.get(id);
        log.debug(String.format("job is done: %s", job.isDone()));
        if (job.isDone()) {
            Boolean success = Boolean.FALSE;

            try {
                log.debug("DatadockPool checking job");
                success = job.get();
            } catch (ExecutionException ee) {
                // getting exception from thread
                Throwable cause = ee.getCause();
                log.error(String.format("Exception caught for identifier: %s, from thread: '%s'", id, cause),
                        cause);

                log.info(String.format("Setting status to FAILURE for identifier: %s with message: '%s'", id,
                        cause.getMessage()));
                try {
                    String msg = cause.getMessage() == null ? cause.toString() : cause.getMessage(); // avoid giving null to setStatusFailure
                    harvester.setStatusFailure(id, msg);
                } catch (HarvesterUnknownIdentifierException ex) {
                    String error = String.format(
                            "Failed to set failure status for identifier: %s . Message: %s", id,
                            ex.getMessage());
                    log.error(error, ex);
                } catch (HarvesterInvalidStatusChangeException ex) {
                    String error = String.format(
                            "Failed to set failure status for identifier: %s . Message: %s", id,
                            ex.getMessage());
                    log.error(error, ex);
                } catch (HarvesterIOException ex) {
                    String error = String.format(
                            "Failed to set failure status for identifier: %s . Message: %s", id,
                            ex.getMessage());
                    log.error(error, ex);
                }
            }

            log.debug("DatadockPool adding to finished jobs");
            finishedJobs.add(id);
        }
    }

    for (IIdentifier finishedJobId : finishedJobs) {
        log.debug(String.format("Removing Job with id: %s. Remaining jobs: %s", finishedJobId, jobs.size()));
        jobs.remove(finishedJobId);
    }
}

From source file:ubic.gemma.loader.util.fetcher.HttpFetcher.java

/**
 * @param future/*  w  w w  .  j  av a 2  s .c  o  m*/
 * @param seekFile
 * @param outputFileName
 * @return
 */
protected Collection<LocalFile> doTask(FutureTask<Boolean> future, String seekFile, String outputFileName) {
    Executors.newSingleThreadExecutor().execute(future);
    try {

        while (!future.isDone()) {
            try {
                Thread.sleep(INFO_UPDATE_INTERVAL);
            } catch (InterruptedException ie) {

            }
            log.info((new File(outputFileName).length() + " bytes read"));
        }
        if (future.get().booleanValue()) {
            return listFiles(seekFile, outputFileName);
        }
    } catch (ExecutionException e) {
        throw new RuntimeException("Couldn't fetch file for " + seekFile, e);
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted: Couldn't fetch file for " + seekFile, e);
    } catch (IOException e) {
        throw new RuntimeException("Couldn't fetch file for " + seekFile, e);
    }
    throw new RuntimeException("Couldn't fetch file for " + seekFile);
}

From source file:ubic.gemma.loader.util.fetcher.AbstractFetcher.java

/**
 * @param future//from   www.  ja  v a2  s .c  om
 * @return true if it finished normally, false if it was cancelled.
 */
protected boolean waitForDownload(FutureTask<Boolean> future) {
    StopWatch timer = new StopWatch();
    timer.start();
    long lastTime = timer.getTime();
    while (!future.isDone() && !future.isCancelled()) {
        try {
            Thread.sleep(INFO_UPDATE_INTERVAL);
        } catch (InterruptedException ie) {
            log.info("Cancelling download");
            boolean cancelled = future.cancel(true);
            if (cancelled) {
                log.info("Download stopped successfully.");
                return false;
            }
            throw new RuntimeException("Cancellation failed.");

        }

        if (log.isInfoEnabled() && timer.getTime() > (lastTime + 2000L)) {
            log.info("Waiting ... " + timer.getTime() + "ms elapsed....");
        }
    }
    return true;
}

From source file:ubic.gemma.core.loader.util.fetcher.AbstractFetcher.java

/**
 * @param future future task/*from  w ww .  ja v a  2 s . c  om*/
 * @return true if it finished normally, false if it was cancelled.
 */
protected boolean waitForDownload(FutureTask<Boolean> future) {
    StopWatch timer = new StopWatch();
    timer.start();
    long lastTime = timer.getTime();
    while (!future.isDone() && !future.isCancelled()) {
        try {
            Thread.sleep(AbstractFetcher.INFO_UPDATE_INTERVAL);
        } catch (InterruptedException ie) {
            AbstractFetcher.log.info("Cancelling download");
            boolean cancelled = future.cancel(true);
            if (cancelled) {
                AbstractFetcher.log.info("Download stopped successfully.");
                return false;
            }
            throw new RuntimeException("Cancellation failed.");

        }

        if (AbstractFetcher.log.isInfoEnabled() && timer.getTime() > (lastTime + 2000L)) {
            AbstractFetcher.log.info("Waiting ... " + timer.getTime() + "ms elapsed....");
        }
    }
    return true;
}

From source file:ubic.gemma.loader.util.fetcher.AbstractFetcher.java

/**
 * @param future//from www .j  a v  a2 s  .  com
 * @param expectedSize
 * @param outputFileName
 * @return true if it finished normally, false if it was cancelled.
 */
protected boolean waitForDownload(FutureTask<Boolean> future, long expectedSize, File outputFile) {
    int iters = 0;
    long previousSize = 0;
    StopWatch idleTimer = new StopWatch();
    while (!future.isDone() && !future.isCancelled()) {
        try {
            Thread.sleep(INFO_UPDATE_INTERVAL);
        } catch (InterruptedException ie) {
            log.info("Cancelling download");
            boolean cancelled = future.cancel(true);
            if (cancelled) {
                return false;
            }

            // double check...
            if (future.isCancelled() || future.isDone()) {
                return false;
            }

            log.error("Cancellation of actual download might not have happend? Task says it was not cancelled: "
                    + future);

            return false;

        }

        /*
         * Avoid logging too much. If we're waiting for a long download, reduce frequency of updates.
         */
        if (outputFile.length() < expectedSize
                && (iters < NUMBER_OF_TIMES_TO_LOG_WAITING_BEFORE_REDUCING_VERBOSITY
                        || iters % NUMBER_OF_TIMES_TO_LOG_WAITING_BEFORE_REDUCING_VERBOSITY == 0)) {

            double percent = 100.00 * outputFile.length() / expectedSize;

            // can cause npe error, breaking hot deploy
            if (log != null && log.isInfoEnabled()) {
                log.info((outputFile.length() + (expectedSize > 0 ? "/" + expectedSize : "") + " bytes read ("
                        + String.format("%.1f", percent) + "%)"));
            }

            if (previousSize == outputFile.length()) {
                /*
                 * Possibly consider bailing after a while.
                 */
                if (idleTimer.getTime() > STALLED_BAIL_TIME_LIMIT) {
                    log.warn("Download does not seem to be happening, bailing");
                    return false;
                }
                if (idleTimer.getTime() == 0)
                    idleTimer.start();
            } else {
                idleTimer.reset();
                idleTimer.start();
            }
        }

        if (outputFile.length() >= expectedSize) {
            // no special action, it will finish soon enough.
        }

        previousSize = outputFile.length();

        iters++;
    }
    if (iters == 0)
        log.info("File with size " + outputFile.length() + " bytes.");
    return true;
}