org.apache.camel.util.concurrent
Class ExecutorServiceHelper

java.lang.Object
  extended by org.apache.camel.util.concurrent.ExecutorServiceHelper

public final class ExecutorServiceHelper
extends Object

Helper for ExecutorService to construct executors using a thread factory that create thread names with Camel prefix.

This helper should NOT be used by end users of Camel, as you should use ExecutorServiceStrategy which you obtain from CamelContext to create thread pools.

This helper should only be used internally in Camel.

Version:

Field Summary
static String DEFAULT_PATTERN
           
 
Method Summary
static ExecutorService getConfiguredExecutorService(RouteContext routeContext, String name, ExecutorServiceAwareDefinition definition)
          Will lookup and get the configured ExecutorService from the given definition.
static ScheduledExecutorService getConfiguredScheduledExecutorService(RouteContext routeContext, String name, ExecutorServiceAwareDefinition definition)
          Will lookup and get the configured ScheduledExecutorService from the given definition.
static String getThreadName(String pattern, String name)
          Creates a new thread name with the given prefix
static ExecutorService newCachedThreadPool(String pattern, String name, boolean daemon)
          Creates a new cached thread pool.
static ExecutorService newFixedThreadPool(int poolSize, String pattern, String name, boolean daemon)
          Creates a new fixed thread pool.
static ScheduledExecutorService newScheduledThreadPool(int poolSize, String pattern, String name, boolean daemon)
          Creates a new scheduled thread pool which can schedule threads.
static ExecutorService newSingleThreadExecutor(String pattern, String name, boolean daemon)
          Creates a new single thread pool (usually for background tasks)
static ExecutorService newSynchronousThreadPool()
          Creates a new synchronous executor service which always executes the task in the call thread (its just a thread pool facade)
static ExecutorService newThreadPool(String pattern, String name, int corePoolSize, int maxPoolSize)
          Creates a new custom thread pool using 60 seconds as keep alive and with an unbounded queue.
static ExecutorService newThreadPool(String pattern, String name, int corePoolSize, int maxPoolSize, int maxQueueSize)
          Creates a new custom thread pool using 60 seconds as keep alive and with bounded queue.
static ExecutorService newThreadPool(String pattern, String name, int corePoolSize, int maxPoolSize, long keepAliveTime, TimeUnit timeUnit, int maxQueueSize, RejectedExecutionHandler rejectedExecutionHandler, boolean daemon)
          Creates a new custom thread pool
static void timeoutTask(CompletionService completionService)
          Timeout the completion service.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_PATTERN

public static final String DEFAULT_PATTERN
See Also:
Constant Field Values
Method Detail

getThreadName

public static String getThreadName(String pattern,
                                   String name)
Creates a new thread name with the given prefix

Parameters:
pattern - the pattern
name - the name
Returns:
the thread name, which is unique

newScheduledThreadPool

public static ScheduledExecutorService newScheduledThreadPool(int poolSize,
                                                              String pattern,
                                                              String name,
                                                              boolean daemon)
Creates a new scheduled thread pool which can schedule threads.

Parameters:
poolSize - the core pool size
pattern - pattern of the thread name
name - ${name} in the pattern name
daemon - whether the threads is daemon or not
Returns:
the created pool

newFixedThreadPool

public static ExecutorService newFixedThreadPool(int poolSize,
                                                 String pattern,
                                                 String name,
                                                 boolean daemon)
Creates a new fixed thread pool.

Beware that the task queue is unbounded

Parameters:
poolSize - the fixed pool size
pattern - pattern of the thread name
name - ${name} in the pattern name
daemon - whether the threads is daemon or not
Returns:
the created pool

newSingleThreadExecutor

public static ExecutorService newSingleThreadExecutor(String pattern,
                                                      String name,
                                                      boolean daemon)
Creates a new single thread pool (usually for background tasks)

Parameters:
pattern - pattern of the thread name
name - ${name} in the pattern name
daemon - whether the threads is daemon or not
Returns:
the created pool

newCachedThreadPool

public static ExecutorService newCachedThreadPool(String pattern,
                                                  String name,
                                                  boolean daemon)
Creates a new cached thread pool.

Important: Using cached thread pool is discouraged as they have no upper bound and can overload the JVM.

