camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Aggregator2
Date Sun, 03 Mar 2013 05:37:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true" type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/Aggregator2">Aggregator2</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~boday">Ben O&#39;Day</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        updated per CAMEL-6119<br />
    </div>
        <br/>
                         <h4>Changes (2)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>*Available as of Camel 2.9* <br></td></tr>
            <tr><td class="diff-changed-lines" >You can manually <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">complete</span> <span class="diff-added-words"style="background-color: #dfd;">trigger completion of</span> all current aggregated exchanges by sending <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">in</span> a message containing the header Exchange.AGGREGATION_COMPLETE_ALL_GROUPS set to true.  The message is considered a signal message only, the message headers/contents will not be processed otherwise. <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">*Available as of Camel 2.11* <br>You can alternatively set the header Exchange.AGGREGATION_COMPLETE_ALL_GROUPS_INCLUSIVE to true to trigger completion of all groups after processing the current message. <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h4. Using a List&lt;V&gt; in AggregationStrategy <br>*Available as of Camel 2.11* <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h3><a name="Aggregator2-Aggregator"></a>Aggregator</h3>

<p><b>This applies for Camel version 2.3 or newer. If you use an older version then use this <a href="/confluence/display/CAMEL/Aggregator" title="Aggregator">Aggregator</a> link instead.</b></p>

<p>The <a href="http://www.enterpriseintegrationpatterns.com/Aggregator.html" class="external-link" rel="nofollow">Aggregator</a> from the <a href="/confluence/display/CAMEL/Enterprise+Integration+Patterns" title="Enterprise Integration Patterns">EIP patterns</a> allows you to combine a number of messages together into a single message. </p>

<p><span class="image-wrap" style=""><img src="http://www.enterpriseintegrationpatterns.com/img/Aggregator.gif" style="border: 0px solid black" /></span></p>

<p>A correlation <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a> is used to determine the messages which should be aggregated together. If you want to aggregate all messages into a single message, just use a constant expression. An AggregationStrategy is used to combine all the message exchanges for a single correlation key into a single message exchange. </p>

