In the Using custom thread pools recipe we saw how thread pools could be defined. These can be re-used across multiple EIPs and routes.
This recipe will take that idea to the next level by guiding you through defining thread pool profiles, which act as templates for thread pool creation. By referring to a thread pool profile, an EIP will have a thread pool created for its own exclusive use with the characteristics defined in the profile.
The Java code for this recipe is located in the org.camelcookbook.parallelprocessing.threadpoolprofiles
package. The Spring XML files are located under src/main/resources/META-INF/spring
and prefixed with threadPoolProfiles
.
Configure a threadPoolProfile
class that acts as a template for any thread pools created within your routes.
threadPoolProfile
block before any threadPool
and route
definitions:<camelContext
xmlns="http://camel.apache.org/schema/spring">
<threadPoolProfile id="customThreadPoolProfile"
poolSize="5"/>
<threadPool ../>
<route>
<!-- ... -->
<route/>
</camelContext>
To instantiate a profile in Java, you use the org.apache.camel.builder.ThreadPoolProfileBuilder
class:
ThreadPoolProfile customThreadPoolProfile =
new ThreadPoolProfileBuilder(
"customThreadPoolProfile")
.poolSize(5).maxQueueSize(100).build();
The first argument is the name that the profile will be registered with. You then register the profile into the Camel context:
context.getExecutorServiceManager() .registerThreadPoolProfile(customThreadPoolProfile);
id
attribute of the threadPoolProfile
class within the executorServiceRef
attribute of any EIPs that support parallel processing.The following XML DSL fragment initializes a threads
block based on our customThreadPoolProfile
:
<threads executorServiceRef="customThreadPoolProfile">
The Java DSL can also use thread pool profiles defined in this manner:
.threads().executorServiceRef("customThreadPoolProfile")
When the Camel context starts up, it attempts to satisfy any EIPs that requested an ExecutorService
instance by name. If an ExecutorService
instance is available with that name in the Camel Registry, it will be used. If none is found, Camel will check with its internal ExecutorServiceManager
to see whether a thread pool profile exists that matches that name.
Setting an executorServiceRef
name that does not resolve to either an ExecutorService
instance or a thread pool profile will cause an error to be thrown on Camel context startup.
There are a number of options that can be customized within a threadPoolProfile
definition—these correspond to the equivalent threadPool
options (see the Using custom thread pools recipe). The only exception is that you cannot define the thread names.
3.144.30.236