}
}
private ScheduledFuture<?> getSchedule(final Callable<T> callable, final Queue<T> queue, final AtomicReference<Throwable> throwable) {
TaskScheduler scheduler = taskScheduler;
if (scheduler == null) {
ConcurrentTaskScheduler concurrentTaskScheduler = new ConcurrentTaskScheduler();
concurrentTaskScheduler.setErrorHandler(new PropagatingErrorHandler());
scheduler = concurrentTaskScheduler;
}
Runnable task = new Runnable() {
public void run() {
if (!queue.isEmpty() || throwable.get() != null) {
return;
}
T result;
try {
result = callable.call();
}
catch (RuntimeException e) {
throwable.set(e);
throw e;
}
catch (Exception e) {
throwable.set(e);
throw new IllegalStateException("Could not obtain result", e);
}
if (result != null) {
queue.add(result);
}
}
};
ScheduledFuture<?> schedule = scheduler.schedule(task, trigger);
return schedule;
}