简体   繁体   English

在camel中使用队列线程DSL行为

[英]Thread DSL behavior with queue in camel

With below route i expect that 10 msg from queue1 should be process concurrently, but only one gets process at a time. 在下面的路由中,我期望来自queue1 10 msg应该同时处理,但是一次只有一个获得进程。

Am i expecting wrong ? 我错了吗? or doing something wrong ? 或做错了什么?

context.addRoutes(new RouteBuilder() {
        public void configure() {                                       
            from("test-jms:queue:test.queue1").threads(10)
            .process(sleep(1)); // sleep id is 1                
        }

        private Processor sleep(final int sleepId) {
            return new Processor() {                    
                @Override
                public void process(Exchange exchange) throws Exception {                       
                    System.out.println(curTime() + " Going for sleep sleepid=" + sleepId );
                    Thread.sleep(5000l);                        
                    System.out.println(curTime() + " Done sleep sleepid=" + sleepId );
                }
            };
        }

Calling the above routes using: 使用以下方式呼叫上述路线:

   ExecutorService ec = Executors.newFixedThreadPool(5);

    ec.submit(new Task(context,template));
    ec.submit(new Task(context,template));
    ec.submit(new Task(context,template));
    ec.submit(new Task(context,template));
    ec.submit(new Task(context,template));

static class Task  implements Runnable{
    CamelContext context;
    ProducerTemplate template;
    public Task(CamelContext context, ProducerTemplate template) {
        super();
        this.context = context;
        this.template = template;
    }
    @Override
    public void run() {         
           Exchange exchange = new DefaultExchange(context);
           exchange.setPattern(ExchangePattern.InOnly);
           exchange.getIn().setBody("Test Message: " + Thread.currentThread().getName());
           System.out.println(Thread.currentThread().getName());
           Exchange send = template.send("test-jms:queue:test.queue1",exchange);
           System.out.println("completed");           
    }

}

OutPut from code: 来自代码的OutPut:

10:24:11 Going for sleep sleepid=1
10:24:16 Done sleep sleepid=1

10:24:16 Going for sleep sleepid=1
10:24:21 Done sleep sleepid=1

10:24:21 Going for sleep sleepid=1
10:24:26 Done sleep sleepid=1

10:24:26 Going for sleep sleepid=1
10:24:31 Done sleep sleepid=1

10:24:31 Going for sleep sleepid=1
10:24:36 Done sleep sleepid=1

If we observe the timestamp we will see that route is only processing 1 msg at time. 如果我们观察时间戳,我们将看到该路由仅处理1 msg。

You need to enable asyncConsumer on the JMS endpoint to allow it to be async. 您需要在JMS端点上启用asyncConsumer以允许它为异步。 When doing this then messages consumed from the queue can be processed out of order, and hence why a consumer is ordered by default. 执行此操作时,可以不按顺序处理从队列中消耗的消息,从而默认情况下订购消费者的原因。

The code should be 代码应该是

 public void configure() {                                       
            from("test-jms:queue:test.queue1?asyncConsumer=true").threads(10)
            .process(sleep(1)); // sleep id is 1                
        }

But the JMS component has built-in concurrency which is usually better to use, as then it can use concurrent JMS consumers, and concurrent networking. 但是JMS组件具有内置并发性,通常可以更好地使用,因为它可以使用并发JMS使用者和并发网络。 See the options concurrentConsumers and maxConcurrentConsumers for more details. 有关更多详细信息,请参阅concurrentConsumersmaxConcurrentConsumers选项。

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

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