camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Aggregate Example
Date Tue, 02 Mar 2010 15:31:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;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-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">&lt;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"&gt;

    <span class="code-tag"><span class="code-comment">&lt;!-- this is our
aggregation strategy which can aggregate the numbers received as input --&gt;</span></span>
    <span class="code-tag">&lt;bean id=<span class="code-quote">"myStrategy"</span>
class=<span class="code-quote">"org.apache.camel.example.NumberAggregationStrategy"</span>/&gt;</span>

    <span class="code-tag"><span class="code-comment">&lt;!-- this is the
persistent repository to store aggregated messages --&gt;</span></span>
    <span class="code-tag">&lt;bean id=<span class="code-quote">"myRepo"</span>
class=<span class="code-quote">"org.apache.camel.component.hawtdb.HawtDBAggregationRepository"</span>&gt;</span>
        <span class="code-tag"><span class="code-comment">&lt;!-- use data/hawtdb.dat
as the persistent store --&gt;</span></span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"persistentFileName"</span>
value=<span class="code-quote">"data/hawtdb.dat"</span>/&gt;</span>
        &lt;!-- the repo must have an unique name,
             as you can have multiple repositories in the same file --&gt;
        <span class="code-tag">&lt;property name=<span class="code-quote">"repositoryName"</span>
value=<span class="code-quote">"myCoolRepo"</span>/&gt;</span>
    <span class="code-tag">&lt;/bean&gt;</span>

    <span class="code-tag"><span class="code-comment">&lt;!-- this is the
camel route which asks for input and aggregates the number --&gt;</span></span>
    <span class="code-tag">&lt;camelContext id=<span class="code-quote">"camel"</span>
xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
        <span class="code-tag">&lt;route&gt;</span>
            <span class="code-tag"><span class="code-comment">&lt;!-- ask
user to enter a number --&gt;</span></span>
            <span class="code-tag">&lt;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;amp;promptDelay=1000"</span>/&gt;</span>
            <span class="code-tag"><span class="code-comment">&lt;!-- aggregate
the input, use eagerCheckCompletion to let the completionPredicate easily detect the STOP
command --&gt;</span></span>
            <span class="code-tag">&lt;aggregate strategyRef=<span class="code-quote">"myStrategy"</span>
aggregationRepositoryRef=<span class="code-quote">"myRepo"</span> eagerCheckCompletion=<span
class="code-quote">"true"</span>&gt;</span>
                <span class="code-tag"><span class="code-comment">&lt;!--
aggregate all messages into the same group --&gt;</span></span>
                <span class="code-tag">&lt;correlationExpression&gt;</span><span
class="code-tag">&lt;constant&gt;</span>true<span class="code-tag">&lt;/constant&gt;</span><span
class="code-tag">&lt;/correlationExpression&gt;</span>
                <span class="code-tag"><span class="code-comment">&lt;!--
if end user enters STOP then complete the aggregation --&gt;</span></span>
                <span class="code-tag">&lt;completionPredicate&gt;</span><span
class="code-tag">&lt;simple&gt;</span>${body} contains 'STOP'<span class="code-tag">&lt;/simple&gt;</span><span
class="code-tag">&lt;/completionPredicate&gt;</span>
                <span class="code-tag"><span class="code-comment">&lt;!--
and transform the completed message to a human readble --&gt;</span></span>
                <span class="code-tag">&lt;transform&gt;</span><span
class="code-tag">&lt;simple&gt;</span>The result is: ${body}<span class="code-tag">&lt;/simple&gt;</span><span
class="code-tag">&lt;/transform&gt;</span>
                <span class="code-tag"><span class="code-comment">&lt;!--
which is printed on the console --&gt;</span></span>
                <span class="code-tag">&lt;to uri=<span class="code-quote">"stream:out"</span>/&gt;</span>
            <span class="code-tag">&lt;/aggregate&gt;</span>
        <span class="code-tag">&lt;/route&gt;</span>

    <span class="code-tag">&lt;/camelContext&gt;</span>

<span class="code-tag">&lt;/beans&gt;</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=3&originalVersion=2">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CAMEL/Aggregate+Example?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message