Class DefaultExecutorComponent

java.lang.Object
com.lucidworks.apollo.component.DefaultExecutorComponent
All Implemented Interfaces:
ExecutorComponent, Closeable, AutoCloseable

public class DefaultExecutorComponent extends Object implements Closeable, ExecutorComponent
A convenience class for creating executor services. It handles the boilerplate of custom thread names (great for stack traces!) and registering the executor services centrally so that they can be shut down appropriately. Provides a potential point for instrumentation, too, should we want to.

Every method in this class returns a ListeningExecutorService, so that you can listen for task completion if you would like.

If you're looking for scheduled thread pools, use ScheduledRunnableComponent instead. It handles more of that for you.

  • Constructor Details

    • DefaultExecutorComponent

      public DefaultExecutorComponent(com.codahale.metrics.MetricRegistry metricRegistry)
  • Method Details

    • getOrCreateSingleThreadExecutor

      public com.google.common.util.concurrent.ListeningExecutorService getOrCreateSingleThreadExecutor(String threadPoolName, String threadNameFormat)
      TODO: experimental. Not sure if we want this kind of interface. If you want to reuse a thread pool between multiple components, you can use this.
      Specified by:
      getOrCreateSingleThreadExecutor in interface ExecutorComponent
      Parameters:
      threadPoolName - the name of the thread pool to get. Multiple requests with the same thread pool name will return the same thread pool.
      threadNameFormat - a String.format(String, Object...)-compatible format String, to which a unique integer (0, 1, etc.) will be supplied as the single parameter. This integer will be unique to the built instance of the ThreadFactory and will be assigned sequentially. For example, "rpc-pool-%d" will generate thread names like "rpc-pool-0", "rpc-pool-1", "rpc-pool-2", etc.
      Returns:
    • getOrCreateFixedThreadPool

      public com.google.common.util.concurrent.ListeningExecutorService getOrCreateFixedThreadPool(int nThreads, String threadNameFormat)
      Specified by:
      getOrCreateFixedThreadPool in interface ExecutorComponent
    • newSingleThreadExecutor

      public com.google.common.util.concurrent.ListeningExecutorService newSingleThreadExecutor(String threadNameFormat)
      Specified by:
      newSingleThreadExecutor in interface ExecutorComponent
      Parameters:
      threadNameFormat - a String.format(String, Object...)-compatible format String, to which a unique integer (0, 1, etc.) will be supplied as the single parameter. This integer will be unique to the built instance of the ThreadFactory and will be assigned sequentially. For example, "rpc-pool-%d" will generate thread names like "rpc-pool-0", "rpc-pool-1", "rpc-pool-2", etc.
      Returns:
      the newly created thread pool
    • newFixedThreadPool

      public com.google.common.util.concurrent.ListeningExecutorService newFixedThreadPool(int nThreads, String threadNameFormat)
      Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue, using the provided ThreadFactory to create new threads when needed. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.
      Specified by:
      newFixedThreadPool in interface ExecutorComponent
      Parameters:
      nThreads - the number of threads in the pool
      threadNameFormat - a String.format(String, Object...)-compatible format String, to which a unique integer (0, 1, etc.) will be supplied as the single parameter. This integer will be unique to the built instance of the ThreadFactory and will be assigned sequentially. For example, "rpc-pool-%d" will generate thread names like "rpc-pool-0", "rpc-pool-1", "rpc-pool-2", etc.
      Returns:
      the newly created thread pool
    • newCachedThreadPool

      public com.google.common.util.concurrent.ListeningExecutorService newCachedThreadPool(String threadNameFormat)
      Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks. Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool. Threads that have not been used for sixty seconds are terminated and removed from the cache. Thus, a pool that remains idle for long enough will not consume any resources. Note that pools with similar properties but different details (for example, timeout parameters) may be created using ThreadPoolExecutor constructors. Note that unlike java.util.concurrent.Executors#newCachedThreadPool, this implementation does limit the pool size to 15,000 threads, since that is a practical limit on a Linux machine with ~16gb of RAM. That is based on the assumptions that: 1.) if you hit that limit, something is going wrong in your system, and 2.) it's at least marginally better to fail at the point of execution with a RejectedExecutionException versus an OutOfMemoryError with a "could not create new native thread" message. Both are probably terminal, but at least the former may be slightly more understandable?
      Specified by:
      newCachedThreadPool in interface ExecutorComponent
      Parameters:
      threadNameFormat - a String.format(String, Object...)-compatible format String, to which a unique integer (0, 1, etc.) will be supplied as the single parameter. This integer will be unique to the built instance of the ThreadFactory and will be assigned sequentially. For example, "rpc-pool-%d" will generate thread names like "rpc-pool-0", "rpc-pool-1", "rpc-pool-2", etc.
      Returns:
      the newly created thread pool
    • getOrCreateCachedThreadPool

      public com.google.common.util.concurrent.ListeningExecutorService getOrCreateCachedThreadPool(String threadNameFormat)
      Specified by:
      getOrCreateCachedThreadPool in interface ExecutorComponent
    • newBuilder

    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException