骆驼分割/聚集并合并列表< A>包含List< B>包含List< C> [英] camel split/aggregate and merge List<A> that contains List<B> that contains List<C>
问题描述
给出与该结构相似的结构:
Given a structure similar to this one:
@Data
public class A {
//..other fields..
private List<B> bs;
}
@Data
public class B {
//..other fields..
private List<C> cs;
}
我必须在多个步骤/路线中处理类型A的列表.一些操作在A级,其他一些在C级.
I have to process a list of type A in multiple steps/routes. Some operation are in the A level, others in other levels like C.
我要解决的问题是处理给定A的每个C,然后某些逻辑必须与更新的A模型一起使用.
The problem I'm trying to solve is to process every single C given an A and then some logic afterwards have to work with the updated A model.
我可以成功地拆分并聚合回list<C>
,但是现在我在尝试根据给定B和C的输出来重建A的过程中遇到困难.
I can successfully split and aggregate back the list<C>
but now i'm stuck trying to rebuild A given the output for B and C.
这是我目前所拥有的:
from("direct:my-A-Item")
.id("direct-a")
.autoStartup(true)
.split(ExpressionBuilder.beanExpression(new CSplitter(), "getBs"), new MyAggregationStrategy())
.streaming()
.split(ExpressionBuilder.beanExpression(new CSplitter(), "getCs"), new MyAggregationStrategy())
.streaming()
.bean(processor, "doStuff")//Working on a since C instance
.end()
.bean(processor, "test") //Here I get the worked List<C>
.end()
//.bean(processor, "thisProcessorNeedsA").end(); //TODO get the original A and the output List<C> so i can make further work on them
如何使用新的C列表更新B实例,然后执行相同操作以更新A?
How can I update the B instances with the new list of C and then do the same to update A?
public class CSplitter {
public List<B> getBs(A a) {
return a.getBs();
}
public List<C> getCs(B b) {
return b.getCs();
}
}
public class MyAggregationStrategy implements AggregationStrategy {
@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
Object newBody = newExchange.getIn().getBody();
ArrayList<Object> list = null;
if (oldExchange == null) {
list = new ArrayList<Object>();
list.add(newBody);
newExchange.getIn().setBody(list);
return newExchange;
} else {
list = oldExchange.getIn().getBody(ArrayList.class);
list.add(newBody);
return oldExchange;
}
}
}
在查看文档和在线资源后,我找不到任何示例,也可以汇总上一步的内容...欢迎任何提示:)
Checking the documentations and online resources i could not find any example aggregating having also the body from a previous step... any tips is welcome :)
推荐答案
我将在拆分之前将主体A捕获为交换属性,然后将其提供.
I would capture the body A as an exchange property before splitting, then it will be available later.
from("direct:my-A-Item")
.id("direct-a")
.autoStartup(true)
.setProperty("originalA", body())
.split
// etc.
.end()
.bean(processor, "myMethod(${property.originalA}, ${body})").end();
这篇关于骆驼分割/聚集并合并列表< A>包含List< B>包含List< C>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!