Since we're on a major migration process of this website, some component documents here are out of sync right now. In the meantime you may want to look at the early version of the new website
https://camel.apache.org/staging/
We would very much like to receive any feedback on the new site, please join the discussion on the Camel user mailing list.
Aggregate ExampleAvailable as of Camel 2.3 AboutThis example demonstrates the new overhauled Aggregator EIP in Apache Camel 2.3. The example is focused on the persistence support, which means the aggregated messages is stored in a persistent file storage using the new HawtDB component. How to runYou simply run it using the following maven goal: How it worksThe example is an interactive example where it prompt on the console for you to enter a number and press ENTER. The numbers you enter will then be aggregated and persisted. That means you can at any time hit When you want to complete the aggregation you can enter The persistent datastore is located in the ExampleFor example we start the example for the first time using [pache.camel.spring.Main.main()] DefaultCamelContext INFO Apache Camel 2.3-SNAPSHOT (CamelContext:camel) started Enter a number to be added (use STOP to end, and ctrl c to shutdown Camel): 5 Enter a number to be added (use STOP to end, and ctrl c to shutdown Camel): 7 Enter a number to be added (use STOP to end, and ctrl c to shutdown Camel): ^C[ Thread-2] Main$HangupInterceptor INFO Received hang up - stopping the main instance. [ Thread-2] MainSupport INFO Apache Camel 2.3-SNAPSHOT stopping Now we start the example again using [pache.camel.spring.Main.main()] DefaultCamelContext INFO Apache Camel 2.3-SNAPSHOT (CamelContext:camel) started Enter a number to be added (use STOP to end, and ctrl c to shutdown Camel): 3 Enter a number to be added (use STOP to end, and ctrl c to shutdown Camel): STOP The result is: 15 Enter a number to be added (use STOP to end, and ctrl c to shutdown Camel): Using AggregatorThe example is configured as follows in Spring XML. Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> <!-- This is our aggregation strategy for the numbers received as input --> <bean id="myStrategy" class="org.apache.camel.example.NumberAggregationStrategy"/> <!-- This is the repository to store aggregated messages --> <bean id="myRepo" class="org.apache.camel.processor.aggregate.MemoryAggregationRepository"/> <!-- This is the Camel route which asks for input and aggregates incoming numbers --> <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> <route> <!-- ask user to enter a number --> <from uri="stream:in?promptMessage=Enter a number to be added (enter STOP to end, and Ctrl-C to shutdown Camel): &promptDelay=1000"/> <!-- aggregate the input, use eagerCheckCompletion to let the completionPredicate easily detect the STOP command --> <aggregate strategyRef="myStrategy" aggregationRepositoryRef="myRepo" eagerCheckCompletion="true"> <!-- aggregate all messages into the same group --> <correlationExpression> <constant>true</constant> </correlationExpression> <!-- if end user enters STOP then complete the aggregation --> <completionPredicate> <simple>${body} contains 'STOP'</simple> </completionPredicate> <!-- and transform the completed message to a human readable --> <transform> <simple>The result is: ${body}</simple> </transform> <!-- which is printed on the console --> <to uri="stream:out"/> </aggregate> </route> </camelContext> </beans> And it has a public class NumberAggregationStrategy implements AggregationStrategy { @Override public Exchange aggregate(Exchange oldExchange, Exchange newExchange) { if (oldExchange == null) { return newExchange; } // check for stop command String input = newExchange.getIn().getBody(String.class); if ("STOP".equalsIgnoreCase(input)) { return oldExchange; } Integer num1 = oldExchange.getIn().getBody(Integer.class); Integer num2 = newExchange.getIn().getBody(Integer.class); // just avoid bad inputs by assuming its a 0 value Integer num3 = (num1 != null ? num1 : 0) + (num2 != null ? num2 : 0); oldExchange.getIn().setBody(num3); return oldExchange; } } See Also |