camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Claus Ibsen (Confluence)" <conflue...@apache.org>
Subject [CONF] Apache Camel > Recipient List
Date Tue, 20 Aug 2013 11:33:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/en/2176/1/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/Recipient+List">Recipient
List</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
        <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" >|| Name || Default Value || Description
|| <br>| {{delimiter}} | {{,}} | Delimiter used if the [Expression] returned multiple
endpoints. | <br></td></tr>
            <tr><td class="diff-changed-lines" >| {{strategyRef}} | | An [AggregationStrategy|http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/processor/aggregate/AggregationStrategy.html]
that will assemble the replies from recipients into a single outgoing message from the [Recipient
List]. By default Camel will use the last reply as the outgoing message. <span class="diff-added-words"style="background-color:
#dfd;">From *Camel 2.12* onwards you can also use a POJO as the {{AggregationStrategy}},
see the [Aggregate|Aggregator2] page for more details.</span> | <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">|
{{strategyMethodName}} | | *Camel 2.12:* This option can be used to explicit declare the method
name to use, when using POJOs as the {{AggregationStrategy}}. See the [Aggregate|Aggregator2]
page for more details. | <br></td></tr>
            <tr><td class="diff-unchanged" >| {{strategyMethodAllowNull}} | {{false}}
| *Camel 2.12:* If this option is {{false}} then the aggregate method is not used if there
was no data to enrich. If this option is {{true}} then {{null}} values is used as the {{oldExchange}}
(when no data to enrich), when using POJOs as the {{AggregationStrategy}}. See the [Aggregate|Aggregator2]
page for more details. | <br>| {{parallelProcessing}} | {{false}} | *Camel 2.2:* If
enabled, messages are sent to the recipients concurrently. Note that the calling thread will
still wait until all messages have been fully processed before it continues; it&#39;s
the sending and processing of replies from recipients which happens in parallel. |  <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h3><a name="RecipientList-RecipientList"></a>Recipient List</h3>

<p>The <a href="http://www.enterpriseintegrationpatterns.com/RecipientList.html"
class="external-link" rel="nofollow">Recipient List</a> from the <a href="/confluence/display/CAMEL/Enterprise+Integration+Patterns"
title="Enterprise Integration Patterns">EIP patterns</a> allows you to route messages
to a number of dynamically specified recipients.</p>

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

<p>The recipients will receive a copy of the <b>same</b> <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a>, and Camel will execute them sequentially.</p>

<h3><a name="RecipientList-Options"></a>Options</h3>

