0

在 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">
      ....

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

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

4

1 回答 1

0

所以我必须引用 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">
    
于 2022-02-01T19:43:55.190 回答