<h3><a name="Aggregator2-Aggregatoroptions"></a>Aggregator options</h3>
<p>The aggregator supports the following options:</p>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Option </th>
<th class='confluenceTh'> Default </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> correlationExpression </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> Mandatory <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a> which evaluates the correlation key to use for aggregation. The <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a> which has the same correlation key is aggregated together. If the correlation key could not be evaluated an Exception is thrown. You can disable this by using the <tt>ignoreBadCorrelationKeys</tt> option. </td>
</tr>
<tr>
<td class='confluenceTd'> aggregationStrategy </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> Mandatory <tt>AggregationStrategy</tt> which is used to <em>merge</em> the incoming <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a> with the existing already merged exchanges. At first call the <tt>oldExchange</tt> parameter is <tt>null</tt>. On subsequent invocations the <tt>oldExchange</tt> contains the merged exchanges and <tt>newExchange</tt> is of course the new incoming Exchange. From <b>Camel 2.9.2</b> onwards the strategy can also be a <tt>TimeoutAwareAggregationStrategy</tt> implementation, supporting the timeout callback, see further below for more details. </td>
</tr>
<tr>
<td class='confluenceTd'> strategyRef </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> A reference to lookup the <tt>AggregationStrategy</tt> in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>. </td>
</tr>
<tr>
<td class='confluenceTd'> completionSize </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> Number of messages aggregated before the aggregation is complete. This option can be set as either a fixed value or using an <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a> which allows you to evaluate a size dynamically - will use <tt>Integer</tt> as result. If both are set Camel will fallback to use the fixed value if the <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a> result was <tt>null</tt> or <tt>0</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> completionTimeout </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> Time in millis that an aggregated exchange should be inactive before its complete. This option can be set as either a fixed value or using an <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a> which allows you to evaluate a timeout dynamically - will use <tt>Long</tt> as result. If both are set Camel will fallback to use the fixed value if the <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a> result was <tt>null</tt> or <tt>0</tt>. You cannot use this option together with completionInterval, only one of the two can be used. </td>
</tr>
<tr>
<td class='confluenceTd'> completionInterval </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> A repeating period in millis by which the aggregator will complete all current aggregated exchanges. Camel has a background task which is triggered every period. You cannot use this option together with completionTimeout, only one of them can be used. </td>
</tr>
<tr>
<td class='confluenceTd'> completionPredicate </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> A <a href="/confluence/display/CAMEL/Predicate" title="Predicate">Predicate</a> to indicate when an aggregated exchange is complete. </td>
</tr>
<tr>
<td class='confluenceTd'> completionFromBatchConsumer </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> This option is if the exchanges are coming from a <a href="/confluence/display/CAMEL/Batch+Consumer" title="Batch Consumer">Batch Consumer</a>. Then when enabled the <a href="/confluence/display/CAMEL/Aggregator2" title="Aggregator2">Aggregator2</a> will use the batch size determined by the <a href="/confluence/display/CAMEL/Batch+Consumer" title="Batch Consumer">Batch Consumer</a> in the message header <tt>CamelBatchSize</tt>. See more details at <a href="/confluence/display/CAMEL/Batch+Consumer" title="Batch Consumer">Batch Consumer</a>. This can be used to aggregate all files consumed from a <a href="/confluence/display/CAMEL/File2" title="File2">File</a> endpoint in that given poll. </td>
</tr>
<tr>
<td class='confluenceTd'> forceCompletionOnStop </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.9</b> Indicates to complete all current aggregated exchanges when the context is stopped </td>
</tr>
<tr>
<td class='confluenceTd'> eagerCheckCompletion </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Whether or not to eager check for completion when a new incoming <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a> has been received. This option influences the behavior of the <tt>completionPredicate</tt> option as the <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a> being passed in changes accordingly. When <tt>false</tt> the <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a> passed in the <a href="/confluence/display/CAMEL/Predicate" title="Predicate">Predicate</a> is the <em>aggregated</em> Exchange which means any information you may store on the aggregated Exchange from the <tt>AggregationStrategy</tt> is available for the <a href="/confluence/display/CAMEL/Predicate" title="Predicate">Predicate</a>. When <tt>true</tt> the <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a> passed in the <a href="/confluence/display/CAMEL/Predicate" title="Predicate">Predicate</a> is the <em>incoming</em> <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>, which means you can access data from the incoming Exchange. </td>
</tr>
<tr>
<td class='confluenceTd'> groupExchanges </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> If enabled then Camel will group all aggregated Exchanges into a single combined <tt>org.apache.camel.impl.GroupedExchange</tt> holder class that holds all the aggregated Exchanges. And as a result only one Exchange is being sent out from the aggregator. Can be used to combine many incoming Exchanges into a single output Exchange without coding a custom <tt>AggregationStrategy</tt> yourself. <b>Important:</b> This option does <b>not</b> support persistant repository with the aggregator. </td>
</tr>
<tr>
<td class='confluenceTd'> ignoreInvalidCorrelationKeys </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Whether or not to ignore correlation keys which could not be evaluated to a value. By default Camel will throw an Exception, but you can enable this option and ignore the situation instead. </td>
</tr>
<tr>
<td class='confluenceTd'> closeCorrelationKeyOnCompletion </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> Whether or not too <em>late</em> Exchanges should be accepted or not. You can enable this to indicate that if a correlation key has already been completed, then any new exchanges with the same correlation key be denied. Camel will then throw a <tt>closedCorrelationKeyException</tt> exception. When using this option you pass in a <tt>integer</tt> which is a number for a LRUCache which keeps that last X number of closed correlation keys. You can pass in 0 or a negative value to indicate a unbounded cache. By passing in a number you are ensured that cache won't grow too big if you use a log of different correlation keys. </td>
</tr>
<tr>
<td class='confluenceTd'> discardOnCompletionTimeout </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.5:</b> Whether or not exchanges which complete due to a timeout should be discarded. If enabled then when a timeout occurs the aggregated message will <b>not</b> be sent out but dropped (discarded). </td>
</tr>
<tr>
<td class='confluenceTd'> aggregationRepository </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> Allows you to plugin you own implementation of <tt>org.apache.camel.spi.AggregationRepository</tt> which keeps track of the current inflight aggregated exchanges. Camel uses by default a memory based implementation. </td>
</tr>
<tr>
<td class='confluenceTd'> aggregationRepositoryRef </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> Reference to lookup a <tt>aggregationRepository</tt> in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>. </td>
</tr>
<tr>
<td class='confluenceTd'> parallelProcessing </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> When aggregated are completed they are being send out of the aggregator. This option indicates whether or not Camel should use a thread pool with multiple threads for concurrency. If no custom thread pool has been specified then Camel creates a default pool with 10 concurrent threads. </td>
</tr>
<tr>
<td class='confluenceTd'> executorService </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> If using <tt>parallelProcessing</tt> you can specify a custom thread pool to be used. In fact also if you are not using <tt>parallelProcessing</tt> this custom thread pool is used to send out aggregated exchanges as well. </td>
</tr>
<tr>
<td class='confluenceTd'> executorServiceRef </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> Reference to lookup a <tt>executorService</tt> in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a> </td>
</tr>
<tr>
<td class='confluenceTd'> timeoutCheckerExecutorService </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> <b>Camel 2.9:</b> If using either of the <tt>completionTimeout</tt>, <tt>completionTimeoutExpression</tt>, or <tt>completionInterval</tt> options a background thread is created to check for the completion for every aggregator. Set this option to provide a custom thread pool to be used rather than creating a new thread for every aggregator. </td>
</tr>
<tr>
<td class='confluenceTd'> timeoutCheckerExecutorServiceRef </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> <b>Camel 2.9:</b> Reference to lookup a <tt>timeoutCheckerExecutorService</tt> in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a> </td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="Aggregator2-ExchangeProperties"></a>Exchange Properties</h3>

