apache camel 的 split 和 aggregate
2019獨角獸企業重金招聘Python工程師標準>>>
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{@Overridepublic Exchange aggregate(Exchange oldExchange, Exchange newExchange) {if (oldExchange == null) {// the first time we aggregate we only have the new exchange,// so we just return itreturn 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....
我的例子里面從一個jms消息開始,然后經歷一系列的路由處理,最終將任務完成。問題是,我們希望在任務結束的時候,再發一個jms來通知任務發起者,“我已經完成啦。”,但上面的輸出顯然有問題,因為有好多個end....。當然,根據我提供的代碼,你肯定發現多種方法來判斷任務的結束。你可以試試看。轉載于:https://my.oschina.net/jianglibo/blog/17627
總結
以上是生活随笔為你收集整理的apache camel 的 split 和 aggregate的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 买房要交什么费用?(买房需要交纳哪些费用
- 下一篇: 实现做出html的上标以及下标