split和aggregate,看图就明白了。
下面我用一个例子来说明,非常难得,你很难在网上找到apache camel这样的例子。
1、路由
1 | from( "jms:TOOL.TTT" ).bean(TttBean. class , "start" ).split(body(List. class ), new MyAggregationStrategy()) |
2 | .bean(TttBean. class , "processOneLi" ).end().split(body(List. class )).bean(TttBean. class , "processOne" ).bean(TttBean. class , "end" ); |
01 | public class TttBean { |
02 | |
03 | public void start(Exchange exchange){ |
04 | List<List<Integer>> ii = new ArrayList<List<Integer>>(); |
05 | for ( int i = 0 ;i< 2 ;i++){ |
06 | List<Integer> li = new ArrayList<Integer>(); |
07 | for ( int j = 10 ;j < 15 ; j++){ |
08 | li.add(j); |
09 | } |
10 | ii.add(li); |
11 | } |
12 | exchange.getIn().setBody(ii); |
13 | System.out.println( "start...." ); |
14 | } |
15 | |
16 | public void processOneLi(Exchange exchange){ |
17 | |
18 | System.out.println( "CamelSplitIndex:" + exchange.getProperty( "CamelSplitIndex" , int . class )); |
19 | System.out.println( "CamelSplitSize:" + exchange.getProperty( "CamelSplitSize" , int . class )); |
20 | System.out.println( "CamelSplitComplete:" + exchange.getProperty( "CamelSplitComplete" , boolean . class )); |
21 | List<Integer> li = exchange.getIn().getBody(List. class ); |
22 | System.out.println( "oneLi" ); |
23 | } |
24 | |
25 | public void processOne(Exchange exchange){ |
26 | System.out.println( "CamelSplitIndex:" + exchange.getProperty( "CamelSplitIndex" , int . class )); |
27 | System.out.println( "CamelSplitSize:" + exchange.getProperty( "CamelSplitSize" , int . class )); |
28 | System.out.println( "CamelSplitComplete:" + exchange.getProperty( "CamelSplitComplete" , boolean . class )); |
29 |
30 | int i = exchange.getIn().getBody( int . class ); |
31 | System.out.println(i); |
32 | } |
33 | |
34 | |
35 | public void end(Exchange exchange){ |
36 | System.out.println( "ending...." ); |
37 | } |
38 | } |
01 | public class MyAggregationStrategy implements AggregationStrategy{ |
02 |
03 | @Override |
04 | public Exchange aggregate(Exchange oldExchange, Exchange newExchange) { |
05 | |
06 |
07 | if (oldExchange == null ) { |
08 | // the first time we aggregate we only have the new exchange, |
09 | // so we just return it |
10 | return newExchange; |
11 |
12 | } |
13 | |
14 | List<Integer> lio = oldExchange.getIn().getBody(List. class ); |
15 | List<Integer> lin = newExchange.getIn().getBody(List. class ); |
16 | |
17 | for (Integer i : lin){ |
18 | lio.add(i); |
19 | } |
20 | |
21 | oldExchange.getIn().setBody(lio); |
22 | return oldExchange; |
23 | } |
24 |
25 | } |
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....