<p>The following properties are set on each aggregated Exchange:</p>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> header </th>
<th class='confluenceTh'> type </th>
<th class='confluenceTh'> description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelAggregatedSize</tt> </td>
<td class='confluenceTd'> int </td>
<td class='confluenceTd'> The total number of Exchanges aggregated into this combined Exchange. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelAggregatedCompletedBy</tt> </td>
<td class='confluenceTd'> String </td>
<td class='confluenceTd'> Indicator how the aggregation was completed as a value of either: <tt>predicate</tt>, <tt>size</tt>, <tt>consumer</tt>, <tt>timeout</tt> or <tt>interval</tt>. </td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="Aggregator2-AboutAggregationStrategy"></a>About AggregationStrategy</h3>
<p>The <tt>AggregationStrategy</tt> is used for aggregating the old (lookup by its correlation id) and the new exchanges together into a single exchange. Possible implementations include performing some kind of combining or delta processing, such as adding line items together into an invoice or just using the newest exchange and removing old exchanges such as for state tracking or market data prices; where old values are of little use.</p>

<p>Notice the aggregation strategy is a mandatory option and must be provided to the aggregator.</p>

<p>Here are a few example AggregationStrategy implementations that should help you create your own custom strategy.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">//simply combines Exchange <span class="code-object">String</span> body values using '+' as a delimiter
</span>class StringAggregationStrategy <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">String</span> oldBody = oldExchange.getIn().getBody(<span class="code-object">String</span>.class);
        <span class="code-object">String</span> newBody = newExchange.getIn().getBody(<span class="code-object">String</span>.class);
        oldExchange.getIn().setBody(oldBody + <span class="code-quote">"+"</span> + newBody);
        <span class="code-keyword">return</span> oldExchange;
    }
}

