简体   繁体   English

骆驼多线程使用者

[英]Camel Multi-threaded Consumer

I have a DB with orders, each with a due date and a creation date. 我有一个带有订单的数据库,每个订单都有一个到期日和一个创建日期。 I want to pull a maximum of 4 orders into route and process them simultaneously. 我想最多将4个订单拉入路线并同时处理它们。 Each order may take between 10-20 minutes to process. 每个订单可能需要10到20分钟才能处理。 But I would like to keep all threads going as much as possible, not having any downtime. 但是我想让所有线程尽可能地运行,而不造成任何停机。

Here is what I have now: 这是我现在所拥有的:

from("timer://GetOrder?fixedRate=true&period=1s")
            .to("bean:orderInfoDao?method=getNextOrder")
            .to("jms://process-orders")
            .end();

from("jms://process-orders?concurrentConsumers=4")
            .to("bean:orderService?method=processOrder(${body})")
            .to("direct:send-result")
            .end();

The getNextOrder DAO function returns the oldest order by creation date, which has passed its due date. getNextOrder DAO函数按创建日期返回最早的订单,该日期已超过其到期日期。 Incoming order are attempted immediately. 立即尝试输入订单。

Right now, the issue is that incoming orders pile up in the JMS route as a result of the timer and when getNextOrder returns a much older order, it is way behind in the queue. 现在,问题在于,由于计时器的原因,传入的订单在JMS路由中堆积,当getNextOrder返回更旧的订单时,它在队列中落后了。

Any ideas how I can structure these routes so that the DB is polled for the oldest 4 orders and those are executed simultaneously? 有什么想法可以构造这些路由,以便轮询数据库中最旧的4个订单并同时执行这些订单? Changes to the DAO are acceptable. 可以更改DAO。

Is there any kind of multi-threaded producer? 是否有任何一种多线程生产者?

Thanks in advance for suggestions! 预先感谢您的建议!

final Semaphore semaphore = new Semaphore(4); 

from("timer://GetOrder?period=1s")
            .to("bean:orderInfoDao?method=getNextOrder")
            .to("jms://process-orders")
            .process(new Processor() {
                 public void process(Exchange exchange) {
                     semaphore.acquire();
                 }
             })
            .end();

from("jms://process-orders?concurrentConsumers=4")
            .to("bean:orderService?method=processOrder(${body})")
            .process(new Processor() {
                 public void process(Exchange exchange) {
                     semaphore.release();
                 }
             })
            .to("direct:send-result")
            .end();

Notice that timer fixedRate is off (default). 请注意,计时器fixedRate已关闭(默认)。

This is the first idea that came to my mind, I hope that there are some Camel EIPs which can help to implement this logic in a better way. 这是我想到的第一个想法,我希望有一些Camel EIP可以帮助以更好的方式实现此逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM