With below route i expect that 10 msg from queue1
should be process concurrently, but only one gets process at a time.
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:
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.
You need to enable asyncConsumer
on the JMS endpoint to allow it to be async. 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. See the options concurrentConsumers
and maxConcurrentConsumers
for more details.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.