<span class="code-comment">//simply combines Exchange body values into an ArrayList&lt;<span class="code-object">Object</span>&gt;
</span>class ArrayListAggregationStrategy <span class="code-keyword">implements</span> AggregationStrategy {

    <span class="code-keyword">public</span> Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
	<span class="code-object">Object</span> newBody = newExchange.getIn().getBody();
	ArrayList&lt;<span class="code-object">Object</span>&gt; list = <span class="code-keyword">null</span>;
        <span class="code-keyword">if</span> (oldExchange == <span class="code-keyword">null</span>) {
		list = <span class="code-keyword">new</span> ArrayList&lt;<span class="code-object">Object</span>&gt;();
		list.add(newBody);
		newExchange.getIn().setBody(list);
		<span class="code-keyword">return</span> newExchange;
        } <span class="code-keyword">else</span> {
	        list = oldExchange.getIn().getBody(ArrayList.class);
		list.add(newBody);
		<span class="code-keyword">return</span> oldExchange;
	}
    }
}
</pre>
</div></div>

<h3><a name="Aggregator2-Aboutcompletion"></a>About completion</h3>
<p>When aggregation <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>s at some point you need to indicate that the aggregated exchanges is complete, so they can be send out of the aggregator. Camel allows you to indicate completion in various ways as follows:</p>
<ul class="alternate" type="square">
	<li>completionTimeout - Is an inactivity timeout in which is triggered if no new exchanges have been aggregated for that particular correlation key within the period.</li>
	<li>completionInterval - Once every X period all the current aggregated exchanges are completed.</li>
	<li>completionSize - Is a number indicating that after X aggregated exchanges it's complete.</li>
	<li>completionPredicate - Runs a <a href="/confluence/display/CAMEL/Predicate" title="Predicate">Predicate</a> when a new exchange is aggregated to determine if we are complete or not</li>
	<li>completionFromBatchConsumer - Special option for <a href="/confluence/display/CAMEL/Batch+Consumer" title="Batch Consumer">Batch Consumer</a> which allows you to complete when all the messages from the batch has been aggregated.</li>
	<li>forceCompletionOnStop - <b>Camel 2.9</b> Indicates to complete all current aggregated exchanges when the context is stopped</li>
</ul>


<p>Notice that all the completion ways are per correlation key. And you can combine them in any way you like. It's basically the first which triggers that wins. So you can use a completion size together with a completion timeout. Only completionTimeout and completionInterval cannot be used at the same time.</p>

<p>Notice the completion is a mandatory option and must be provided to the aggregator. If not provided Camel will thrown an Exception on startup.</p>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b>Callbacks</b><br />See the <tt>TimeoutAwareAggregationStrategy</tt> and <tt>CompletionAwareAggregationStrategy</tt> extensions to <tt>AggregationStrategy</tt> that has callbacks when the aggregated Exchange was completed and if a timeout occurred.</td></tr></table></div>

<h3><a name="Aggregator2-PersistentAggregationRepository"></a>Persistent AggregationRepository</h3>
<p>The aggregator provides a pluggable repository which you can implement your own <tt>org.apache.camel.spi.AggregationRepository</tt>.<br/>
If you need persistent repository then you can use either Camel <a href="/confluence/display/CAMEL/HawtDB" title="HawtDB">HawtDB</a> or <a href="/confluence/display/CAMEL/SQL+Component" title="SQL Component">SQL Component</a> components.</p>

<h3><a name="Aggregator2-Examples"></a>Examples</h3>
<p>See some examples from the old <a href="/confluence/display/CAMEL/Aggregator" title="Aggregator">Aggregator</a> which is somewhat similar to this new aggregator.</p>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b>Setting options in Spring XML</b><br />Many of the options are configurable as attributes on the <tt>&lt;aggregate&gt;</tt> tag when using Spring XML.</td></tr></table></div>

<h4><a name="Aggregator2-UsingcompletionTimeout"></a>Using completionTimeout</h4>
<p>In this example we want to aggregate all incoming messages and after 3 seconds of inactivity we want the aggregation to complete. This is done using the <tt>completionTimeout</tt> option as shown:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:start"</span>)
    <span class="code-comment">// aggregate all exchanges correlated by the id header.
</span>    <span class="code-comment">// Aggregate them using the BodyInAggregatingStrategy strategy which
</span>    <span class="code-comment">// and after 3 seconds of inactivity them timeout and complete the aggregation
</span>    <span class="code-comment">// and send it to mock:aggregated
</span>    .aggregate(header(<span class="code-quote">"id"</span>), <span class="code-keyword">new</span> BodyInAggregatingStrategy()).completionTimeout(3000)
        .to(<span class="code-quote">"mock:aggregated"</span>);
