camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject [CONF] Apache Camel > Aggregate Example
Date Tue, 02 Mar 2010 15:34:00 GMT
    <base href="">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="">Aggregate
     <h4>Page <b>edited</b> by             <a href="">Claus
     <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>

<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

<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): 

<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">""</span>
       <span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">""</span>
       <span class="code-keyword">xmlns:camel</span>=<span class="code-quote">""</span>

    <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">""</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
                <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
                <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
                <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
                <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>

<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
        <span class="code-object">Integer</span> num2 = newExchange.getIn().getBody(<span

        <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);

        <span class="code-keyword">return</span> oldExchange;


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

     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href=""
class="grey">Change Notification Preferences</a>

       <a href="">View
       <a href="">View
       <a href=";showCommentArea=true#addcomment">Add

View raw message