结果
最终的问题有点蠢,是由于不了解 Camel JMS组件默认是单线程去消费,导致即使加线程池了也不起作用
然后官网能找到的都是在 from 里面去设置:
from("jms:SomeQueue?concurrentConsumers=20&asyncConsumer=true")
假如是通过自己根据不同的 connectionFactory 去定义 JmsComponent,就搜索不到了。例如想要连接TibjmsConnectionFactory,SolConnectionFactory等等。正确写法如下:
JmsComponent jmsComponent = new JmsComponent()
jmsComponent.setConnectionFactory(connectionFactory)
jmsComponent.setAsyncConsumer(true) // 这一句很关键
jmsComponent.setConcurrentConsumers(20) // 指定并发消费者的数量
前言
因为每秒钟要处理的消息太多了, Apache Camel 的单线程运行处理不过来,导致很多消息 pending 在 queue 里面,就打算用多线程运行的方式解决这个问题
第一次尝试
我给他设置了一个10大小的线程池,应该可以完成了吧
from(fromUri).threads(10)
结果看日志发现不行,还是一个一个处理
第二次尝试
是不是不能用 Camel 的线程池,自己写一个线程池给他?
.threads().executorService(executorService)
结果还是不行
第三次尝试
可能要是通过多播的形式,然后通过 ParallelProcessing 来并发处理?然后代码改成了这样:
ExecutorService executorService = Executors.newFixedThreadPool(10);
MulticastDefinition multicastDefinition = from(fromUri).multicast();
multicastDefinition.setParallelProcessing(true);
multicastDefinition.setExecutorService(executorService);
multicastDefinition.to(
xxxxx
).end();
结果还是不行,然后就没想法了,不断去 Google ,但是大家都好像只是加了个线程池就可以并发处理了,并没有做些别的操作。国内也好像基本没有人去用 Apache Camel
第四次尝试
我也跟官网的一样,在 from 那里去拼接一些参数,但因为我是用了自己写的 JMS 组件,所以没有生效
最终
然后突然想到会不会在 from 前去设置一些什么来去让他异步消费能够实现,然后代码就如结果所示,最后也跟设想一样实现了异步处理消息