public interface ExecutorServiceManager extends ShutdownableService, StaticService
This manager is pluggable so you can plugin a custom provider, for example if you want to leverage the WorkManager for a JEE server.
You may want to just implement a custom ThreadPoolFactory
and rely on the
org.apache.camel.impl.DefaultExecutorServiceManager
, if that is sufficient. The ThreadPoolFactory
is
always used for creating the actual thread pools. You can implement a custom ThreadPoolFactory
to leverage
the WorkManager for a JEE server.
The ThreadPoolFactory
has pure JDK API, where as this ExecutorServiceManager
has Camel API concepts
such as ThreadPoolProfile
. Therefore it may be easier to only implement a custom ThreadPoolFactory
.
This manager has fine grained methods for creating various thread pools, however custom strategies do not have to exactly create those kind of pools. Feel free to return a shared or different kind of pool.
If you use the newXXX methods to create thread pools, then Camel will by default take care of shutting down
those created pools when CamelContext
is shutting down.
For more information about shutting down thread pools see the shutdown(java.util.concurrent.ExecutorService)
and shutdownNow(java.util.concurrent.ExecutorService)
, and getShutdownAwaitTermination()
methods.
Notice the details about using a graceful shutdown at first, and then falling back to aggressive shutdown in case of
await termination timeout occurred.
ThreadPoolFactory
Modifier and Type | Method and Description |
---|---|
boolean |
awaitTermination(ExecutorService executorService,
long shutdownAwaitTermination)
Awaits the termination of the thread pool.
|
ThreadPoolProfile |
getDefaultThreadPoolProfile()
Gets the default thread pool profile
|
long |
getShutdownAwaitTermination()
Gets the time to wait for thread pools to shutdown orderly, when invoking the
ShutdownableService.shutdown() method. |
String |
getThreadNamePattern()
Gets the thread name pattern to use
|
ThreadPoolFactory |
getThreadPoolFactory()
Gets the
ThreadPoolFactory to use for creating the thread pools. |
ThreadPoolProfile |
getThreadPoolProfile(String id)
Gets the thread pool profile by the given id
|
ExecutorService |
newCachedThreadPool(Object source,
String name)
Creates a new cached thread pool.
|
ScheduledExecutorService |
newDefaultScheduledThreadPool(Object source,
String name)
Creates a new scheduled thread pool using the default thread pool profile.
|
ExecutorService |
newDefaultThreadPool(Object source,
String name)
Creates a new thread pool using the default thread pool profile.
|
ExecutorService |
newFixedThreadPool(Object source,
String name,
int poolSize)
Creates a new fixed thread pool (the pool will not grow or shrink but has a fixed number of threads constantly).
|
ScheduledExecutorService |
newScheduledThreadPool(Object source,
String name,
int poolSize)
Creates a new scheduled thread pool.
|
ScheduledExecutorService |
newScheduledThreadPool(Object source,
String name,
String profileId)
Creates a new scheduled thread pool using a profile id
|
ScheduledExecutorService |
newScheduledThreadPool(Object source,
String name,
ThreadPoolProfile profile)
Creates a new scheduled thread pool using a profile
|
ExecutorService |
newSingleThreadExecutor(Object source,
String name)
Creates a new single-threaded thread pool.
|
ScheduledExecutorService |
newSingleThreadScheduledExecutor(Object source,
String name)
Creates a new single-threaded thread pool.
|
Thread |
newThread(String name,
Runnable runnable)
Creates a new daemon thread with the given name.
|
ExecutorService |
newThreadPool(Object source,
String name,
int poolSize,
int maxPoolSize)
Creates a new thread pool.
|
ExecutorService |
newThreadPool(Object source,
String name,
String profileId)
Creates a new thread pool using using the given profile id
|
ExecutorService |
newThreadPool(Object source,
String name,
ThreadPoolProfile profile)
Creates a new thread pool using the given profile
|
void |
registerThreadPoolProfile(ThreadPoolProfile profile)
Registers the given thread pool profile
|
String |
resolveThreadName(String name)
Creates a full thread name
|
void |
setDefaultThreadPoolProfile(ThreadPoolProfile defaultThreadPoolProfile)
Sets the default thread pool profile
|
void |
setShutdownAwaitTermination(long timeInMillis)
Sets the time to wait for thread pools to shutdown orderly, when invoking the
ShutdownableService.shutdown() method. |
void |
setThreadNamePattern(String pattern)
Sets the thread name pattern used for creating the full thread name.
|
void |
setThreadPoolFactory(ThreadPoolFactory threadPoolFactory)
Sets a custom
ThreadPoolFactory to use |
void |
shutdown(ExecutorService executorService)
Shutdown the given executor service (not graceful).
|
void |
shutdownGraceful(ExecutorService executorService)
Shutdown the given executor service graceful at first, and then aggressively if the await termination timeout was
hit.
|
void |
shutdownGraceful(ExecutorService executorService,
long shutdownAwaitTermination)
Shutdown the given executor service graceful at first, and then aggressively if the await termination timeout was
hit.
|
List<Runnable> |
shutdownNow(ExecutorService executorService)
Shutdown now the given executor service aggressively.
|
shutdown
ThreadPoolFactory getThreadPoolFactory()
ThreadPoolFactory
to use for creating the thread pools.void setThreadPoolFactory(ThreadPoolFactory threadPoolFactory)
ThreadPoolFactory
to usethreadPoolFactory
- the thread pool factoryString resolveThreadName(String name)
name
- name which is appended to the full thread nameThreadPoolProfile getThreadPoolProfile(String id)
id
- id of the thread pool profile to getvoid registerThreadPoolProfile(ThreadPoolProfile profile)
profile
- the profilevoid setDefaultThreadPoolProfile(ThreadPoolProfile defaultThreadPoolProfile)
defaultThreadPoolProfile
- the new default thread pool profileThreadPoolProfile getDefaultThreadPoolProfile()
void setThreadNamePattern(String pattern) throws IllegalArgumentException
The default pattern is: Camel (#camelId#) thread ##counter# - #name#
Where #camelId# is the name of the CamelContext
and #counter# is a unique incrementing counter.
and #name# is the regular thread name.
You can also use #longName# is the long thread name which can include endpoint parameters etc.
pattern
- the patternIllegalArgumentException
- if the pattern is invalid.String getThreadNamePattern()
void setShutdownAwaitTermination(long timeInMillis)
ShutdownableService.shutdown()
method.
The default value is 10000 millis.
timeInMillis
- time in millis.long getShutdownAwaitTermination()
ShutdownableService.shutdown()
method.
The default value is 10000 millis.
Thread newThread(String name, Runnable runnable)
name
- name which is appended to the thread namerunnable
- a runnable to be executed by new thread instanceExecutorService newDefaultThreadPool(Object source, String name)
source
- the source object, usually it should be this passed in as parametername
- name which is appended to the thread nameScheduledExecutorService newDefaultScheduledThreadPool(Object source, String name)
source
- the source object, usually it should be this passed in as parametername
- name which is appended to the thread nameExecutorService newThreadPool(Object source, String name, ThreadPoolProfile profile)
source
- the source object, usually it should be this passed in as parametername
- name which is appended to the thread nameprofile
- the profile with the thread pool settings to useExecutorService newThreadPool(Object source, String name, String profileId)
source
- the source object, usually it should be this passed in as parametername
- name which is appended to the thread nameprofileId
- the id of the profile with the thread pool settings to useExecutorService newThreadPool(Object source, String name, int poolSize, int maxPoolSize)
Will fallback and use values from the default thread pool profile for keep alive time, rejection policy and other parameters which cannot be specified.
source
- the source object, usually it should be this passed in as parametername
- name which is appended to the thread namepoolSize
- the core pool sizemaxPoolSize
- the maximum pool sizeExecutorService newSingleThreadExecutor(Object source, String name)
Notice that there will always be a single thread in the pool. If you want the pool to be able to shrink to no threads, then use the newThreadPool method, and use 0 in core pool size, and 1 in max pool size.
source
- the source object, usually it should be this passed in as parametername
- name which is appended to the thread nameExecutorService newCachedThreadPool(Object source, String name)
Important: Using cached thread pool should be used by care as they have no upper bound on created threads, and have no task backlog, and can therefore overload the JVM.
source
- the source object, usually it should be this passed in as parametername
- name which is appended to the thread nameExecutorService newFixedThreadPool(Object source, String name, int poolSize)
source
- the source object, usually it should be this passed in as parametername
- name which is appended to the thread namepoolSize
- the core pool sizeScheduledExecutorService newScheduledThreadPool(Object source, String name, int poolSize)
source
- the source object, usually it should be this passed in as parametername
- name which is appended to the thread namepoolSize
- the core pool sizeScheduledExecutorService newSingleThreadScheduledExecutor(Object source, String name)
source
- the source object, usually it should be this passed in as parametername
- name which is appended to the thread nameScheduledExecutorService newScheduledThreadPool(Object source, String name, ThreadPoolProfile profile)
source
- the source object, usually it should be this passed in as parametername
- name which is appended to the thread nameprofile
- the profile with the thread pool settings to useScheduledExecutorService newScheduledThreadPool(Object source, String name, String profileId)
source
- the source object, usually it should be this passed in as parametername
- name which is appended to the thread nameprofileId
- the id of the profile with the thread pool settings to usevoid shutdown(ExecutorService executorService)
This implementation will issues a regular shutdown of the executor service, ie calling
ExecutorService.shutdown()
and return.
executorService
- the executor service to shutdownExecutorService.shutdown()
void shutdownGraceful(ExecutorService executorService)
Will try to perform an orderly shutdown by giving the running threads time to complete tasks, before going more
aggressively by doing a shutdownNow(java.util.concurrent.ExecutorService)
which forces a shutdown. The
getShutdownAwaitTermination()
is used as timeout value waiting for orderly shutdown to complete
normally, before going aggressively.
executorService
- the executor service to shutdownExecutorService.shutdown()
,
getShutdownAwaitTermination()
void shutdownGraceful(ExecutorService executorService, long shutdownAwaitTermination)
Will try to perform an orderly shutdown by giving the running threads time to complete tasks, before going more
aggressively by doing a shutdownNow(java.util.concurrent.ExecutorService)
which forces a shutdown. The
parameter shutdownAwaitTermination is used as timeout value waiting for orderly shutdown to complete
normally, before going aggressively.
executorService
- the executor service to shutdownshutdownAwaitTermination
- timeout in millis to wait for orderly shutdownExecutorService.shutdown()
List<Runnable> shutdownNow(ExecutorService executorService)
This implementation will issues a regular shutdownNow of the executor service, ie calling
ExecutorService.shutdownNow()
and return.
executorService
- the executor service to shutdown nowExecutorService.shutdownNow()
boolean awaitTermination(ExecutorService executorService, long shutdownAwaitTermination) throws InterruptedException
This implementation will log every 2nd second at INFO level that we are waiting, so the end user can see we are not hanging in case it takes longer time to terminate the pool.
executorService
- the thread poolshutdownAwaitTermination
- time in millis to use as timeoutInterruptedException
- is thrown if we are interrupted during waitingApache Camel