View Javadoc
1   package com.atlassian.activeobjects.spi;
2   
3   import com.atlassian.sal.api.executor.ThreadLocalDelegateExecutorFactory;
4   import com.atlassian.tenancy.api.Tenant;
5   import com.google.common.annotations.VisibleForTesting;
6   import com.google.common.util.concurrent.ThreadFactoryBuilder;
7   import org.slf4j.Logger;
8   import org.slf4j.LoggerFactory;
9   
10  import javax.annotation.Nonnull;
11  import java.util.concurrent.ExecutorService;
12  import java.util.concurrent.Executors;
13  import java.util.concurrent.ThreadFactory;
14  
15  public class DefaultInitExecutorServiceProvider implements InitExecutorServiceProvider {
16      private static final Logger logger = LoggerFactory.getLogger(DefaultInitExecutorServiceProvider.class);
17  
18      private final ThreadLocalDelegateExecutorFactory threadLocalDelegateExecutorFactory;
19  
20      @VisibleForTesting
21      final ThreadFactory aoContextThreadFactory;
22  
23      public DefaultInitExecutorServiceProvider(final ThreadLocalDelegateExecutorFactory threadLocalDelegateExecutorFactory) {
24          this.threadLocalDelegateExecutorFactory = threadLocalDelegateExecutorFactory;
25  
26          // store the CCL of the ao-plugin bundle for use by all shared thread pool executors
27          ClassLoader bundleContextClassLoader = Thread.currentThread().getContextClassLoader();
28          aoContextThreadFactory = new ContextClassLoaderThreadFactory(bundleContextClassLoader);
29      }
30  
31      /**
32       * Create a thread pool executor with the same context class loader that was used when creating this class.
33       *
34       * Pool size is <code>activeobjects.servicefactory.ddl.threadpoolsize</code>, default 1.
35       *
36       * Runs in the same thread local context as the calling code.
37       *
38       * @param tenant active-objects-init-&lt;tenant.toString()&gt;-%d
39       */
40      @Nonnull
41      @Override
42      public ExecutorService initExecutorService(@Nonnull Tenant tenant) {
43          logger.debug("creating default init executor");
44  
45          final ThreadFactory threadFactory = new ThreadFactoryBuilder()
46                  .setThreadFactory(aoContextThreadFactory)
47                  .setNameFormat("active-objects-init-" + tenant.toString() + "-%d")
48                  .build();
49  
50          final ExecutorService delegate = Executors.newFixedThreadPool(Integer.getInteger("activeobjects.servicefactory.ddl.threadpoolsize", 1), threadFactory);
51  
52          return threadLocalDelegateExecutorFactory.createExecutorService(delegate);
53      }
54  }