Using thread pool profiles

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.

Getting ready

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.

How to do it...

Configure a threadPoolProfile class that acts as a template for any thread pools created within your routes.

  1. In the XML DSL, define a 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);
  2. Use the 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")

How it works...

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.

Note

A thread pool reference that resolves to a profile will have its own unique ExecutorService instance created for it by Camel. Therefore, if two EIPs both ask for the same profile, two thread pools will be created, one for each EIP instance.

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's more...

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.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.76.237