<html>
<head>
<base href="http://cwiki.apache.org/confluence">
<link rel="stylesheet" href="/confluence/s/1810/9/1/_/styles/combined.css?spaceKey=CAMEL&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="http://cwiki.apache.org/confluence/display/CAMEL/RoutingSlip+Annotation">RoutingSlip
Annotation</a></h2>
<h4>Page <b>added</b> by <a href="http://cwiki.apache.org/confluence/display/~njiang">willem
jiang</a>
</h4>
<br/>
<div class="notificationGreySide">
<h2><a name="RoutingSlipAnnotation-@RoutingSlipAnnotation"></a>@RoutingSlip
Annotation</h2>
<p>As of Camel 2.4.0 we now support the use of @RoutingSlip on a bean method to easily
create a dynamic <a href="/confluence/display/CAMEL/Routing+Slip" title="Routing Slip">Routing
Slip</a> using a Java method.</p>
<h3><a name="RoutingSlipAnnotation-SimpleExampleusing@Consume"></a>Simple
Example using @Consume</h3>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">package</span> com.acme.foo;
<span class="code-keyword">public</span> class RouterBean {
@Consume(uri = <span class="code-quote">"activemq:foo"</span>)
@RoutingSlip
<span class="code-keyword">public</span> <span class="code-object">String</span>[]
route(<span class="code-object">String</span> body) {
<span class="code-keyword">return</span> <span class="code-keyword">new</span>
<span class="code-object">String</span>[]{<span class="code-quote">"activemq:bar"</span>,
<span class="code-quote">"activemq:whatnot"</span>};
}
}
</pre>
</div></div>
<p>For example if the above bean is configured in <a href="/confluence/display/CAMEL/Spring"
title="Spring">Spring</a> when using a <b><camelContext></b>
element as follows</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag"><?xml version=<span class="code-quote">"1.0"</span>
encoding=<span class="code-quote">"UTF-8"</span>?></span>
<beans xmlns=<span class="code-quote">"http://www.springframework.org/schema/beans"</span>
<span class="code-keyword">xmlns:xsi</span>=<span class="code-quote">"http://www.w3.org/2001/XMLSchema-instance"</span>
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd
">
<span class="code-tag"><camelContext xmlns=<span class="code-quote">"http://activemq.apache.org/camel/schema/spring"</span>/></span>
<span class="code-tag"><bean id=<span class="code-quote">"myRecipientList"</span>
class=<span class="code-quote">"com.acme.foo.RouterBean"</span>/></span>
<span class="code-tag"></beans></span>
</pre>
</div></div>
<p>then a route will be created consuming from the <b>foo</b> queue on the
<a href="/confluence/display/CAMEL/ActiveMQ" title="ActiveMQ">ActiveMQ</a> component
which when a message is received the message will be forwarded to the endpoints defined by
the result of this method call - namely the <b>bar</b> and <b>whatnot</b>
queues.</p>
<h3><a name="RoutingSlipAnnotation-Howitworks"></a>How it works</h3>
<p>The return value of the @RoutingSlip method is converted to either a java.util.Collection
/ java.util.Iterator or array of objects where each element is converted to an <a href="/confluence/display/CAMEL/Endpoint"
title="Endpoint">Endpoint</a> or a String, or if you are only going to route to a
single endpoint then just return either an Endpoint object or an object that can be converted
to a String. So the following methods are all valid </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
@RoutingSlip
<span class="code-keyword">public</span> <span class="code-object">String</span>[]
route(<span class="code-object">String</span> body) { ... }
@RoutingSlip
<span class="code-keyword">public</span> List<<span class="code-object">String</span>>
route(<span class="code-object">String</span> body) { ... }
@RoutingSlip
<span class="code-keyword">public</span> Endpoint route(<span class="code-object">String</span>
body) { ... }
@RoutingSlip
<span class="code-keyword">public</span> Endpoint[] route(<span class="code-object">String</span>
body) { ... }
@RoutingSlip
<span class="code-keyword">public</span> Collection<Endpoint> route(<span
class="code-object">String</span> body) { ... }
@RoutingSlip
<span class="code-keyword">public</span> URI route(<span class="code-object">String</span>
body) { ... }
@RecipientList
<span class="code-keyword">public</span> URI[] route(<span class="code-object">String</span>
body) { ... }
</pre>
</div></div>
<p>Then for each endpoint or URI the message is forwarded a separate copy to that endpoint.
</p>
<p>You can then use whatever Java code you wish to figure out what endpoints to route
to; for example you can use the <a href="/confluence/display/CAMEL/Bean+Binding" title="Bean
Binding">Bean Binding</a> annotations to inject parts of the message body or headers
or use <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a>
values on the message.</p>
<h3><a name="RoutingSlipAnnotation-MoreComplexExampleUsingDSL"></a>More
Complex Example Using DSL</h3>
<p>In this example we will use more complex <a href="/confluence/display/CAMEL/Bean+Binding"
title="Bean Binding">Bean Binding</a>, plus we will use a separate route to invoke
the <a href="/confluence/display/CAMEL/Recipient+List" title="Recipient List">Recipient
List</a></p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">public</span> class RouterBean2 {
@RoutingSlip
<span class="code-keyword">public</span> <span class="code-object">String</span>
route(@Header(<span class="code-quote">"customerID"</span>) <span class="code-object">String</span>
custID <span class="code-object">String</span> body) {
<span class="code-keyword">if</span> (custID == <span class="code-keyword">null</span>)
<span class="code-keyword">return</span> <span class="code-keyword">null</span>;
<span class="code-keyword">return</span> <span class="code-quote">"activemq:Customers.Orders."</span>
+ custID;
}
}
<span class="code-keyword">public</span> class MyRouteBuilder <span class="code-keyword">extends</span>
RouteBuilder {
<span class="code-keyword">protected</span> void configure() {
from(<span class="code-quote">"activemq:Orders.Incoming"</span>).routingSlip(bean(<span
class="code-quote">"myRouterBean"</span>, <span class="code-quote">"route"</span>));
}
}
</pre>
</div></div>
<p>Notice how we are injecting some headers or expressions and using them to determine
the recipients using <a href="/confluence/display/CAMEL/Routing+Slip" title="Routing Slip">Routing
Slip</a> EIP.<br/>
See the <a href="/confluence/display/CAMEL/Bean+Integration" title="Bean Integration">Bean
Integration</a> for more details.</p>
</div>
<div id="commentsSection" class="wiki-content pageSection">
<div style="float: right;">
<a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
</div>
<a href="http://cwiki.apache.org/confluence/display/CAMEL/RoutingSlip+Annotation">View
Online</a>
|
<a href="http://cwiki.apache.org/confluence/display/CAMEL/RoutingSlip+Annotation?showComments=true&showCommentArea=true#addcomment">Add
Comment</a>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
|