View Javadoc

1   package com.atlassian.sal.core.executor;
2   
3   import com.atlassian.sal.api.executor.ThreadLocalContextManager;
4   
5   import java.util.concurrent.Callable;
6   import java.util.concurrent.ScheduledExecutorService;
7   import java.util.concurrent.ScheduledFuture;
8   import java.util.concurrent.TimeUnit;
9   
10  import static com.google.common.base.Preconditions.*;
11  
12  /**
13   * Scheduled executor service that wraps executing callables and runnables in a wrapper that transfers the thread local
14   * state of the caller to the thread of the executing task.
15   *
16   * @since 2.0
17   */
18  public class ThreadLocalDelegateScheduledExecutorService<C> extends ThreadLocalDelegateExecutorService<C> implements ScheduledExecutorService
19  {
20      private final ScheduledExecutorService delegate;
21  
22      public ThreadLocalDelegateScheduledExecutorService(ThreadLocalContextManager<C> manager, ScheduledExecutorService delegate)
23      {
24          super(checkNotNull(manager), checkNotNull(delegate));
25          this.delegate = delegate;
26      }
27  
28      public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
29      {
30          return delegate.schedule(new ThreadLocalDelegateRunnable<C>(manager, command), delay, unit);
31      }
32  
33      public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
34      {
35          return delegate.schedule(new ThreadLocalDelegateCallable<C, V>(manager, callable), delay, unit);
36      }
37  
38      public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
39      {
40          return delegate.scheduleAtFixedRate(new ThreadLocalDelegateRunnable<C>(manager, command), initialDelay, period, unit);
41      }
42  
43      public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
44      {
45          return delegate.scheduleWithFixedDelay(new ThreadLocalDelegateRunnable<C>(manager, command), initialDelay, delay, unit);
46      }
47  }