<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Default Value </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>delimiter</tt> </td>
<td class='confluenceTd'> <tt>,</tt> </td>
<td class='confluenceTd'> Delimiter used if the <a href="/confluence/display/CAMEL/Expression"
title="Expression">Expression</a> returned multiple endpoints. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>strategyRef</tt> </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> An <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/processor/aggregate/AggregationStrategy.html"
class="external-link" rel="nofollow">AggregationStrategy</a> that will assemble the
replies from recipients into a single outgoing message from the <a href="/confluence/display/CAMEL/Recipient+List"
title="Recipient List">Recipient List</a>. By default Camel will use the last reply
as the outgoing message. From <b>Camel 2.12</b> onwards you can also use a POJO
as the <tt>AggregationStrategy</tt>, see the <a href="/confluence/display/CAMEL/Aggregator2"
title="Aggregator2">Aggregate</a> page for more details. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>strategyMethodName</tt> </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> <b>Camel 2.12:</b> This option can be used to
explicit declare the method name to use, when using POJOs as the <tt>AggregationStrategy</tt>.
See the <a href="/confluence/display/CAMEL/Aggregator2" title="Aggregator2">Aggregate</a>
page for more details. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>strategyMethodAllowNull</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.12:</b> If this option is <tt>false</tt>
then the aggregate method is not used if there was no data to enrich. If this option is <tt>true</tt>
then <tt>null</tt> values is used as the <tt>oldExchange</tt> (when
no data to enrich), when using POJOs as the <tt>AggregationStrategy</tt>. See
the <a href="/confluence/display/CAMEL/Aggregator2" title="Aggregator2">Aggregate</a>
page for more details. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>parallelProcessing</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.2:</b> If enabled, messages are sent
to the recipients concurrently. Note that the calling thread will still wait until all messages
have been fully processed before it continues; it's the sending and processing of replies
from recipients which happens in parallel. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>executorServiceRef</tt> </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> <b>Camel 2.2:</b> A custom <a href="/confluence/display/CAMEL/Threading+Model"
title="Threading Model">Thread Pool</a> to use for parallel processing. Note that
enabling this option implies parallel processing, so you need not enable that option as well.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>stopOnException</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.2:</b> Whether to immediately stop
processing when an exception occurs. If disabled, Camel will send the message to all recipients
regardless of any individual failures. You can process exceptions in an <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/processor/aggregate/AggregationStrategy.html"
class="external-link" rel="nofollow">AggregationStrategy</a> implementation, which
supports full control of error handling. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>ignoreInvalidEndpoints</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> Whether to ignore an endpoint
URI that could not be resolved. If disabled, Camel will throw an exception identifying the
invalid endpoint URI. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>streaming</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.5:</b> If enabled, Camel will process
replies out-of-order - that is, in the order received in reply from each recipient. If disabled,
Camel will process replies in the same order as specified by the <a href="/confluence/display/CAMEL/Expression"
title="Expression">Expression</a>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>timeout</tt> </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> <b>Camel 2.5:</b> Specifies a processing timeout
milliseconds. If the <a href="/confluence/display/CAMEL/Recipient+List" title="Recipient
List">Recipient List</a> hasn't been able to send and process all replies within
this timeframe, then the timeout triggers and the <a href="/confluence/display/CAMEL/Recipient+List"
title="Recipient List">Recipient List</a> breaks out, with message flow continuing
to the next element. Note that if you provide a <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/processor/aggregate/TimeoutAwareAggregationStrategy.html"
class="external-link" rel="nofollow">TimeoutAwareAggregationStrategy</a>, its <tt>timeout</tt>
method is invoked before breaking out. <b>Beware:</b> If the timeout is reached
with running tasks still remaining, certain tasks for which it is difficult for Camel to shut
down in a graceful manner may continue to run.  So use this option with a bit of care.  We
may be able to improve this functionality in future Camel releases. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>onPrepareRef</tt> </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> <b>Camel 2.8:</b> A custom <a href="/confluence/display/CAMEL/Processor"
title="Processor">Processor</a> to prepare the copy of the <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> each recipient will receive. This allows you to perform
arbitrary transformations, such as deep-cloning the message payload (or any other custom logic).
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>shareUnitOfWork</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.8:</b> Whether the unit of work should
be shared. See <a href="/confluence/display/CAMEL/Splitter#Splitter-Sharingunitofwork">the
same option on Splitter</a> for more details. </td>
</tr>
</tbody></table>
</div>
</div>

<h4><a name="RecipientList-StaticRecipientList"></a>Static Recipient List</h4>

<p>The following example shows how to route a request from an input <b>queue:a</b>
endpoint to a static list of destinations</p>

<p><b>Using Annotations</b><br/>
You can use the <a href="/confluence/display/CAMEL/RecipientList+Annotation" title="RecipientList
Annotation">RecipientList Annotation</a> on a POJO to create a Dynamic Recipient
List. For more details see the <a href="/confluence/display/CAMEL/Bean+Integration" title="Bean
Integration">Bean Integration</a>.</p>

<p><b>Using the <a href="/confluence/display/CAMEL/Fluent+Builders" title="Fluent
Builders">Fluent Builders</a></b></p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="theme: Default; brush: java; gutter: false"><![CDATA[
RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        errorHandler(deadLetterChannel("mock:error"));

        from("direct:a")
            .multicast().to("direct:b", "direct:c", "direct:d");
    }
};
]]></script>
</div></div>

<p><b>Using the <a href="/confluence/display/CAMEL/Spring+XML+Extensions" title="Spring
XML Extensions">Spring XML Extensions</a></b></p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="theme: Default; brush: xml; gutter: false"><![CDATA[
&lt;camelContext errorHandlerRef="errorHandler" xmlns="http://camel.apache.org/schema/spring"&gt;
    &lt;route&gt;
        &lt;from uri="direct:a"/&gt;
        &lt;multicast&gt;
            &lt;to uri="direct:b"/&gt;
            &lt;to uri="direct:c"/&gt;
            &lt;to uri="direct:d"/&gt;
        &lt;/multicast&gt;
    &lt;/route&gt;
