/** * Attempts to cancel the specified {@link ScheduledTask}. * * @param task the {@link ScheduledTask} to cancel * @return true if the {@link ScheduledTask} was canceled; otherwise, false */ public boolean cancel(T task) { CancellableTask<K, T> cancellableTask = this.cancellableTaskMap.getIfPresent(task.getKey()); if (cancellableTask != null && cancellableTask.getScheduledTask() == task && cancellableTask.cancel()) { this.cancellableTaskMap.invalidate(task.getKey()); return true; } return false; }
@Override public T apply(CancellableTask<K, T> cancellableTask) { return cancellableTask.getScheduledTask(); } });
/** * Gets the {@link ScheduledTask} with the specified key. * * @return the {@link ScheduledTask} */ public final Optional<T> getScheduledTask(K key) { CancellableTask<K, T> cancellableTask = this.cancellableTaskMap.getIfPresent(key); if (cancellableTask != null) { return Optional.of(cancellableTask.getScheduledTask()); } return Optional.absent(); }
/** * Attempts to cancel the specified {@link ScheduledTask}. * * @param task the {@link ScheduledTask} to cancel * @return true if the {@link ScheduledTask} was canceled; otherwise, false */ public boolean cancel(T task) { CancellableTask<K, T> cancellableTask = this.cancellableTaskMap.getIfPresent(task.getKey()); if (cancellableTask != null && cancellableTask.getScheduledTask() == task && cancellableTask.cancel()) { this.cancellableTaskMap.invalidate(task.getKey()); return true; } return false; }
/** * Schedules a subclass of {@link ScheduledTask} to run periodically. * * @param task the subclass of {@link ScheduledTask} to run every period * @param period the period between successive executions of the task * @param unit the time unit of the initialDelay and period parameters */ public final void schedule(final T task, final long period, final TimeUnit unit) throws IOException { Preconditions.checkArgument(this.isStarted, "TaskScheduler is not started"); try { CancellableTask<K, T> cancellableTask = this.cancellableTaskMap.get(task.getKey(), new Callable<CancellableTask<K, T>>() { @Override public CancellableTask<K, T> call() { return scheduleImpl(task, period, unit); } }); if (cancellableTask.getScheduledTask() != task) { throw new IOException("Failed to schedule task with duplicate key"); } } catch (ExecutionException e) { throw new IOException("Failed to schedule task", e); } }
@Override public T apply(CancellableTask<K, T> cancellableTask) { return cancellableTask.getScheduledTask(); } });
/** * Gets the {@link ScheduledTask} with the specified key. * * @return the {@link ScheduledTask} */ public final Optional<T> getScheduledTask(K key) { CancellableTask<K, T> cancellableTask = this.cancellableTaskMap.getIfPresent(key); if (cancellableTask != null) { return Optional.of(cancellableTask.getScheduledTask()); } return Optional.absent(); }
/** * Schedules a subclass of {@link ScheduledTask} to run periodically. * * @param task the subclass of {@link ScheduledTask} to run every period * @param period the period between successive executions of the task * @param unit the time unit of the initialDelay and period parameters */ public final void schedule(final T task, final long period, final TimeUnit unit) throws IOException { Preconditions.checkArgument(this.isStarted, "TaskScheduler is not started"); try { CancellableTask<K, T> cancellableTask = this.cancellableTaskMap.get(task.getKey(), new Callable<CancellableTask<K, T>>() { @Override public CancellableTask<K, T> call() { return scheduleImpl(task, period, unit); } }); if (cancellableTask.getScheduledTask() != task) { throw new IOException("Failed to schedule task with duplicate key"); } } catch (ExecutionException e) { throw new IOException("Failed to schedule task", e); } }