Example usage for java.util.concurrent ScheduledFuture get

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

Introduction

In this page you can find the example usage for java.util.concurrent ScheduledFuture 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.funambol.pushlistener.service.taskexecutor.ScheduledTaskExecutor.java

/**
 * If there is, logs the exception thrown by the taskWrapper execution
 * @param taskWrapper the executed task wrapper
 * @param scheduledFuture the scheduledFuture with the exception (if there is)
 *///  w  ww.  j a v a 2s.c  o m
private void logTaskWrapperExecutionError(TaskWrapper taskWrapper, ScheduledFuture scheduledFuture) {
    try {
        if (scheduledFuture.isDone()) {
            scheduledFuture.get();
        }
    } catch (InterruptedException ie) {
    } catch (ExecutionException ee) {
        //
        // This is done to retrieve the possible exception thrown by the
        // task
        //
        Throwable realThrowable = ee.getCause();

        if (taskWrapper != null) {
            log.error("Task '" + taskWrapper + "' throws an uncaught exception. ", realThrowable);
        } else {
            log.error("Uncaught exception thrown by: " + scheduledFuture, realThrowable);
        }
    }
}

From source file:com.laudandjolynn.mytv.crawler.tvmao.TvMaoCrawler.java

@Override
public List<ProgramTable> crawlProgramTable(String date, TvStation station) {
    if (station == null) {
        logger.debug("station is null while crawl program table.");
        return null;
    }// w w  w . ja v a2s .c  om

    Date dateObj = DateUtils.string2Date(date, "yyyy-MM-dd");
    if (dateObj == null) {
        logger.debug("date is null while crawl program table of " + station.getName());
        return null;
    }
    String queryDate = DateUtils.date2String(dateObj, "yyyy-MM-dd");
    final TvMaoCrawlTask task = new TvMaoCrawlTask();
    task.date = queryDate;
    task.tvStation = station;

    ScheduledFuture<List<ProgramTable>> future = SCHEDULED_EXECUTOR_SERVICE
            .schedule(new Callable<List<ProgramTable>>() {
                @Override
                public List<ProgramTable> call() throws Exception {
                    return crawlProgramTable(task);
                }
            }, getScheduleFrequency(), TimeUnit.MILLISECONDS);
    try {
        return future.get();
    } catch (InterruptedException e) {
        logger.error("crawl task interrupted while crawl program table of " + station + " at " + queryDate, e);
    } catch (ExecutionException e) {
        logger.error("crawl task executed fail while crawl program table of " + station + " at " + queryDate,
                e);
    }
    return null;
}

From source file:com.funambol.pushlistener.service.taskexecutor.ScheduledTaskExecutor.java

/**
 * Called when a ScheduledTask ends its execution. See afterExecute.
 *///from ww w. ja va 2s.  co  m
protected void afterScheduledTaskExecution(Runnable r, Throwable t) {
    super.afterExecute(r, t);

    ScheduledTaskWrapper scheduledTask = null;
    Lock handlingTaskLock = null;

    if (r instanceof ScheduledFuture) {
        ScheduledFuture scheduledFuture = (ScheduledFuture) r;
        synchronized (scheduledFutures) {
            scheduledTask = (ScheduledTaskWrapper) scheduledFutures.getKey(scheduledFuture);

            if (scheduledTask != null) {
                handlingTaskLock = getHandlingTaskLock(scheduledTask.getId());
                handlingTaskLock.lock();
            }
        }
        //
        // Bear in mind that here the scheduledTask could be null if the scheduledFuture
        // has been cancelled and removed from the scheduledFutures map.
        //
        if (log.isTraceEnabled()) {
            if (scheduledTask == null) {
                log.trace("Scheduled task null for: " + r + ". Is it cancelled ? "
                        + scheduledFuture.isCancelled());
            }
        }

        try {
            if (scheduledFuture.isDone()) {
                scheduledFuture.get();
            }
        } catch (InterruptedException ie) {
        } catch (ExecutionException ee) {
            //
            // This is done to retrieve the possible exception thrown by the
            // task
            //
            Throwable realThrowable = ee.getCause();

            if (scheduledTask != null) {

                log.error("Task '" + scheduledTask + "' throws an uncaught exception. "
                        + "The task will be rescheduled", realThrowable);
                try {
                    scheduledTask.shutdown();
                } catch (TaskException ex) {
                    log.error("Error shutting down scheduled task '" + scheduledTask + "'", ex);
                }

                scheduledTask.setState(ScheduledTaskWrapper.State.SHUTDOWN);

                synchronized (scheduledFutures) {
                    //
                    // Any time we remove the scheduledTask from scheduledFutures,
                    // we try to remove the scheduledFuture from the queue. This
                    // is not really needed because after a while this is performed
                    // automatically but in this way we keep scheduledFutures and
                    // the queue in sync
                    //
                    if (scheduledFuture instanceof Runnable) {
                        super.remove((Runnable) scheduledFuture);
                    }
                    scheduledFutures.remove(scheduledTask);
                }

                //
                // The task will be rescheduled using the period as delay
                // because otherwise a new execution is performed asap
                //
                scheduleTask(scheduledTask, scheduledTask.getPeriod());

            } else {
                log.error("Uncaught exception thrown by: " + scheduledFuture
                        + ". This ScheduledFuture seems not relative to a ScheduleTask"
                        + " so nothing will be rescheduled (it could be about "
                        + " to an already cancelled task)", realThrowable);
            }

        } catch (CancellationException ce) {
        } finally {
            if (handlingTaskLock != null) {
                handlingTaskLock.unlock();
            }
            handlingScheduledExecutionTimeInformation(scheduledTask);
            LogContext.clear();
        }
    }
}