apache camel 的 split 和 aggregate

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....

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

转载于:https://my.oschina.net/jianglibo/blog/17627

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值