&lt;/camelContext&gt;
]]></script>
</div></div>

<h4><a name="RecipientList-DynamicRecipientList"></a>Dynamic Recipient List</h4>

<p>Usually one of the main reasons for using the <a href="http://www.enterpriseintegrationpatterns.com/RecipientList.html"
class="external-link" rel="nofollow">Recipient List</a> pattern is that the list
of recipients is dynamic and calculated at runtime. The following example demonstrates how
to create a dynamic recipient list using an <a href="/confluence/display/CAMEL/Expression"
title="Expression">Expression</a> (which in this case it extracts a named header
value dynamically) to calculate the list of endpoints which are either of type <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Endpoint.html"
class="external-link" rel="nofollow">Endpoint</a> or are converted to a String and
then resolved using the endpoint <a href="/confluence/display/CAMEL/URIs" title="URIs">URIs</a>.</p>

<p><b>Using the <a href="/confluence/display/CAMEL/Fluent+Builders" title="Fluent
Builders">Fluent Builders</a></b></p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="theme: Default; brush: java; gutter: false"><![CDATA[
RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        errorHandler(deadLetterChannel("mock:error"));

        from("direct:a")
            .recipientList(header("foo"));
    }
};
]]></script>
</div></div>

<p>The above assumes that the header contains a list of endpoint URIs. The following
takes a single string header and tokenizes it</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="theme: Default; brush: java; gutter: false"><![CDATA[
from("direct:a").recipientList(
        header("recipientListHeader").tokenize(","));
]]></script>
</div></div>

<h5><a name="RecipientList-Iteratablevalue"></a>Iteratable value</h5>
<p>The dynamic list of recipients that are defined in the header must be iteratable
such as:</p>
<ul class="alternate" type="square">
	<li><tt>java.util.Collection</tt></li>
	<li><tt>java.util.Iterator</tt></li>
	<li>arrays</li>
	<li><tt>org.w3c.dom.NodeList</tt></li>
	<li>a single String with values separated with comma</li>
	<li>any other type will be regarded as a single value</li>
</ul>


<p><b>Using the <a href="/confluence/display/CAMEL/Spring+XML+Extensions" title="Spring
XML Extensions">Spring XML Extensions</a></b></p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="theme: Default; brush: xml; gutter: false"><![CDATA[
&lt;camelContext errorHandlerRef="errorHandler" xmlns="http://camel.apache.org/schema/spring"&gt;
    &lt;route&gt;
        &lt;from uri="direct:a"/&gt;
        &lt;recipientList&gt;
            &lt;xpath&gt;$foo&lt;/xpath&gt;
        &lt;/recipientList&gt;
    &lt;/route&gt;
&lt;/camelContext&gt;
]]></script>
</div></div>

<p>For further examples of this pattern in use you could look at one of the <a href="http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RecipientListTest.java?view=markup"
class="external-link" rel="nofollow">junit test case</a></p>

<h5><a name="RecipientList-UsingdelimiterinSpringXML"></a>Using delimiter
in Spring XML</h5>

<p>In Spring DSL you can set the <tt>delimiter</tt> attribute for setting
a delimiter to be used if the header value is a single String with multiple separated endpoints.
By default Camel uses comma as delimiter, but this option lets you specify a customer delimiter
to use instead.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="theme: Default; brush: xml; gutter: false"><![CDATA[
&lt;route&gt;
  &lt;from uri="direct:a" /&gt;
  &lt;!-- use comma as a delimiter for String based values --&gt;
  &lt;recipientList delimiter=","&gt;
    &lt;header&gt;myHeader&lt;/header&gt;
  &lt;/recipientList&gt;
&lt;/route&gt;
]]></script>
</div></div>

<p>So if <b>myHeader</b> contains a String with the value <tt>"activemq:queue:foo,
activemq:topic:hello , log:bar"</tt> then Camel will split the String using the delimiter
given in the XML that was comma, resulting into 3 endpoints to send to. You can use spaces
between the endpoints as Camel will trim the value when it lookup the endpoint to send to.
</p>