</pre>
</div></div>

<p>And the same example using Spring XML:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;camelContext 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">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
        <span class="code-tag">&lt;aggregate strategyRef=<span class="code-quote">"aggregatorStrategy"</span> completionTimeout=<span class="code-quote">"3000"</span>&gt;</span>
            <span class="code-tag">&lt;correlationExpression&gt;</span>
                <span class="code-tag">&lt;simple&gt;</span>header.id<span class="code-tag">&lt;/simple&gt;</span>
            <span class="code-tag">&lt;/correlationExpression&gt;</span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:aggregated"</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;bean id=<span class="code-quote">"aggregatorStrategy"</span> class=<span class="code-quote">"org.apache.camel.processor.BodyInAggregatingStrategy"</span>/&gt;</span>
</pre>
</div></div>

<h4><a name="Aggregator2-UsingTimeoutAwareAggregationStrategy"></a>Using TimeoutAwareAggregationStrategy</h4>
<p><b>Available as of Camel 2.9.2</b></p>

<p>If your aggregation strategy implements <tt>TimeoutAwareAggregationStrategy</tt>, then Camel will invoke the <tt>timeout</tt> method when the timeout occurs. Notice that the values for index and total parameters will be -1, and the timeout parameter will be provided only if configured as a fixed value. You must <b>not</b> throw any exceptions from the <tt>timeout</tt> method.</p>

<h4><a name="Aggregator2-UsingCompletionAwareAggregationStrategy"></a>Using CompletionAwareAggregationStrategy</h4>
<p><b>Available as of Camel 2.9.3</b></p>

<p>If your aggregation strategy implements <tt>CompletionAwareAggregationStrategy</tt>, then Camel will invoke the <tt>onComplete</tt> method when the aggregated Exchange is completed.  This allows you to do any last minute custom logic such as to cleanup some resources, or additional work on the exchange as it's now completed.<br/>
You must <b>not</b> throw any exceptions from the <tt>onCompletion</tt> method.</p>

<h4><a name="Aggregator2-UsingcompletionSize"></a>Using completionSize</h4>
<p>In this example we want to aggregate all incoming messages and when we have 3 messages aggregated (in the same correlation group) we want the aggregation to complete. This is done using the <tt>completionSize</tt> option as shown:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:start"</span>)
    <span class="code-comment">// aggregate all exchanges correlated by the id header.
</span>    <span class="code-comment">// Aggregate them using the BodyInAggregatingStrategy strategy which
</span>    <span class="code-comment">// and after 3 messages has been aggregated then complete the aggregation
</span>    <span class="code-comment">// and send it to mock:aggregated
</span>    .aggregate(header(<span class="code-quote">"id"</span>), <span class="code-keyword">new</span> BodyInAggregatingStrategy()).completionSize(3)
        .to(<span class="code-quote">"mock:aggregated"</span>);
</pre>
</div></div>

<p>And the same example using Spring XML:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;camelContext 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">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
        <span class="code-tag">&lt;aggregate strategyRef=<span class="code-quote">"aggregatorStrategy"</span> completionSize=<span class="code-quote">"3"</span>&gt;</span>
            <span class="code-tag">&lt;correlationExpression&gt;</span>
                <span class="code-tag">&lt;simple&gt;</span>header.id<span class="code-tag">&lt;/simple&gt;</span>
            <span class="code-tag">&lt;/correlationExpression&gt;</span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:aggregated"</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;bean id=<span class="code-quote">"aggregatorStrategy"</span> class=<span class="code-quote">"org.apache.camel.processor.BodyInAggregatingStrategy"</span>/&gt;</span>
</pre>
</div></div>

