如何为 Apache Camel 路由的拆分准备执行器服务,org.apache.camel.spi.ThreadPoolProfile vs java.util.concurrent.ExecutorService

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

在 Apache Camel 的多线程指南中,有一个关于 ThreadPoolProfile 用作 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>

我期待使用拆分的路由采用相同的方法,因为它也具有 executorServiceRef 属性。

所以,我准备在注册表中注册 bean:

    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);

然后我在拆分中以相同的方式引用执行程序服务:

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

但令人惊讶的是,它需要另一种对象类型:

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

那么,我应该将什么作为执行程序服务传递,以及如果此处不接受,如何从 ThreadPoolProfile 生成它。 split.

没有任何自定义线程池配置文件示例

所以我必须引用 ExecutorService 并为其创建一个对象并将其放入注册表:

  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");

将其添加到注册表

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

在路线 XML

中调用它们
<split parallelProcessing="true" executorServiceRef="only5threads">