apache camel 的 split 和 aggregate

来源:互联网 发布:算法和并行计算 编辑:程序博客网 时间:2024/05/05 20:39

split和aggregate,看图就明白了。

下面我用一个例子来说明,非常难得,你很难在网上找到apache camel这样的例子。

1、路由

1from("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");
2、测试bean

01public 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}
3、聚合类

01public 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}
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....

我的例子里面从一个jms消息开始,然后经历一系列的路由处理,最终将任务完成。问题是,我们希望在任务结束的时候,再发一个jms来通知任务发起者,“我已经完成啦。”,但上面的输出显然有问题,因为有好多个end....。当然,根据我提供的代码,你肯定发现多种方法来判断任务的结束。你可以试试看。

原创粉丝点击