<h4><a name="Aggregator2-UsingcompletionPredicate"></a>Using completionPredicate</h4>
<p>In this example we want to aggregate all incoming messages and use a <a href="/confluence/display/CAMEL/Predicate" title="Predicate">Predicate</a> to determine when we are complete. The <a href="/confluence/display/CAMEL/Predicate" title="Predicate">Predicate</a> can be evaluated using either the aggregated exchange (default) or the incoming exchange. We will so both situations as examples. We start with the default situation as shown:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:start"</span>)
    <span class="code-comment">// aggregate all exchanges correlated by the id header.
</span>    <span class="code-comment">// Aggregate them using the BodyInAggregatingStrategy strategy which
</span>    <span class="code-comment">// and when the aggregated body contains A+B+C then complete the aggregation
</span>    <span class="code-comment">// and send it to mock:aggregated
</span>    .aggregate(header(<span class="code-quote">"id"</span>), <span class="code-keyword">new</span> BodyInAggregatingStrategy()).completionPredicate(body().contains(<span class="code-quote">"A+B+C"</span>))
        .to(<span class="code-quote">"mock:aggregated"</span>);
</pre>
</div></div>

<p>And the same example using Spring XML:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;camelContext 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">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
        <span class="code-tag">&lt;aggregate strategyRef=<span class="code-quote">"aggregatorStrategy"</span>&gt;</span>
            <span class="code-tag">&lt;correlationExpression&gt;</span>
                <span class="code-tag">&lt;simple&gt;</span>header.id<span class="code-tag">&lt;/simple&gt;</span>
            <span class="code-tag">&lt;/correlationExpression&gt;</span>
            <span class="code-tag">&lt;completionPredicate&gt;</span>
                <span class="code-tag">&lt;simple&gt;</span>${body} contains 'A+B+C'<span class="code-tag">&lt;/simple&gt;</span>
            <span class="code-tag">&lt;/completionPredicate&gt;</span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:aggregated"</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;bean id=<span class="code-quote">"aggregatorStrategy"</span> class=<span class="code-quote">"org.apache.camel.processor.BodyInAggregatingStrategy"</span>/&gt;</span>
</pre>
</div></div>

<p>And the other situation where we use the <tt>eagerCheckCompletion</tt> option to tell Camel to use the incoming Exchange. Notice how we can just test in the completion predicate that the incoming message is the <em>END</em> message:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:start"</span>)
    <span class="code-comment">// aggregate all exchanges correlated by the id header.
</span>    <span class="code-comment">// Aggregate them using the BodyInAggregatingStrategy strategy
</span>    <span class="code-comment">// <span class="code-keyword">do</span> eager checking which means the completion predicate will use the incoming exchange
</span>    <span class="code-comment">// which allows us to trigger completion when a certain exchange arrived which is the
</span>    <span class="code-comment">// END message
</span>    .aggregate(header(<span class="code-quote">"id"</span>), <span class="code-keyword">new</span> BodyInAggregatingStrategy())
        .eagerCheckCompletion().completionPredicate(body().isEqualTo(<span class="code-quote">"END"</span>))
        .to(<span class="code-quote">"mock:aggregated"</span>);
</pre>
</div></div>

<p>And the same example using Spring XML:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;camelContext 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">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
        <span class="code-tag">&lt;aggregate strategyRef=<span class="code-quote">"aggregatorStrategy"</span> eagerCheckCompletion=<span class="code-quote">"true"</span>&gt;</span>
            <span class="code-tag">&lt;correlationExpression&gt;</span>
                <span class="code-tag">&lt;simple&gt;</span>header.id<span class="code-tag">&lt;/simple&gt;</span>
            <span class="code-tag">&lt;/correlationExpression&gt;</span>
            <span class="code-tag">&lt;completionPredicate&gt;</span>
                <span class="code-tag">&lt;simple&gt;</span>${body} == 'END'<span class="code-tag">&lt;/simple&gt;</span>
            <span class="code-tag">&lt;/completionPredicate&gt;</span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:aggregated"</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;bean id=<span class="code-quote">"aggregatorStrategy"</span> class=<span class="code-quote">"org.apache.camel.processor.BodyInAggregatingStrategy"</span>/&gt;</span>
</pre>
</div></div>