<p>Note: In Java DSL you use the <tt>tokenizer</tt> to archive the same.
The route above in Java DSL:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
    from("direct:a").recipientList(header("myHeader").tokenize(","));
</pre>
</div></div>

<p>In <b>Camel 2.1</b> its a bit easier as you can pass in the delimiter
as 2nd parameter:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
    from("direct:a").recipientList(header("myHeader"), "#");
</pre>
</div></div>

<h3><a name="RecipientList-Sendingtomultiplerecipientsinparallel"></a>Sending
to multiple recipients in parallel</h3>
<p><b>Available as of Camel 2.2</b></p>

<p>The <a href="/confluence/display/CAMEL/Recipient+List" title="Recipient List">Recipient
List</a> now supports <tt>parallelProcessing</tt> that for example <a
href="/confluence/display/CAMEL/Splitter" title="Splitter">Splitter</a> also supports.
You can use it to use a thread pool to have concurrent tasks sending the <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> to multiple recipients concurrently.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
    from("direct:a").recipientList(header("myHeader")).parallelProcessing();
</pre>
</div></div>

<p>And in Spring XML its an attribute on the recipient list tag.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
   &lt;route&gt;
       &lt;from uri="direct:a"/&gt;
       &lt;recipientList parallelProcessing="true"&gt;
           &lt;header&gt;myHeader&lt;/header&gt;
       &lt;/recipientList&gt;
   &lt;/route&gt;
</pre>
</div></div>

<h3><a name="RecipientList-Stopcontinuingincaseonerecipientfailed"></a>Stop
continuing in case one recipient failed</h3>
<p><b>Available as of Camel 2.2</b></p>

<p>The <a href="/confluence/display/CAMEL/Recipient+List" title="Recipient List">Recipient
List</a> now supports <tt>stopOnException</tt> that for example <a href="/confluence/display/CAMEL/Splitter"
title="Splitter">Splitter</a> also supports. You can use it to stop sending to any
further recipients in case any recipient failed. </p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
    from("direct:a").recipientList(header("myHeader")).stopOnException();
</pre>
</div></div>

<p>And in Spring XML its an attribute on the recipient list tag.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
   &lt;route&gt;
       &lt;from uri="direct:a"/&gt;
       &lt;recipientList stopOnException="true"&gt;
           &lt;header&gt;myHeader&lt;/header&gt;
       &lt;/recipientList&gt;
   &lt;/route&gt;
</pre>
</div></div>

<p><b>Note:</b> You can combine <tt>parallelProcessing</tt>
and <tt>stopOnException</tt> and have them both <tt>true</tt>.</p>

<h3><a name="RecipientList-Ignoreinvalidendpoints"></a>Ignore invalid endpoints</h3>
<p><b>Available as of Camel 2.3</b></p>

<p>The <a href="/confluence/display/CAMEL/Recipient+List" title="Recipient List">Recipient
List</a> now supports <tt>ignoreInvalidEndpoints</tt> which the <a href="/confluence/display/CAMEL/Routing+Slip"
title="Routing Slip">Routing Slip</a> also supports. You can use it to skip endpoints
which is invalid.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
    from("direct:a").recipientList(header("myHeader")).ignoreInvalidEndpoints();
</pre>
</div></div>

<p>And in Spring XML its an attribute on the recipient list tag.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
   &lt;route&gt;
       &lt;from uri="direct:a"/&gt;
       &lt;recipientList ignoreInvalidEndpoints="true"&gt;
           &lt;header&gt;myHeader&lt;/header&gt;
       &lt;/recipientList&gt;
   &lt;/route&gt;
</pre>
</div></div>

<p>Then lets say the <tt>myHeader</tt> contains the following two endpoints
<tt>direct:foo,xxx:bar</tt>. The first endpoint is valid and works. However the
2nd is invalid and will just be ignored. Camel logs at INFO level about, so you can see why
the endpoint was invalid.</p>


<h3><a name="RecipientList-Usingcustom%7B%7BAggregationStrategy%7D%7D"></a>Using
custom <tt>AggregationStrategy</tt></h3>
<p><b>Available as of Camel 2.2</b></p>

