camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Dynamic Router
Date Mon, 30 Aug 2010 07:55:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/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/Dynamic+Router">Dynamic
Router</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-changed-lines" ><span class="diff-changed-words">h<span
class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">3</span><span
class="diff-added-chars"style="background-color: #dfd;">2</span>.</span> Dynamic
Router <br></td></tr>
            <tr><td class="diff-unchanged" > <br>The [Dynamic Router|http://www.enterpriseintegrationpatterns.com/DynamicRouter.html]
from the [EIP patterns|Enterprise Integration Patterns] allows you to route messages while
avoiding the dependency of the router on all possible destinations while maintaining its efficiency.
<br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >!http://www.enterpriseintegrationpatterns.com/img/DynamicRouter.gif!
<br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">In
*Camel 2.5* we introduced a {{dynamicRouter}} in the DSL which is essentially a dynamic [Routing
Slip] which evaluates the slip _on-the-fly_.  <br> <br>h3. Dynamic Router in Camel
2.5 onwards <br> <br>In Java DSL you can use the {{routingSlip}} as shown below:
<br> <br>TODO: e1 <br> <br>Which will leverage a [Bean] to compute
the slip _on-the-fly_, which could be implemented as follows: <br> <br>TODO: e2
<br> <br>The same example in Spring XML would be: <br> <br>You can
also use the {{@DynamicRouter}} annotation, for example the Camel 2.4 example below could
be written as follows. The {{route}} method would then be invoked repeatedly as the message
is processed dynamically. The idea is to return the next endpoint uri where to go. Return
{{null}} to indicate the end. You can return multiple endpoints if you like, just as the [Routing
Slip], where each endpoint is separated by a delimiter. <br>{code:java} <br>public
class MyDynamicRouter { <br> <br>    @Consume(uri = &quot;activemq:foo&quot;)
<br>    @DynamicRouter <br>    public String route(@XPath(&quot;/customer/id&quot;)
String customerId, @Header(&quot;Location&quot;) String location, Document body) {
<br>        // query a database to find the best match of the endpoint based on the
input parameteres <br>        // return the next endpoint uri, where to go. Return null
to indicate the end. <br>    } <br>} <br>{code} <br> <br>h3.
Dynamic Router in Camel 2.4 or older <br></td></tr>
            <tr><td class="diff-unchanged" >The simplest way to implement this
is to use the [RecipientList Annotation] on a Bean method to determine where to route the
message. <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
        </table>
</div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="DynamicRouter-DynamicRouter"></a>Dynamic Router</h2>

<p>The <a href="http://www.enterpriseintegrationpatterns.com/DynamicRouter.html"
class="external-link" rel="nofollow">Dynamic Router</a> from the <a href="/confluence/display/CAMEL/Enterprise+Integration+Patterns"
title="Enterprise Integration Patterns">EIP patterns</a> allows you to route messages
while avoiding the dependency of the router on all possible destinations while maintaining
its efficiency.</p>

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

<p>In <b>Camel 2.5</b> we introduced a <tt>dynamicRouter</tt>
in the DSL which is essentially a dynamic <a href="/confluence/display/CAMEL/Routing+Slip"
title="Routing Slip">Routing Slip</a> which evaluates the slip <em>on-the-fly</em>.
</p>

<h3><a name="DynamicRouter-DynamicRouterinCamel2.5onwards"></a>Dynamic Router
in Camel 2.5 onwards</h3>

<p>In Java DSL you can use the <tt>routingSlip</tt> as shown below:</p>

<p>TODO: e1</p>

<p>Which will leverage a <a href="/confluence/display/CAMEL/Bean" title="Bean">Bean</a>
to compute the slip <em>on-the-fly</em>, which could be implemented as follows:</p>

<p>TODO: e2</p>

<p>The same example in Spring XML would be:</p>

<p>You can also use the <tt>@DynamicRouter</tt> annotation, for example
the Camel 2.4 example below could be written as follows. The <tt>route</tt> method
would then be invoked repeatedly as the message is processed dynamically. The idea is to return
the next endpoint uri where to go. Return <tt>null</tt> to indicate the end. You
can return multiple endpoints if you like, just as the <a href="/confluence/display/CAMEL/Routing+Slip"
title="Routing Slip">Routing Slip</a>, where each endpoint is separated by a delimiter.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">public</span> class MyDynamicRouter {

    @Consume(uri = <span class="code-quote">"activemq:foo"</span>)
    @DynamicRouter
    <span class="code-keyword">public</span> <span class="code-object">String</span>
route(@XPath(<span class="code-quote">"/customer/id"</span>) <span class="code-object">String</span>
customerId, @Header(<span class="code-quote">"Location"</span>) <span class="code-object">String</span>
location, Document body) {
        <span class="code-comment">// query a database to find the best match of the
endpoint based on the input parameteres
</span>        <span class="code-comment">// <span class="code-keyword">return</span>
the next endpoint uri, where to go. Return <span class="code-keyword">null</span>
to indicate the end.
</span>    }
}
</pre>
</div></div>

<h3><a name="DynamicRouter-DynamicRouterinCamel2.4orolder"></a>Dynamic Router
in Camel 2.4 or older</h3>
<p>The simplest way to implement this is to use the <a href="/confluence/display/CAMEL/RecipientList+Annotation"
title="RecipientList Annotation">RecipientList Annotation</a> on a Bean method to
determine where to route the message.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">public</span> class MyDynamicRouter {

    @Consume(uri = <span class="code-quote">"activemq:foo"</span>)
    @RecipientList
    <span class="code-keyword">public</span> List&lt;<span class="code-object">String</span>&gt;
route(@XPath(<span class="code-quote">"/customer/id"</span>) <span class="code-object">String</span>
customerId, @Header(<span class="code-quote">"Location"</span>) <span class="code-object">String</span>
location, Document body) {
        <span class="code-comment">// query a database to find the best match of the
endpoint based on the input parameteres
</span>        ...
    }
}
</pre>
</div></div>

<p>In the above we can use the <a href="/confluence/display/CAMEL/Parameter+Binding+Annotations"
title="Parameter Binding Annotations">Parameter Binding Annotations</a> to bind different
parts of the <a href="/confluence/display/CAMEL/Message" title="Message">Message</a>
to method parameters or use an <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a>
such as using <a href="/confluence/display/CAMEL/XPath" title="XPath">XPath</a>
or <a href="/confluence/display/CAMEL/XQuery" title="XQuery">XQuery</a>.</p>

<p>The method can be invoked in a number of ways as described in the <a href="/confluence/display/CAMEL/Bean+Integration"
title="Bean Integration">Bean Integration</a> such as</p>

<ul>
	<li><a href="/confluence/display/CAMEL/POJO+Producing" title="POJO Producing">POJO
Producing</a></li>
	<li><a href="/confluence/display/CAMEL/Spring+Remoting" title="Spring Remoting">Spring
Remoting</a></li>
	<li><a href="/confluence/display/CAMEL/Bean" title="Bean">Bean</a> component</li>
</ul>


<h4><a name="DynamicRouter-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;">
            <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/Dynamic+Router">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=98539&revisedVersion=3&originalVersion=2">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Dynamic+Router?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message