我想将聚合的结果作为路由的主要消息,而不是 IN 上的原始消息。我也想在一条路线上做到这一点。
我知道我可以.to("direct:one_result")
在聚合之后使用,但是我在一个路由中这样做有很大的限制,因为我正在动态生成路由。
我的.to("mock:out")
将被更长的路线定义所取代。
from("direct:in").routeId("TEST_AGGREGATION_ROUTE")
.log("<IN> ${body}")
.aggregate(header("THE_ID"), (oldExchange, newExchange) -> {
final List<Object> body;
final Exchange outExchange;
if (oldExchange == null) {
outExchange = newExchange;
body = new ArrayList<>();
body.add(newExchange.getIn().getBody());
} else {
outExchange = oldExchange;
body = oldExchange.getIn().getBody(List.class);
body.add(newExchange.getIn().getBody());
}
outExchange.getIn().setBody(body);
return outExchange;
})
.completionSize(4)
.completionTimeout(30000)
.log("<AGGREGATION> size = ${body.size}") // HERE I GET THE AGGREGATION RESULT
.end()
.log("<OUT> ${body}") // HERE I GET THE INPUT MESSAGES
.to("mock:out")
;
测试输出如下所示:
TEST_AGGREGATION_ROUTE - <IN> BODY1
TEST_AGGREGATION_ROUTE - <OUT> BODY1
TEST_AGGREGATION_ROUTE - <IN> BODY2
TEST_AGGREGATION_ROUTE - <OUT> BODY2
TEST_AGGREGATION_ROUTE - <IN> BODY3
TEST_AGGREGATION_ROUTE - <OUT> BODY3
TEST_AGGREGATION_ROUTE - <IN> BODY4
TEST_AGGREGATION_ROUTE - <AGGREGATION> size = 4
TEST_AGGREGATION_ROUTE - <OUT> BODY4