<p>You can now use you own <tt>AggregationStrategy</tt> with the <a href="/confluence/display/CAMEL/Recipient+List"
title="Recipient List">Recipient List</a>. However its not that often you need that.
What its good for is that in case you are using <a href="/confluence/display/CAMEL/Request+Reply"
title="Request Reply">Request Reply</a> messaging then the replies from the recipient
can be aggregated. By default Camel uses <tt>UseLatestAggregationStrategy</tt>
which just keeps that last received reply. What if you must remember all the bodies that all
the recipients send back, then you can use your own custom aggregator that keeps those. Its
the same principle as with the <a href="/confluence/display/CAMEL/Aggregator" title="Aggregator">Aggregator</a>
EIP so check it out for details.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
    from("direct:a")
        .recipientList(header("myHeader")).aggregationStrategy(new MyOwnAggregationStrategy())
        .to("direct:b");
</pre>
</div></div>


<p>And in Spring XML its an attribute on the recipient list tag.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
   &lt;route&gt;
       &lt;from uri="direct:a"/&gt;
       &lt;recipientList strategyRef="myStrategy"&gt;
           &lt;header&gt;myHeader&lt;/header&gt;
       &lt;/recipientList&gt;
       &lt;to uri="direct:b"/&gt;
   &lt;/route&gt;

   &lt;bean id="myStrategy" class="com.mycompany.MyOwnAggregationStrategy"/&gt;
</pre>
</div></div>

<h4><a name="RecipientList-Knowingwhichendpointwhenusingcustom%7B%7BAggregationStrategy%7D%7D"></a>Knowing
which endpoint when using custom <tt>AggregationStrategy</tt></h4>
<p><b>Available as of Camel 2.12</b></p>

<p>When using a custom <tt>AggregationStrategy</tt> then the <tt>aggregate</tt>
method is always invoked in the sequential order (also if parallel processing is enabled)
of the endpoints the <a href="/confluence/display/CAMEL/Recipient+List" title="Recipient
List">Recipient List</a> is using. However from Camel 2.12 this is easier to know
as the <tt>newExchange</tt> <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
has a property stored (key is <tt>Exchange.RECIPIENT_LIST_ENDPOINT</tt> with the
uri of the <a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a>.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
    @Override
    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
        String uri = newExchange.getProperty(Exchange.RECIPIENT_LIST_ENDPOINT, String.class);
        ...
    }
</pre>
</div></div>



<h3><a name="RecipientList-Usingcustomthreadpool"></a>Using custom thread
pool</h3>
<p><b>Available as of Camel 2.2</b></p>

<p>A thread pool is only used for <tt>parallelProcessing</tt>. You supply
your own custom thread pool via the <tt>ExecutorServiceStrategy</tt> (see Camel's
<a href="/confluence/display/CAMEL/Threading+Model" title="Threading Model">Threading
Model</a>), the same way you would do it for the <tt>aggregationStrategy</tt>.
By default Camel uses a thread pool with 10 threads (subject to change in a future version).</p>

<h3><a name="RecipientList-Usingmethodcallasrecipientlist"></a>Using method
call as recipient list</h3>
<p>You can use a <a href="/confluence/display/CAMEL/Bean" title="Bean">Bean</a>
to provide the recipients, for example:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
from("activemq:queue:test").recipientList().method(MessageRouter.class, "routeTo");
</pre>
</div></div>

<p>And then <tt>MessageRouter</tt>:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
public class MessageRouter {

    public String routeTo() {
        String queueName = "activemq:queue:test2";
        return queueName;
    }
}
</pre>
</div></div>

<p>When you use a <a href="/confluence/display/CAMEL/Bean" title="Bean">Bean</a>
then do <b>not</b> also use the <tt>@RecipientList</tt> annotation
as this will in fact add yet another recipient list, so you end up having two. Do <b>not</b>
do like this.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
public class MessageRouter {

    @RecipientList
    public String routeTo() {
        String queueName = "activemq:queue:test2";
        return queueName;
    }
}
</pre>
</div></div>

