以下是我期望的路由,我希望从 queue1
中同时处理10条消息,但事实上只有一条消息在同一时间被处理。
我是期望错了吗?还是我的做法有误?
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 );
}
};
}
使用以下方式调用上述路由:
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");
}
}
代码输出结果:
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
如果我们观察时间戳,我们会发现路由器一次只处理一个消息。