<html>
<head>
<base href="http://cwiki.apache.org/confluence">
<link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAMEL&forWysiwyg=true"
type="text/css">
</head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
<h2><a href="http://cwiki.apache.org/confluence/display/CAMEL/Aggregate+Example">Aggregate
Example</a></h2>
<h4>Page <b>edited</b> by <a href="http://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
</h4>
<br/>
<div class="notificationGreySide">
<h2><a name="AggregateExample-AggregateExample"></a>Aggregate Example</h2>
<p><b>Available as of Camel 2.3</b></p>
<h3><a name="AggregateExample-About"></a>About</h3>
<p>This example demonstrates the new overhauled <a href="/confluence/display/CAMEL/Aggregator2"
title="Aggregator2">Aggregator</a> 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 <a href="/confluence/display/CAMEL/HawtDB" title="HawtDB">HawtDB</a>
component.</p>
<h3><a name="AggregateExample-Howtorun"></a>How to run</h3>
<p>You simply run it using the following maven goal: <tt>mvn camel:run</tt></p>
<h3><a name="AggregateExample-Howitworks"></a>How it works</h3>
<p>The 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 <tt>ctrl + c</tt> to shutdown Camel. Then you should
be able to start the example again and resume where you left.</p>
<p>When you want to <em>complete</em> the aggregation you can enter <tt>STOP</tt>
as input and Camel will show you the result, which is the sum of all the numbers entered.</p>
<p>The persistent datastore is located in the <tt>data/hawtdb.dat</tt> file.
Its automatic created the first time.</p>
<h3><a name="AggregateExample-Example"></a>Example</h3>
<p>For example we start the example for the first time using <tt>mvn camel:run</tt>
and then we enter the two numbers 5 and 7 before we shutdown using <tt>ctrl + c</tt>.
The relevant console output:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
[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[
<span class="code-object">Thread</span>-2] Main$HangupInterceptor
INFO Received hang up - stopping the main instance.
[ <span class="code-object">Thread</span>-2] MainSupport
INFO Apache Camel 2.3-SNAPSHOT stopping
</pre>
</div></div>
<p>Now we start the example again using <tt>mvn camel:run</tt> and enter
the number 3 and then enter <tt>STOP</tt> to see the result. As expected the result
is 5+7+3 = 15 as outputted on the console. As you can see the persistence of the aggregated
messages ensures we could continue where we stopped.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
[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):
</pre>
</div></div>
<h3><a name="AggregateExample-UsingAggregator"></a>Using Aggregator</h3>
<p>The example is configured as follows in Spring XML.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><beans xmlns=<span class="code-quote">"http://www.springframework.org/schema/beans"</span>
<span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
<span class="code-keyword">xmlns:camel</span>=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<span class="code-tag"><span class="code-comment"><!-- this is our
aggregation strategy which can aggregate the numbers received as input --></span></span>
<span class="code-tag"><bean id=<span class="code-quote">"myStrategy"</span>
class=<span class="code-quote">"org.apache.camel.example.NumberAggregationStrategy"</span>/></span>
<span class="code-tag"><span class="code-comment"><!-- this is the
persistent repository to store aggregated messages --></span></span>
<span class="code-tag"><bean id=<span class="code-quote">"myRepo"</span>
class=<span class="code-quote">"org.apache.camel.component.hawtdb.HawtDBAggregationRepository"</span>></span>
<span class="code-tag"><span class="code-comment"><!-- use data/hawtdb.dat
as the persistent store --></span></span>
<span class="code-tag"><property name=<span class="code-quote">"persistentFileName"</span>
value=<span class="code-quote">"data/hawtdb.dat"</span>/></span>
<!-- the repo must have an unique name,
as you can have multiple repositories in the same file -->
<span class="code-tag"><property name=<span class="code-quote">"repositoryName"</span>
value=<span class="code-quote">"myCoolRepo"</span>/></span>
<span class="code-tag"></bean></span>
<span class="code-tag"><span class="code-comment"><!-- this is the
camel route which asks for input and aggregates the number --></span></span>
<span class="code-tag"><camelContext id=<span class="code-quote">"camel"</span>
xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>></span>
<span class="code-tag"><route></span>
<span class="code-tag"><span class="code-comment"><!-- ask
user to enter a number --></span></span>
<span class="code-tag"><from uri=<span class="code-quote">"stream:in?promptMessage=Enter
a number to be added (use STOP to end, and ctrl+c to shutdown Camel): &amp;promptDelay=1000"</span>/></span>
<span class="code-tag"><span class="code-comment"><!-- aggregate
the input, use eagerCheckCompletion to let the completionPredicate easily detect the STOP
command --></span></span>
<span class="code-tag"><aggregate strategyRef=<span class="code-quote">"myStrategy"</span>
aggregationRepositoryRef=<span class="code-quote">"myRepo"</span> eagerCheckCompletion=<span
class="code-quote">"true"</span>></span>
<span class="code-tag"><span class="code-comment"><!--
aggregate all messages into the same group --></span></span>
<span class="code-tag"><correlationExpression></span><span
class="code-tag"><constant></span>true<span class="code-tag"></constant></span><span
class="code-tag"></correlationExpression></span>
<span class="code-tag"><span class="code-comment"><!--
if end user enters STOP then complete the aggregation --></span></span>
<span class="code-tag"><completionPredicate></span><span
class="code-tag"><simple></span>${body} contains 'STOP'<span class="code-tag"></simple></span><span
class="code-tag"></completionPredicate></span>
<span class="code-tag"><span class="code-comment"><!--
and transform the completed message to a human readble --></span></span>
<span class="code-tag"><transform></span><span
class="code-tag"><simple></span>The result is: ${body}<span class="code-tag"></simple></span><span
class="code-tag"></transform></span>
<span class="code-tag"><span class="code-comment"><!--
which is printed on the console --></span></span>
<span class="code-tag"><to uri=<span class="code-quote">"stream:out"</span>/></span>
<span class="code-tag"></aggregate></span>
<span class="code-tag"></route></span>
<span class="code-tag"></camelContext></span>
<span class="code-tag"></beans></span>
</pre>
</div></div>
<p>And it has a <tt>AggregationStrategy</tt> to sum the numbers which is
done in Java code as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class NumberAggregationStrategy
<span class="code-keyword">implements</span> AggregationStrategy {
<span class="code-keyword">public</span> Exchange aggregate(Exchange oldExchange,
Exchange newExchange) {
<span class="code-keyword">if</span> (oldExchange == <span class="code-keyword">null</span>)
{
<span class="code-keyword">return</span> newExchange;
}
<span class="code-object">Integer</span> num1 = oldExchange.getIn().getBody(<span
class="code-object">Integer</span>.class);
<span class="code-object">Integer</span> num2 = newExchange.getIn().getBody(<span
class="code-object">Integer</span>.class);
<span class="code-comment">// just avoid bad inputs by assuming its a 0 value
</span> <span class="code-object">Integer</span> num3 = (num1 !=
<span class="code-keyword">null</span> ? num1 : 0) + (num2 != <span class="code-keyword">null</span>
? num2 : 0);
oldExchange.getIn().setBody(num3);
<span class="code-keyword">return</span> oldExchange;
}
}
</pre>
</div></div>
<h3><a name="AggregateExample-SeeAlso"></a>See Also</h3>
<ul class="alternate" type="square">
<li><a href="/confluence/display/CAMEL/Examples" title="Examples">Examples</a></li>
<li><a href="/confluence/display/CAMEL/Aggregator2" title="Aggregator2">Aggregator</a></li>
<li><a href="/confluence/display/CAMEL/HawtDB" title="HawtDB">HawtDB</a></li>
</ul>
</div>
<div id="commentsSection" class="wiki-content pageSection">
<div style="float: right;">
<a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
</div>
<a href="http://cwiki.apache.org/confluence/display/CAMEL/Aggregate+Example">View
Online</a>
|
<a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=14811499&revisedVersion=4&originalVersion=3">View
Change</a>
|
<a href="http://cwiki.apache.org/confluence/display/CAMEL/Aggregate+Example?showComments=true&showCommentArea=true#addcomment">Add
Comment</a>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
|