我正在努力处理用例,我们必须拆分消息,将拆分消息发送到JMS队列,然后在队列中处理完第一条消息中的所有拆分项后执行某些操作。
我知道我可以在分割时使用parallelProcessing,但由于这个原因,在这种情况下进入那种情况是不可能的。
我已经让JMS将replyTo消息发送到队列。是否有任何方法可以在split()之后生成end()to(" jms:aqueuethatreplies")阻止直到收到所有对该分割的回复?
这里有一些示例代码 - 我喜欢"已完成:"只有在我已经看到所有相应的"收到的项目时才会打印出来的消息:"消息。
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.*;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.impl.DefaultCamelContext;
import javax.jms.ConnectionFactory;
import java.util.Random;
public class SplitAndAggregate {
public static void main(String args[]) throws Exception {
CamelContext context = new DefaultCamelContext();
ConnectionFactory connectionFactory =
new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
context.addComponent("jms",
JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
// add our route to the CamelContext
context.addRoutes(new RouteBuilder() {
@Override
public void configure() {
from("direct:somexml")
.split(xpath("/items/item"))
.to("jms:item")
.end()
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
System.out.println("Completed:" + System.lineSeparator() + toStr(exchange));
}
})
.end();
from("jms:item?maxConcurrentConsumers=10&exchangePattern=InOut&replyTo=replyqueue")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
sleep();
System.out.println("Item received:" + System.lineSeparator() + toStr(exchange));
}
});
}
});
ProducerTemplate producerTemplate = context.createProducerTemplate();
context.start();
producerTemplate.sendBody("direct:somexml",
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<items>\n" +
" <item>item1</item>\n" +
" <item>item2</item>\n" +
" <item>item3</item>\n" +
"</items>\n");
producerTemplate.sendBody("direct:somexml",
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<items>\n" +
" <item>item4</item>\n" +
" <item>item5</item>\n" +
" <item>item6</item>\n" +
"</items>\n");
Thread.sleep(10000);
context.stop();
}
private static String toStr(Exchange exchange) {
if (exchange != null) {
return "In:"+System.lineSeparator()+toStr(exchange.getIn())+"Out:"+System.lineSeparator()+toStr(exchange.getOut());
} else {
return "null";
}
}
private static String toStr(Message message) {
if (message != null) {
return message.getHeaders() + System.lineSeparator() + message.getBody()+System.lineSeparator();
} else {
return "null";
}
}
public static void sleep() throws InterruptedException {
Thread.sleep(new Random().nextInt(1000));
}
}