Class ExecutorServiceFactory


  • public class ExecutorServiceFactory
    extends Object
    Responsible for generating ExecutorService instances which are automatically wired up to shutdown when the ShutdownListener event triggers. This factory simplifies the creation of ExecutorServices which could overlook the important step of registering with the ShutdownManager. Failure to do so will prevent the server from shutting down. Note: Executors created using this factory will be triggered with the ExecutorService#shutdownNow method. This will interrupt all blocking calls made by those threads. This may be important for some users.
    Since:
    1.3.5
    • Constructor Detail

      • ExecutorServiceFactory

        public ExecutorServiceFactory​(ShutdownManager shutdownManager)
        Create an instance of the factory.
        Parameters:
        shutdownManager - Required to ensure each ExecutorService will be shutdown.
    • Method Detail

      • createScheduledService

        public ScheduledExecutorService createScheduledService​(int poolSize,
                                                               String threadNamePrefix)
        Generates a ScheduledExecutorService which has been pre-registered with the ShutdownManager.
        Parameters:
        poolSize - The size of the ScheduledExecutorService thread pool.
        threadNamePrefix - The thread name prefix to use when generating new threads.
        Returns:
        A non null ScheduledExecutorService
        See Also:
        Executors.newScheduledThreadPool(int)
      • createCancellableScheduledService

        public ScheduledExecutorService createCancellableScheduledService​(int poolSize,
                                                                          String threadNamePrefix)
        Generates a ScheduledExecutorService which has been pre-registered with the ShutdownManager and has the remove on cancel policy enabled.
        Parameters:
        poolSize - The size of the ScheduledExecutorService thread pool.
        threadNamePrefix - The thread name prefix to use when generating new threads.
        Returns:
        A non null ScheduledExecutorService
        See Also:
        Executors.newScheduledThreadPool(int)
      • createFixedThreadPool

        public ExecutorService createFixedThreadPool​(int pool,
                                                     ThreadFactory factory)
        Creates a fixed size Thread Pool ExecutorService which has been pre-registered with the ShutdownManager.
        Parameters:
        pool - The size of the pool to create.
        factory - The ThreadFactory used to generate new threads.
        Returns:
        Non null.
      • createFixedThreadPool

        public ExecutorService createFixedThreadPool​(int pool,
                                                     String threadNamePrefix)
        Create a fixed size Thread Pool ExecutorService using the provided name as the prefix of the thread names.
        Parameters:
        pool - Size of the fixed pool.
        threadNamePrefix - The thread name prefix to use when generating new threads.
        Returns:
        Non null.
        See Also:
        createFixedThreadPool(int, java.util.concurrent.ThreadFactory)
      • createCachedThreadPool

        public ExecutorService createCachedThreadPool​(ThreadFactory factory)
        Generates a Cached Thread Pool ExecutorService which has been pre-registered with the ShutdownManager. The provided ThreadFactory is used by the service when creating Threads.
        Parameters:
        factory - The ThreadFactory that will be used when generating threads. May not be null.
        Returns:
        A non null ExecutorService.
        See Also:
        Executors.newCachedThreadPool(java.util.concurrent.ThreadFactory)
      • createThreadPool

        @Deprecated
        public ExecutorService createThreadPool​(int coreSize,
                                                int maxSize,
                                                long idleTimeout,
                                                TimeUnit timeoutTimeunit,
                                                BlockingQueue<Runnable> runnables)
        Deprecated.
        createThreadPool(int, int, long, TimeUnit, BlockingQueue, String) should be used so that threads have meaningful names.
        Generates a ThreadPoolExecutor with the provided values, and registers that executor as listening for shutdown messages.
        Parameters:
        coreSize - the number of threads to keep in the pool, even if they are idle
        maxSize - Max number of threads in the pool
        idleTimeout - When the number of threads is greater than core, maximum time that excess idle threads will wait before terminating
        timeoutTimeunit - The time unit for the idleTimeout argument
        runnables - Queue of threads to be run
        Returns:
        a configured ExecutorService, registered to listen to shutdown messages.
      • createThreadPool

        public ExecutorService createThreadPool​(int coreSize,
                                                int maxSize,
                                                long idleTimeout,
                                                TimeUnit timeoutTimeunit,
                                                BlockingQueue<Runnable> runnables,
                                                String threadNamePrefix)
        Generates a ThreadPoolExecutor with the provided values, and registers that executor as listening for shutdown messages.
        Parameters:
        coreSize - the number of threads to keep in the pool, even if they are idle
        maxSize - Max number of threads in the pool
        idleTimeout - When the number of threads is greater than core, maximum time that excess idle threads will wait before terminating
        timeoutTimeunit - The time unit for the idleTimeout argument
        runnables - Queue of threads to be run
        threadNamePrefix - The thread name prefix to use when generating new threads.
        Returns:
        a configured ExecutorService, registered to listen to shutdown messages.