<h4><a name="Aggregator2-UsingdynamiccompletionTimeout"></a>Using dynamic completionTimeout</h4>
<p>In this example we want to aggregate all incoming messages and after a period of inactivity we want the aggregation to complete. The period should be computed at runtime based on the <tt>timeout</tt> header in the incoming messages. This is done using the <tt>completionTimeout</tt> option as shown:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:start"</span>)
    <span class="code-comment">// aggregate all exchanges correlated by the id header.
</span>    <span class="code-comment">// Aggregate them using the BodyInAggregatingStrategy strategy which
</span>    <span class="code-comment">// and the timeout header contains the timeout in millis of inactivity them timeout and complete the aggregation
</span>    <span class="code-comment">// and send it to mock:aggregated
</span>    .aggregate(header(<span class="code-quote">"id"</span>), <span class="code-keyword">new</span> BodyInAggregatingStrategy()).completionTimeout(header(<span class="code-quote">"timeout"</span>))
        .to(<span class="code-quote">"mock:aggregated"</span>);
</pre>
</div></div>

<p>And the same example using Spring XML:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;camelContext 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">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
        <span class="code-tag">&lt;aggregate strategyRef=<span class="code-quote">"aggregatorStrategy"</span>&gt;</span>
            <span class="code-tag">&lt;correlationExpression&gt;</span>
                <span class="code-tag">&lt;simple&gt;</span>header.id<span class="code-tag">&lt;/simple&gt;</span>
            <span class="code-tag">&lt;/correlationExpression&gt;</span>
            <span class="code-tag">&lt;completionTimeout&gt;</span>
                <span class="code-tag">&lt;header&gt;</span>timeout<span class="code-tag">&lt;/header&gt;</span>
            <span class="code-tag">&lt;/completionTimeout&gt;</span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:aggregated"</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;bean id=<span class="code-quote">"aggregatorStrategy"</span> class=<span class="code-quote">"org.apache.camel.processor.BodyInAggregatingStrategy"</span>/&gt;</span>
</pre>
</div></div>

<p><b>Note:</b> You can also add a fixed timeout value and Camel will fallback to use this value if the dynamic value was <tt>null</tt> or <tt>0</tt>.</p>

<h4><a name="Aggregator2-UsingdynamiccompletionSize"></a>Using dynamic completionSize</h4>
<p>In this example we want to aggregate all incoming messages based on a dynamic size per correlation key. The size is computed at runtime based on the <tt>mySize</tt> header in the incoming messages. This is done using the <tt>completionSize</tt> option as shown:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:start"</span>)
    <span class="code-comment">// aggregate all exchanges correlated by the id header.
</span>    <span class="code-comment">// Aggregate them using the BodyInAggregatingStrategy strategy which
</span>    <span class="code-comment">// and the header mySize determines the number of aggregated messages should trigger the completion
</span>    <span class="code-comment">// and send it to mock:aggregated
</span>    .aggregate(header(<span class="code-quote">"id"</span>), <span class="code-keyword">new</span> BodyInAggregatingStrategy()).completionSize(header(<span class="code-quote">"mySize"</span>))
        .to(<span class="code-quote">"mock:aggregated"</span>);
</pre>
</div></div>

<p>And the same example using Spring XML:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;camelContext 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">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
        <span class="code-tag">&lt;aggregate strategyRef=<span class="code-quote">"aggregatorStrategy"</span>&gt;</span>
            <span class="code-tag">&lt;correlationExpression&gt;</span>
                <span class="code-tag">&lt;simple&gt;</span>header.id<span class="code-tag">&lt;/simple&gt;</span>
            <span class="code-tag">&lt;/correlationExpression&gt;</span>
            <span class="code-tag">&lt;completionSize&gt;</span>
                <span class="code-tag">&lt;header&gt;</span>mySize<span class="code-tag">&lt;/header&gt;</span>
            <span class="code-tag">&lt;/completionSize&gt;</span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:aggregated"</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;bean id=<span class="code-quote">"aggregatorStrategy"</span> class=<span class="code-quote">"org.apache.camel.processor.BodyInAggregatingStrategy"</span>/&gt;</span>
