Package org.apache.camel.spi
Interface ExecutorServiceManager
- All Superinterfaces:
AutoCloseable
,Service
,ShutdownableService
,StaticService
Strategy to create thread pools.
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
, if that is sufficient. The
ThreadPoolFactory
and rely on the
invalid reference
org.apache.camel.impl.DefaultExecutorServiceManager
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.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interface
Listener when a newThreadFactory
is created, which allows to plugin custom behaviour. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addThreadFactoryListener
(ExecutorServiceManager.ThreadFactoryListener threadFactoryListener) Adds a customExecutorServiceManager.ThreadFactoryListener
to useboolean
awaitTermination
(ExecutorService executorService, long shutdownAwaitTermination) Awaits the termination of the thread pool.Gets the default thread pool profilelong
Gets the time to wait for thread pools to shutdown orderly, when invoking theShutdownableService.shutdown()
method.Gets the thread name pattern to useGets theThreadPoolFactory
to use for creating the thread pools.Gets the thread pool profile by the given idnewCachedThreadPool
(Object source, String name) Creates a new cached thread pool.newDefaultScheduledThreadPool
(Object source, String name) Creates a new scheduled thread pool using the default thread pool profile.newDefaultThreadPool
(Object source, String name) Creates a new thread pool using the default thread pool profile.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).newScheduledThreadPool
(Object source, String name, int poolSize) Creates a new scheduled thread pool.newScheduledThreadPool
(Object source, String name, String profileId) Creates a new scheduled thread pool using a profile idnewScheduledThreadPool
(Object source, String name, ThreadPoolProfile profile) Creates a new scheduled thread pool using a profilenewSingleThreadExecutor
(Object source, String name) Creates a new single-threaded thread pool.newSingleThreadScheduledExecutor
(Object source, String name) Creates a new single-threaded thread pool.Creates a new daemon thread with the given name.newThreadPool
(Object source, String name, int poolSize, int maxPoolSize) Creates a new thread pool.newThreadPool
(Object source, String name, String profileId) Creates a new thread pool using using the given profile idnewThreadPool
(Object source, String name, ThreadPoolProfile profile) Creates a new thread pool using the given profilevoid
Registers the given thread pool profileresolveThreadName
(String name) Creates a full thread namevoid
setDefaultThreadPoolProfile
(ThreadPoolProfile defaultThreadPoolProfile) Sets the default thread pool profilevoid
setShutdownAwaitTermination
(long timeInMillis) Sets the time to wait for thread pools to shutdown orderly, when invoking theShutdownableService.shutdown()
method.void
setThreadNamePattern
(String pattern) Sets the thread name pattern used for creating the full thread name.void
setThreadPoolFactory
(ThreadPoolFactory threadPoolFactory) Sets a customThreadPoolFactory
to usevoid
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.shutdownNow
(ExecutorService executorService) Shutdown now the given executor service aggressively.Methods inherited from interface org.apache.camel.ShutdownableService
shutdown
-
Method Details
-
addThreadFactoryListener
Adds a customExecutorServiceManager.ThreadFactoryListener
to use- Parameters:
threadFactoryListener
- the thread factory listener
-
getThreadPoolFactory
ThreadPoolFactory getThreadPoolFactory()Gets theThreadPoolFactory
to use for creating the thread pools.- Returns:
- the thread pool factory
-
setThreadPoolFactory
Sets a customThreadPoolFactory
to use- Parameters:
threadPoolFactory
- the thread pool factory
-
resolveThreadName
Creates a full thread name- Parameters:
name
- name which is appended to the full thread name- Returns:
- the full thread name
-
getThreadPoolProfile
Gets the thread pool profile by the given id- Parameters:
id
- id of the thread pool profile to get- Returns:
- the found profile, or null if not found
-
registerThreadPoolProfile
Registers the given thread pool profile- Parameters:
profile
- the profile
-
setDefaultThreadPoolProfile
Sets the default thread pool profile- Parameters:
defaultThreadPoolProfile
- the new default thread pool profile
-
getDefaultThreadPoolProfile
ThreadPoolProfile getDefaultThreadPoolProfile()Gets the default thread pool profile- Returns:
- the default profile which are newer null
-
setThreadNamePattern
Sets the thread name pattern used for creating the full thread name. The default pattern is: Camel (#camelId#) thread ##counter# - #name# Where #camelId# is the name of theCamelContext
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.- Parameters:
pattern
- the pattern- Throws:
IllegalArgumentException
- if the pattern is invalid.
-
getThreadNamePattern
String getThreadNamePattern()Gets the thread name pattern to use- Returns:
- the pattern
-
setShutdownAwaitTermination
void setShutdownAwaitTermination(long timeInMillis) Sets the time to wait for thread pools to shutdown orderly, when invoking theShutdownableService.shutdown()
method. The default value is 10000 millis.- Parameters:
timeInMillis
- time in millis.
-
getShutdownAwaitTermination
long getShutdownAwaitTermination()Gets the time to wait for thread pools to shutdown orderly, when invoking theShutdownableService.shutdown()
method. The default value is 10000 millis.- Returns:
- the timeout value
-
newThread
Creates a new daemon thread with the given name.- Parameters:
name
- name which is appended to the thread namerunnable
- a runnable to be executed by new thread instance- Returns:
- the created thread
-
newDefaultThreadPool
Creates a new thread pool using the default thread pool profile.- Parameters:
source
- the source object, usually it should be this passed in as parametername
- name which is appended to the thread name- Returns:
- the created thread pool
-
newDefaultScheduledThreadPool
Creates a new scheduled thread pool using the default thread pool profile.- Parameters:
source
- the source object, usually it should be this passed in as parametername
- name which is appended to the thread name- Returns:
- the created thread pool
-
newThreadPool
Creates a new thread pool using the given profile- Parameters:
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 use- Returns:
- the created thread pool
-
newThreadPool
Creates a new thread pool using using the given profile id- Parameters:
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 use- Returns:
- the created thread pool, or null if the thread pool profile could not be found
-
newThreadPool
Creates a new thread pool. Will fallback and use values from the default thread pool profile for keep alive time, rejection policy and other parameters which cannot be specified.- Parameters:
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 size- Returns:
- the created thread pool
-
newSingleThreadExecutor
Creates a new single-threaded thread pool. This is often used for background threads. 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.- Parameters:
source
- the source object, usually it should be this passed in as parametername
- name which is appended to the thread name- Returns:
- the created thread pool
-
newCachedThreadPool
Creates a new cached thread pool. 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.- Parameters:
source
- the source object, usually it should be this passed in as parametername
- name which is appended to the thread name- Returns:
- the created thread pool
-
newFixedThreadPool
Creates a new fixed thread pool (the pool will not grow or shrink but has a fixed number of threads constantly).- Parameters:
source
- the source object, usually it should be this passed in as parametername
- name which is appended to the thread namepoolSize
- the core pool size- Returns:
- the created thread pool
-
newScheduledThreadPool
Creates a new scheduled thread pool.- Parameters:
source
- the source object, usually it should be this passed in as parametername
- name which is appended to the thread namepoolSize
- the core pool size- Returns:
- the created thread pool
-
newSingleThreadScheduledExecutor
Creates a new single-threaded thread pool. This is often used for background threads.- Parameters:
source
- the source object, usually it should be this passed in as parametername
- name which is appended to the thread name- Returns:
- the created thread pool
-
newScheduledThreadPool
ScheduledExecutorService newScheduledThreadPool(Object source, String name, ThreadPoolProfile profile) Creates a new scheduled thread pool using a profile- Parameters:
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 use- Returns:
- created thread pool
-
newScheduledThreadPool
Creates a new scheduled thread pool using a profile id- Parameters:
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 use- Returns:
- created thread pool
-
shutdown
Shutdown the given executor service (not graceful). This implementation will issue a regular shutdown of the executor service, ie callingExecutorService.shutdown()
and return.- Parameters:
executorService
- the executor service to shutdown- See Also:
-
shutdownGraceful
Shutdown the given executor service graceful at first, and then aggressively if the await termination timeout was hit. Will try to perform an orderly shutdown by giving the running threads time to complete tasks, before going more aggressively by doing ashutdownNow(java.util.concurrent.ExecutorService)
which forces a shutdown. ThegetShutdownAwaitTermination()
is used as timeout value waiting for orderly shutdown to complete normally, before going aggressively.- Parameters:
executorService
- the executor service to shutdown- See Also:
-
shutdownGraceful
Shutdown the given executor service graceful at first, and then aggressively if the await termination timeout was hit. Will try to perform an orderly shutdown by giving the running threads time to complete tasks, before going more aggressively by doing ashutdownNow(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.- Parameters:
executorService
- the executor service to shutdownshutdownAwaitTermination
- timeout in millis to wait for orderly shutdown- See Also:
-
shutdownNow
Shutdown now the given executor service aggressively. This implementation will issues a regular shutdownNow of the executor service, ie callingExecutorService.shutdownNow()
and return.- Parameters:
executorService
- the executor service to shutdown now- Returns:
- list of tasks that never commenced execution
- See Also:
-
awaitTermination
boolean awaitTermination(ExecutorService executorService, long shutdownAwaitTermination) throws InterruptedException Awaits the termination of the thread pool. 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.- Parameters:
executorService
- the thread poolshutdownAwaitTermination
- time in millis to use as timeout- Returns:
- true if the pool is terminated, or false if we timed out
- Throws:
InterruptedException
- is thrown if we are interrupted during waiting
-