View Javadoc

1   package com.atlassian.event.internal;
2   
3   import com.atlassian.event.config.EventThreadPoolConfiguration;
4   import com.atlassian.event.spi.EventExecutorFactory;
5   
6   import java.util.concurrent.BlockingQueue;
7   import java.util.concurrent.Executor;
8   import java.util.concurrent.ThreadPoolExecutor;
9   
10  import static com.google.common.base.Preconditions.checkNotNull;
11  
12  /**
13   * A {@link EventExecutorFactory} that allows the {@link Executor} to be produced with a custom {@link BlockingQueue}
14   *
15   * @since 2.1
16   */
17  public abstract class AbstractEventExecutorFactory implements EventExecutorFactory {
18      private final EventThreadPoolConfiguration configuration;
19      private final EventThreadFactory eventThreadFactory;
20  
21      /**
22       * @param configuration
23       * @param eventThreadFactory
24       * @since 2.0.5
25       */
26      public AbstractEventExecutorFactory(EventThreadPoolConfiguration configuration, EventThreadFactory eventThreadFactory) {
27          this.configuration = checkNotNull(configuration);
28          this.eventThreadFactory = checkNotNull(eventThreadFactory);
29      }
30  
31      public AbstractEventExecutorFactory(EventThreadPoolConfiguration configuration) {
32          this(configuration, new EventThreadFactory());
33      }
34  
35      /**
36       * @return a new {@link BlockingQueue BlockingQueue<Runnable>} for the construction of a new {@link Executor}
37       */
38      protected abstract BlockingQueue<Runnable> getQueue();
39  
40      public Executor getExecutor() {
41          return new ThreadPoolExecutor(
42                  configuration.getCorePoolSize(),
43                  configuration.getMaximumPoolSize(),
44                  configuration.getKeepAliveTime(),
45                  configuration.getTimeUnit(),
46                  getQueue(),
47                  eventThreadFactory);
48      }
49  }