为 Camel 路由中未知数量的拆分元素设置完成大小或谓词



我有一个骆驼路由,它使用一个http服务,它返回一个json,其中包含我需要通过Id关联的几个元素。我不知道响应中有多少具有相同 Id 的元素,那么,如何在聚合中设置完成以关联所有这些元素?

这些是我的路线:

from("direct:getInfo")
.id("getInfo")
.setHeader("accept", constant("application/json"))
.setHeader("authorization", constant("xyz"))
.setHeader("Cache-Control", constant("no-cache"))
.setHeader("content-Type", constant("application/json"))
.setHeader(Exchange.HTTP_METHOD, constant("GET"))
.removeHeader(Exchange.HTTP_PATH)
.removeHeader("CamelHttp*")
.setBody(simple("${null}"))
.streamCaching()
.to("http4:someURL") //responses an array of n json elements
.split().jsonpath("$").streaming()
.to("direct:splitInfo");

from("direct:splitInfo")
.id("splitInfo")
.aggregate(jsonpath("CustomerId"), new ArrayListAggregationStrategy())
.completionSize(???) //How must I set the completion in order to correlate all items
.to("direct:process");

非常感谢。

由于注释中的示例,完全重写了答案

由于您希望拆分和重新聚合完整的 JSON 有效负载,因此您只需要具有聚合策略的拆分器 EIP。

如果为拆分器提供用于拆分有效负载的表达式以及聚合策略,则根本不需要完成条件。每个 JSON 有效负载都作为"批处理"进行处理。

.from(endpoint)
// body = complete JSON payload
.split([split-expression], new MyAggregationStrategy())
// each element is sent to this bean 
.to("bean:elementProcessorBean")
// you must end the splitter 
.end()  
// here you get the complete re-aggregated JSON payload
// how it is re-aggregated is up to MyAggregationStrategy 

有关示例,请查看链接的拆分器文档。

最新更新