@Test public void testCloseCancelsTasks() throws IOException, InterruptedException { Task task = new Task("foo"); try (TaskScheduler<String, Task> taskScheduler = getTaskScheduler("TaskSchedulerTest")) { taskScheduler.schedule(task, 1, TimeUnit.SECONDS); task.getAutoResetEvent().waitOne(2, TimeUnit.SECONDS); } task.getAutoResetEvent().waitOne(2, TimeUnit.SECONDS); Assert.assertEquals(task.getCount(), 1); }
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); } }
TaskScheduler implements Closeable { 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); } } }
TaskScheduler implements Closeable { 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); } } protected  TaskScheduler(); }
TaskScheduler implements Closeable { 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); } } protected  TaskScheduler(); final void schedule(final T task, final long period, final TimeUnit unit); final Iterable<T> getScheduledTasks(); final Optional<T> getScheduledTask(K key); boolean cancel(T task); @Override @Synchronized final void close(); }
TaskScheduler implements Closeable { 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); } } protected  TaskScheduler(); final void schedule(final T task, final long period, final TimeUnit unit); final Iterable<T> getScheduledTasks(); final Optional<T> getScheduledTask(K key); boolean cancel(T task); @Override @Synchronized final void close();  }