split和aggregate,看图就明白了。
下面我用一个例子来说明,非常难得,你很难在网上找到apache camel这样的例子。
1、路由
from("jms:TOOL.TTT").bean(TttBean.class, "start").split(body(List.class),new MyAggregationStrategy())
.bean(TttBean.class, "processOneLi").end().split(body(List.class)).bean(TttBean.class, "processOne").bean(TttBean.class, "end");
2、测试bean
public class TttBean {
public void start(Exchange exchange){
List<List<Integer>> ii = new ArrayList<List<Integer>>();
for(int i =0;i<2;i++){
List<Integer> li = new ArrayList<Integer>();
for(int j = 10;j < 15; j++){
li.add(j);
}
ii.add(li);
}
exchange.getIn().setBody(ii);
System.out.println("start....");
}
public void processOneLi(Exchange exchange){
System.out.println("CamelSplitIndex:" + exchange.getProperty("CamelSplitIndex",int.class));
System.out.println("CamelSplitSize:" + exchange.getProperty("CamelSplitSize",int.class));
System.out.println("CamelSplitComplete:" + exchange.getProperty("CamelSplitComplete",boolean.class));
List<Integer> li = exchange.getIn().getBody(List.class);
System.out.println("oneLi");
}
public void processOne(Exchange exchange){
System.out.println("CamelSplitIndex:" + exchange.getProperty("CamelSplitIndex",int.class));
System.out.println("CamelSplitSize:" + exchange.getProperty("CamelSplitSize",int.class));
System.out.println("CamelSplitComplete:" + exchange.getProperty("CamelSplitComplete",boolean.class));
int i = exchange.getIn().getBody(int.class);
System.out.println(i);
}
public void end(Exchange exchange){
System.out.println("ending....");
}
}
3、聚合类
public class MyAggregationStrategy implements AggregationStrategy{
@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
if (oldExchange == null) {
// the first time we aggregate we only have the new exchange,
// so we just return it
return newExchange;
}
List<Integer> lio = oldExchange.getIn().getBody(List.class);
List<Integer> lin = newExchange.getIn().getBody(List.class);
for(Integer i : lin){
lio.add(i);
}
oldExchange.getIn().setBody(lio);
return oldExchange;
}
}
4、输出:
start....
CamelSplitIndex:0
CamelSplitSize:2
CamelSplitComplete:false
oneLi
CamelSplitIndex:1
CamelSplitSize:2
CamelSplitComplete:true
oneLi
CamelSplitIndex:0
CamelSplitSize:10
CamelSplitComplete:false
10
ending....
CamelSplitIndex:1
CamelSplitSize:10
CamelSplitComplete:false
11
ending....
CamelSplitIndex:2
CamelSplitSize:10
CamelSplitComplete:false
12
ending....
CamelSplitIndex:3
CamelSplitSize:10
CamelSplitComplete:false
13
ending....
CamelSplitIndex:4
CamelSplitSize:10
CamelSplitComplete:false
14
ending....
CamelSplitIndex:5
CamelSplitSize:10
CamelSplitComplete:false
10
ending....
CamelSplitIndex:6
CamelSplitSize:10
CamelSplitComplete:false
11
ending....
CamelSplitIndex:7
CamelSplitSize:10
CamelSplitComplete:false
12
ending....
CamelSplitIndex:8
CamelSplitSize:10
CamelSplitComplete:false
13
ending....
CamelSplitIndex:9
CamelSplitSize:10
CamelSplitComplete:true
14
ending....