javaapache-camelcamel-spring-dsl

How to prepare an Executor service for Apache Camel route's Split, org.apache.camel.spi.ThreadPoolProfile vs java.util.concurrent.ExecutorService


In the multithreading guide of Apache Camel there is an example on ThreadPoolProfile usage as a parameter for executorServiceRef (https://camel.apache.org/manual/threading-model.html):

<threadPoolProfile xmlns="http://camel.apache.org/schema/spring" id="fooProfile"
                       poolSize="20" maxPoolSize="50" maxQueueSize="-1"/>
<route>
       <multicast strategyRef="myStrategy" executorServiceRef="fooProfile">
          ...
       </multicast>
    </route>

I was expecting the same approach for the routes using split, as it has executorServiceRef attribute too.

So, I prepare ang register the bean in the registry:

    ThreadPoolProfileBuilder builder = new ThreadPoolProfileBuilder("only5threads");
    ThreadPoolProfile only5threads = builder.poolSize(5).maxPoolSize(5).maxQueueSize(-1).build();
    final org.apache.camel.impl.SimpleRegistry customRegistry = new org.apache.camel.impl.SimpleRegistry();
    customRegistry.put("only5threads", only5threads);

then I refer the executor service the same way in my split:

    <split parallelProcessing="true" executorServiceRef="only5threads">
      ....

But surprisingly, it expects another Object type:

Caused by: java.lang.ClassCastException: Cannot cast org.apache.camel.spi.ThreadPoolProfile to java.util.concurrent.ExecutorService
    at java.base/java.lang.Class.cast(Class.java:3611)
    at org.apache.camel.impl.SimpleRegistry.lookupByNameAndType(SimpleRegistry.java:47)
    ... 50 common frames omitted

So, what I should pass as an executor service and how to produce it from ThreadPoolProfile if it is not accepted here. There are no any examples of custom Thread Pool Profile for split.


Solution

  • So I have to refer the ExecutorService and create an object of it and put it to the registry:

      ThreadPoolProfileBuilder builder = new ThreadPoolProfileBuilder("only5threads");
            ThreadPoolProfile threadPoolProfile = builder.poolSize(5).maxPoolSize(5).maxQueueSize(-1).build();
            DefaultExecutorServiceManager defaultExecutorServiceManager = new DefaultExecutorServiceManager(camelContext);
            defaultExecutorServiceManager.setDefaultThreadPoolProfile(threadPoolProfile);
            ExecutorService executorService = defaultExecutorServiceManager.newDefaultScheduledThreadPool("only5threads","only5threads");
    

    adding it to registry

     final org.apache.camel.impl.SimpleRegistry customRegistry = new 
    org.apache.camel.impl.SimpleRegistry(); customRegistry.put("only5threads", only5threads);
    

    calling them in route's XML

    <split parallelProcessing="true" executorServiceRef="only5threads">