Parameters:
pattern - pattern of the thread name
name - ${name} in the pattern name
daemon - whether the threads is daemon or not
Returns:
the created pool

newSynchronousThreadPool

public static ExecutorService newSynchronousThreadPool()
Creates a new synchronous executor service which always executes the task in the call thread (its just a thread pool facade)

Returns:
the created pool
See Also:
SynchronousExecutorService

newThreadPool

public static ExecutorService newThreadPool(String pattern,
                                            String name,
                                            int corePoolSize,
                                            int maxPoolSize)
Creates a new custom thread pool using 60 seconds as keep alive and with an unbounded queue.

Parameters:
pattern - pattern of the thread name
name - ${name} in the pattern name
corePoolSize - the core pool size
maxPoolSize - the maximum pool size
Returns:
the created pool

newThreadPool

public static ExecutorService newThreadPool(String pattern,
                                            String name,
                                            int corePoolSize,
                                            int maxPoolSize,
                                            int maxQueueSize)
Creates a new custom thread pool using 60 seconds as keep alive and with bounded queue.

Parameters:
pattern - pattern of the thread name
name - ${name} in the pattern name
corePoolSize - the core pool size
maxPoolSize - the maximum pool size
maxQueueSize - the maximum number of tasks in the queue, use Integer.MAX_VALUE or -1 to indicate unbounded
Returns:
the created pool

newThreadPool

public static ExecutorService newThreadPool(String pattern,
                                            String name,
                                            int corePoolSize,
                                            int maxPoolSize,
                                            long keepAliveTime,
                                            TimeUnit timeUnit,
                                            int maxQueueSize,
                                            RejectedExecutionHandler rejectedExecutionHandler,
                                            boolean daemon)
Creates a new custom thread pool

Parameters:
pattern - pattern of the thread name
name - ${name} in the pattern name
corePoolSize - the core pool size
maxPoolSize - the maximum pool size
keepAliveTime - keep alive time
timeUnit - keep alive time unit
maxQueueSize - the maximum number of tasks in the queue, use Integer.MAX_VALUE or -1 to indicate unbounded
rejectedExecutionHandler - the handler for tasks which cannot be executed by the thread pool. If null is provided then CallerRunsPolicy is used.
daemon - whether the threads is daemon or not
Returns:
the created pool
Throws:
IllegalArgumentException - if parameters is not valid

getConfiguredExecutorService

public static ExecutorService getConfiguredExecutorService(RouteContext routeContext,
                                                           String name,
                                                           ExecutorServiceAwareDefinition definition)
                                                    throws IllegalArgumentException
Will lookup and get the configured ExecutorService from the given definition.

This method will lookup for configured thread pool in the following order

The various ExecutorServiceAwareDefinition should use this helper method to ensure they support configured executor services in the same coherent way.

Parameters:
routeContext - the rout context
name - name which is appended to the thread name, when the ExecutorService is created based on a ThreadPoolProfile.
definition - the node definition which may leverage executor service.
Returns:
the configured executor service, or null if none was configured.
Throws:
IllegalArgumentException - is thrown if lookup of executor service in Registry was not found

getConfiguredScheduledExecutorService

public static ScheduledExecutorService getConfiguredScheduledExecutorService(RouteContext routeContext,
                                                                             String name,
                                                                             ExecutorServiceAwareDefinition definition)
                                                                      throws IllegalArgumentException
Will lookup and get the configured ScheduledExecutorService from the given definition.

This method will lookup for configured thread pool in the following order

The various ExecutorServiceAwareDefinition should use this helper method to ensure they support configured executor services in the same coherent way.

Parameters:
routeContext - the rout context
name - name which is appended to the thread name, when the ExecutorService is created based on a ThreadPoolProfile.
definition - the node definition which may leverage executor service.
Returns:
the configured executor service, or null if none was configured.
Throws:
IllegalArgumentException - is thrown if lookup of executor service in Registry was not found or the found instance is not a ScheduledExecutorService type.

timeoutTask

public static void timeoutTask(CompletionService completionService)
Timeout the completion service.

This can be used to mark the completion service as timed out, allowing you to poll any already completed tasks. This applies when using the SubmitOrderedCompletionService.

Parameters:
completionService - the completion service.


Apache CAMEL