</pre>
</div></div>

<p><b>Note:</b> You can also add a fixed size value and Camel will fallback to use this value if the dynamic value was <tt>null</tt> or <tt>0</tt>.</p>

<h4><a name="Aggregator2-UsingThisPattern"></a>Using This Pattern</h4>

<p>If you would like to use this EIP Pattern then please read the <a href="/confluence/display/CAMEL/Getting+Started" title="Getting Started">Getting Started</a>, you may also find the <a href="/confluence/display/CAMEL/Architecture" title="Architecture">Architecture</a> useful particularly the description of <a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a> and <a href="/confluence/display/CAMEL/URIs" title="URIs">URIs</a>. Then you could try out some of the <a href="/confluence/display/CAMEL/Examples" title="Examples">Examples</a> first before trying this pattern out.</p>

<h4><a name="Aggregator2-ManuallyForcetheCompletionofAllAggregatedExchangesImmediately"></a>Manually Force the Completion of All Aggregated Exchanges Immediately</h4>

<p><b>Available as of Camel 2.9</b><br/>
You can manually trigger completion of all current aggregated exchanges by sending a message containing the header Exchange.AGGREGATION_COMPLETE_ALL_GROUPS set to true.  The message is considered a signal message only, the message headers/contents will not be processed otherwise.</p>

<p><b>Available as of Camel 2.11</b><br/>
You can alternatively set the header Exchange.AGGREGATION_COMPLETE_ALL_GROUPS_INCLUSIVE to true to trigger completion of all groups after processing the current message.</p>

<h4><a name="Aggregator2-UsingaList%3CV%3EinAggregationStrategy"></a>Using a List&lt;V&gt; in AggregationStrategy</h4>
<p><b>Available as of Camel 2.11</b></p>

<p>If you want to aggregate some value from the messages &lt;V&gt; into a List&lt;V&gt; then we have added a <tt>org.apache.camel.processor.aggregate.AbstractListAggregationStrategy</tt> abstract class in <b>Camel 2.11</b> that makes this easier. The completed Exchange that is sent out of the aggregator will contain the List&lt;V&gt; in the message body.</p>

<p>For example to aggregate a List&lt;Integer&gt; you can extend this class as shown below, and implement the <tt>getValue</tt> method:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">/**
 * Our strategy just group a list of integers.
 */
<span class="code-keyword">public</span> <span class="code-keyword">final</span> class MyListOfNumbersStrategy <span class="code-keyword">extends</span> AbstractListAggregationStrategy&lt;<span class="code-object">Integer</span>&gt; {

    @Override
    <span class="code-keyword">public</span> <span class="code-object">Integer</span> getValue(Exchange exchange) {
        <span class="code-comment">// the message body contains a number, so just <span class="code-keyword">return</span> that as-is
</span>        <span class="code-keyword">return</span> exchange.getIn().getBody(<span class="code-object">Integer</span>.class);
    }
}
</pre>
</div></div>


<h3><a name="Aggregator2-Seealso"></a>See also</h3>
<ul class="alternate" type="square">
	<li>The <a href="/confluence/display/CAMEL/Loan+Broker+Example" title="Loan Broker Example">Loan Broker Example</a> which uses an aggregator</li>
	<li><a href="http://tmielke.blogspot.com/2009/01/using-camel-aggregator-correctly.html" class="external-link" rel="nofollow">Blog post by Torsten Mielke</a> about using the aggregator correctly.</li>
	<li>The old <a href="/confluence/display/CAMEL/Aggregator" title="Aggregator">Aggregator</a></li>
	<li><a href="/confluence/display/CAMEL/HawtDB" title="HawtDB">HawtDB</a> or <a href="/confluence/display/CAMEL/SQL+Component" title="SQL Component">SQL Component</a> for persistence support</li>
	<li><a href="/confluence/display/CAMEL/Aggregate+Example" title="Aggregate Example">Aggregate Example</a> for an example application</li>
</ul>

    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action" class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Aggregator2">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=14057759&revisedVersion=31&originalVersion=30">View Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Aggregator2?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message