camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From huntc <hu...@mac.com>
Subject RE: How to use Aggregator and AggregationCollection?
Date Tue, 07 Oct 2008 11:34:30 GMT

Thanks to the help of Claus, I have discovered that I can use my own
aggregation strategy to collect all files into an Exchange containing a
collection of Exchanges. Here is my route building:


public class RouteBuilder extends org.apache.camel.builder.RouteBuilder {
	public void configure() {
		from(
			"file://target/classes/timetables?noop=true&consumer.recursive=true")
			.aggregator(new CollectAllAggregationStrategy()).constant(true)
			.process(new Processor() {
				public void process(Exchange e) {
					System.out.println("Received exchange: "
							+ e.getIn().getBody());
				}
			});
	}
}


...and here is my aggregation strategy:


(CollectAllAggregationStrategy.java)

public class CollectAllAggregationStrategy implements AggregationStrategy {

	@SuppressWarnings("unchecked")
	public Exchange aggregate(Exchange arg0, Exchange arg1) {
		// If our previous exchange holds a collection of Exchanges then we
		// simply add to that collection. Otherwise we create a new Exchange
		// containing a collection of exchanges and use this for future
		// aggregations (and of course add to it presently).
		Collection collection;
		Object body = arg0.getIn().getBody();
		if (body instanceof Collection) {
			collection = (Collection) body;
		} else {
			collection = new Vector();
			collection.add(arg0);

			Exchange newExchange = new DefaultExchange(arg0.getContext());
			Message in = new DefaultMessage();
			in.setBody(collection);
			newExchange.setIn(in);

			arg0 = newExchange;
		}
		collection.add(arg1);
		return arg0;
	}
}


All exchanges appear to be bundled together given the use of the constant
expression.

I hope that others find this use of aggregation useful.

Kind regards,
Christopher
-- 
View this message in context: http://www.nabble.com/How-to-use-Aggregator-and-AggregationCollection--tp19739918s22882p19856197.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message