我有一条路由收集标头,然后将请求发送到另一个队列,我从那里等待一个答案(我只需要响应主体)。问题是我想在一条路由中的一条消息中同时收到标头和答案,但是现在我在一条路由中得到了两条消息(我需要汇总响应正文和标头)。 怎么做?
from("jms:queue:aaa")
.log("incoming message")
.process(exchange -> {
exchange.getIn().setHeader("c", c);
exchange.getIn().setHeader("d", d);
exchange.getIn().setHeader("a", a);
exchange.getIn().setHeader("b", b);
exchange.getIn().setBody("2+3");
})
.removeHeaders("*", "a", "b", "c", "d")
.setHeader("JMSReplyTo", simple("bbb"))
//send request
.to(ExchangePattern.InOnly, "jms:ccc?preserveMessageQos=true&includeSentJMSMessageID=true")
//trying to send headers from this route to bbb
.to(ExchangePattern.InOut, "jms:bbb")
.end();
from("jms:bbb")
.log("${headers}\n${}body")
.end();
from(“ jms:bbb”)
这是我要合并来自。
的结果的路线
.to(ExchangePattern.InOnly, "jms:ccc?preserveMessageQos=true& includeSentJMSMessageID=true")
。
和。
.to (ExchangePattern.InOut, "jms: bbb")
UPD:
public class AggregationStrategyImpl implements AggregationStrategy {
private static final Logger log = LoggerFactory.getLogger(AggregationStrategyImpl.class);
@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
if(newExchange == null)
newExchange = oldExchange;
String a = oldExchange.getIn().getHeader("a", String.class);
String b = oldExchange.getIn().getHeader("b", String.class);
String c = oldExchange.getIn().getHeader("c", String.class);
String d = oldExchange.getIn().getHeader("d", String.class);
newExchange.getIn().setHeader("a", a);
newExchange.getIn().setHeader("b", b);
newExchange.getIn().setHeader("c", c);
newExchange.getIn().setHeader("d", d);
return newExchange;
}
}
.setHeader("JMSReplyTo", simple("bbb"))
.multicast(aggregationStrategy)
.to(ExchangePattern.InOnly, "jms:ccc?preserveMessageQos=true&includeSentJMSMessageID=true")
.to(ExchangePattern.OutOnly, "jms:bbb")
答案 0 :(得分:0)
.log("incoming message")
.process(exchange -> {
exchange.getIn().setHeader("c", c);
exchange.getIn().setHeader("d", d);
exchange.getIn().setHeader("a", a);
exchange.getIn().setHeader("b", b);
exchange.getIn().setBody("2+3");
})
.removeHeaders("*", "a", "b", "c", "d")
.setHeader("JMSReplyTo", simple("bbb"))
//send request
.multicast(aggregateStratergeyBean)
.to(ExchangePattern.InOnly, "jms:ccc?preserveMessageQos=true&includeSentJMSMessageID=true")
//trying to send headers from this route to bbb
.to(ExchangePattern.InOut, "jms:bbb")
.end();
from("jms:bbb")
.log("${headers}\n${}body")
.end();```
aggregateStratergeyBean should implements AggregationStrategy.
Also you can use many options like below
.parallelProcessing(true) // Parallel process both routes
.parallelAggregate() // Aggregate parallel , you need to handle thread safety
.streaming(). // Process response as and when it get the response not in the order it invoked.
答案 1 :(得分:0)
请参见here。
您的第一个if
语句似乎无效:
if(newExchange == null)
newExchange = oldExchange;
这样,新传入的交换对象将始终保持为空。
相反,应该是另一种方式,因为当oldExchange
进入时,您想保留newExchange
:
if (oldExchange == null) {
//...
return newExchange;
} else {
//...
return oldExchange;
}
问题似乎是您在等待newExchange
为空,但是如果调用了聚合,则总是是newExchange
。即将出现的第一个交换对象也是newExchange
。相反,聚合的第一个调用没有oldExchange
!