<p>Well you should only do like that above (using <tt>@RecipientList</tt>)
if you route just route to a <a href="/confluence/display/CAMEL/Bean" title="Bean">Bean</a>
which you then want to act as a recipient list.<br/>
So the original route can be changed to:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
from("activemq:queue:test").bean(MessageRouter.class, "routeTo");
</pre>
</div></div>
<p>Which then would invoke the routeTo method and detect its annotated with <tt>@RecipientList</tt>
and then act accordingly as if it was a recipient list EIP.</p>


<h3><a name="RecipientList-Usingtimeout"></a>Using timeout</h3>
<p><b>Available as of Camel 2.5</b></p>

<p>If you use <tt>parallelProcessing</tt> then you can configure a total
<tt>timeout</tt> value in millis. Camel will then process the messages in parallel
until the timeout is hit. This allows you to continue processing if one message is slow. For
example you can set a timeout value of 20 sec.</p>

<div class='panelMacro'><table class='warningMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/forbidden.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>Tasks may
keep running</b><br />If the timeout is reached with running tasks still remaining,
certain tasks for which it is difficult for Camel to shut down in a graceful manner may continue
to run.  So use this option with a bit of care.  We may be able to improve this functionality
in future Camel releases.</td></tr></table></div>

<p>For example in the unit test below you can see we multicast the message to 3 destinations.
We have a timeout of 2 seconds, which means only the last two messages can be completed within
the timeframe. This means we will only aggregate the last two which yields a result aggregation
which outputs <tt>"BC"</tt>.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="theme: Default; brush: java; gutter: false"><![CDATA[
from("direct:start")
    .multicast(new AggregationStrategy() {
            public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
                if (oldExchange == null) {
                    return newExchange;
                }

                String body = oldExchange.getIn().getBody(String.class);
                oldExchange.getIn().setBody(body + newExchange.getIn().getBody(String.class));
                return oldExchange;
            }
        })
        .parallelProcessing().timeout(250).to("direct:a", "direct:b", "direct:c")
    // use end to indicate end of multicast route
    .end()
    .to("mock:result");

from("direct:a").delay(1000).to("mock:A").setBody(constant("A"));

from("direct:b").to("mock:B").setBody(constant("B"));

from("direct:c").to("mock:C").setBody(constant("C"));
]]></script>
</div></div>

<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>Timeout in other EIPs</b><br
/>This <tt>timeout</tt> feature is also supported by <a href="/confluence/display/CAMEL/Splitter"
title="Splitter">Splitter</a> and both <tt>multicast</tt> and <tt>recipientList</tt>.</td></tr></table></div>

<p>By default if a timeout occurs the <tt>AggregationStrategy</tt> is not
invoked. However you can implement a specialized version</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
public interface TimeoutAwareAggregationStrategy extends AggregationStrategy {

    /**
     * A timeout occurred
     *
     * @param oldExchange  the oldest exchange (is &lt;tt&gt;null&lt;/tt&gt;
on first aggregation as we only have the new exchange)
     * @param index        the index
     * @param total        the total
     * @param timeout      the timeout value in millis
     */
    void timeout(Exchange oldExchange, int index, int total, long timeout);
</pre>
</div></div>

<p>This allows you to deal with the timeout in the <tt>AggregationStrategy</tt>
if you really need to. </p>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>Timeout is
total</b><br />The timeout is total, which means that after X time, Camel will
aggregate the messages which has completed within the timeframe. The remainders will be cancelled.
Camel will also only invoke the <tt>timeout</tt> method in the <tt>TimeoutAwareAggregationStrategy</tt>
once, for the first index which caused the timeout.</td></tr></table></div>


<h3><a name="RecipientList-UsingonPreparetoexecutecustomlogicwhenpreparingmessages"></a>Using
onPrepare to execute custom logic when preparing messages</h3>
<p><b>Available as of Camel 2.8</b></p>

<p>See details at <a href="/confluence/display/CAMEL/Multicast" title="Multicast">Multicast</a></p>


<h4><a name="RecipientList-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>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;" class="grey">
                        <a href="https://cwiki.apache.org/confluence/users/removespacenotification.action?spaceKey=CAMEL">Stop
watching space</a>
            <span style="padding: 0px 5px;">|</span>
                <a href="https://cwiki.apache.org/confluence/users/editmyemailsettings.action">Change
email notification preferences</a>
</div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Recipient+List">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=49325&revisedVersion=46&originalVersion=45">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Recipient+List?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message