gpt4 book ai didi

java - Camel 聚合策略结果错误

转载 作者:行者123 更新时间:2023-12-01 12:30:29 28 4
gpt4 key购买 nike

我使用 CAMEL 聚合来自两个源的响应,一个是 xml,另一个是 json。最初我已经 stub 了这些回复,并从文件中获取它们。目标是汇总两个来源的响应。

我的聚合器路线如下所示

    from("direct:fetchProfile")
.multicast(new ProfileAggregationStrategy()).stopOnException()
.enrich("direct:xmlProfile")
.enrich("direct:jsonProfile")
.end();

我的“direct:xmlProfile”路线看起来像 -

    from("direct:xmlProfile")
.process(new Processor(){

@Override
public void process(Exchange exchange) throws Exception {
String filename = "target/classes/xml/customerDetails.xml";
InputStream fileStream = new FileInputStream(filename);
exchange.getIn().setBody(fileStream);
}
})
.split(body().tokenizeXML("attributes", null))
.unmarshal(jaxbDataFormat)
.process(new Processor(){
@Override
public void process(Exchange exchange) throws Exception {
Profile legacyProfile = exchange.getIn().getBody(Profile.class);
// some more processing here
exchange.getIn().setBody(legacyProfile);
}

});

在上面的 route ,我从文件中读取 xml,然后使用 jaxb 转换器将感兴趣的元素映射到“Profile”表示的类中。调用该路由后,CAMEL 调用 ProfileAggregationStrategy。其代码是 -

    public class ProfileAggregationStrategy implements AggregationStrategy{
@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
// this is the problematic line
Profile newProfile = newExchange.getIn().getBody(Profile.class);
if (oldExchange == null){
return newExchange;
} else {
Profile oldProfile = oldExchange.getIn().getBody(Profile.class);
// code to copy merge oldProfile with newProfile
return oldExchange;
}

}
}

问题在于标记为“有问题的线路”的线路。即使在路由“direct:xmlProfile”的最后阶段,我已显式地将一个对象放入交换的 Body 中,ProfileAggregationStrategy 中的 newExchange 仍然显示 Body 的类型为 iostream。

最佳答案

阅读有关分离器 eip 的文档。

由于拆分器的输出是输入,除非您为拆分器指定聚合策略,您可以在其中决定输出应该是什么。例如,如果您想使用最后一个分割的元素,则可以使用 UseLastestAggregrationStrategy

关于java - Camel 聚合策略结果错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25954729/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com