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.checkNotNull;
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      private final ScheduledExecutorService delegate;
20  
21      public ThreadLocalDelegateScheduledExecutorService(ThreadLocalContextManager<C> manager, ScheduledExecutorService delegate) {
22          super(checkNotNull(manager), checkNotNull(delegate));
23          this.delegate = delegate;
24      }
25  
26      public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
27          return delegate.schedule(new ThreadLocalDelegateRunnable<C>(manager, command), delay, unit);
28      }
29  
30      public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
31          return delegate.schedule(new ThreadLocalDelegateCallable<C, V>(manager, callable), delay, unit);
32      }
33  
34      public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
35          return delegate.scheduleAtFixedRate(new ThreadLocalDelegateRunnable<C>(manager, command), initialDelay, period, unit);
36      }
37  
38      public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
39          return delegate.scheduleWithFixedDelay(new ThreadLocalDelegateRunnable<C>(manager, command), initialDelay, delay, unit);
40      }
41  }