camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Content Based Routing on Camel
Date Wed, 29 Aug 2012 16:29: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/Content+Based+Routing+on+Camel">Content
Based Routing on Camel</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (0)</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" >{code} <br> <br></td></tr>
            <tr><td class="diff-unchanged" >You can define custom exceptions before
running, even in [Web Console] at runtime. However, We don&#39;t encourage to create new
classes in [Web Console] since that involves non-treatable inner-classes for Groovy Renderer
and you will find it hard to deal with through [Web Console]. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>h3. Simpify your test by
using [Web Console] <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="ContentBasedRoutingonCamel-ContentBasedRoutingonCamel"></a>Content
Based Routing on Camel</h2>
<p>Content based routing (CBR) is considered as a necessary technology for service integration
and has been widely used in current enterprise integration software. XML is a intermediary
message type which provides the mediation between diverse data structures and formats as messages
passing between applications or services. CBR can be thought as a service routing mechanism
that determines a service route by analysing the message content at runtime. Camel support
CBR using the <a href="/confluence/display/CAMEL/Content+Based+Router" title="Content Based
Router">Content Based Router</a> and it defines a set of DSLs to simplify the usage
of this patten .</p>

<p>In <a href="/confluence/display/CAMEL/Load+Balance+for+existing+Messaging+Service"
title="Load Balance for existing Messaging Service">Load Balance for existing Messaging
Service</a> sample, we have used the <a href="/confluence/display/CAMEL/Content+Based+Router"
title="Content Based Router">Content Based Router</a>. This sample will guide you
to creating your processor, registering and using it in <a href="/confluence/display/CAMEL/Web+Console"
title="Web Console">Web Console</a>.</p>

<h3><a name="ContentBasedRoutingonCamel-Buildthesample"></a>Build the sample</h3>

<p>You can build a sample as that in <a href="/confluence/display/CAMEL/Load+Balance+for+existing+Messaging+Service"
title="Load Balance for existing Messaging Service">Load Balance for existing Messaging
Service</a>.</p>

<h3><a name="ContentBasedRoutingonCamel-Createaprocessor"></a>Create a processor</h3>
<p>Now we create a validating processor which will check whether the message has been
expired. If expired, the processor will throw a validation exception. The processor can be
defined as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
/**
 * a processor used to validate the message
 */
<span class="code-keyword">public</span> class ValidatingProcessor <span class="code-keyword">implements</span>
Processor {
    <span class="code-keyword">public</span> void process(Exchange exchange) <span
class="code-keyword">throws</span> Exception {
        <span class="code-object">String</span> body = exchange.getIn().getBody().toString();
        <span class="code-object">String</span>[] tmp = body.split(<span class="code-quote">"&lt;expiredDate&gt;|&lt;/expiredDate&gt;"</span>);
        <span class="code-keyword">if</span> (tmp.length &lt; 3) {
            <span class="code-keyword">throw</span> <span class="code-keyword">new</span>
ValidationException(exchange, <span class="code-quote">"The message has no expired date!"</span>);
        }
        
        SimpleDateFormat sdf = <span class="code-keyword">new</span> SimpleDateFormat(<span
class="code-quote">"MM-dd-yyyy"</span>);
        Date expiredDate = sdf.parse(tmp[1]);
        <span class="code-keyword">if</span> (<span class="code-keyword">new</span>
Date().after(expiredDate)) {
            <span class="code-keyword">throw</span> <span class="code-keyword">new</span>
ValidationException(exchange, <span class="code-quote">"The message has been expired!"</span>);
        }
    }
}
</pre>
</div></div>

<h3><a name="ContentBasedRoutingonCamel-Registertheprocessor"></a>Register
the processor</h3>

<p>We can register the processor in the spring context in two steps.</p>
<ul>
	<li>Add a component-scan package in the applicationContext.xml. Spring will scan the
package for service registry when starting.
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag"><span class="code-comment">&lt;!-- configure the spring
component scan package --&gt;</span></span>
<span class="code-tag">&lt;context:component-scan base-package=<span class="code-quote">"org.apache.camel.web.example"</span>/&gt;</span>
</pre>
</div></div></li>
</ul>


<ul>
	<li>Annotate the processor by adding one line code in the processor. Now the processor
is as follows:
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">package</span> org.apache.camel.web.example;

<span class="code-keyword">import</span> java.text.SimpleDateFormat;
<span class="code-keyword">import</span> java.util.Date;

<span class="code-keyword">import</span> org.apache.camel.Exchange;
<span class="code-keyword">import</span> org.apache.camel.Processor;
<span class="code-keyword">import</span> org.apache.camel.ValidationException;
<span class="code-keyword">import</span> org.springframework.stereotype.Service;

/**
 * a processor used to validate the message
 */
@Service(value = <span class="code-quote">"validatingProcessor"</span>)
<span class="code-keyword">public</span> class ValidatingProcessor <span class="code-keyword">implements</span>
Processor {
    <span class="code-comment">// ommision
</span>}
</pre>
</div></div></li>
</ul>


<p>Now you can run the sample, but it won't do the validation because we haven't configured
the route to pass through the processor. You can complete it by editing the applicationContext.xml
before running it, but here we want to show you how to do it through <a href="/confluence/display/CAMEL/Web+Console"
title="Web Console">Web Console</a> in groovy language.</p>

<h3><a name="ContentBasedRoutingonCamel-UseContentBasedRoutertoorchestratetheprocessors"></a>Use
<a href="/confluence/display/CAMEL/Content+Based+Router" title="Content Based Router">Content
Based Router</a> to orchestrate the processors</h3>

<p>We should execute the validation before processing the message, so we edit the route1
as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"file:src/data?noop=<span class="code-keyword">true</span>"</span>).convertBodyTo(java.lang.<span
class="code-object">String</span>.class)
.processRef(<span class="code-quote">"validatingProcessor"</span>).to(<span
class="code-quote">"stream:out"</span>).to(<span class="code-quote">"activemq:personnel.records"</span>)
</pre>
</div></div>

<p>Now a expired date can be specified in the message, so the validating processor can
filter the expired messages. You can add message files with content:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;person user=<span class="code-quote">"xueqiang"</span>&gt;</span>
  <span class="code-tag">&lt;expiredDate&gt;</span>9-1-2009<span class="code-tag">&lt;/expiredDate&gt;</span>
  <span class="code-tag">&lt;firstName&gt;</span>Xueqiang<span class="code-tag">&lt;/firstName&gt;</span>
  <span class="code-tag">&lt;lastName&gt;</span>Mi<span class="code-tag">&lt;/lastName&gt;</span>
  <span class="code-tag">&lt;city&gt;</span>Shanghai<span class="code-tag">&lt;/city&gt;</span>
<span class="code-tag">&lt;/person&gt;</span>
</pre>
</div></div>
<p>When the the message is out of date, a validation exception is thrown. To make it
a little elegant, you may use doTry...doCatch...doFinally to wrap it up and specify a queue
to store the invalid messages.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"file:src/data?noop=<span class="code-keyword">true</span>"</span>).convertBodyTo(java.lang.<span
class="code-object">String</span>.class)
.doTry().processRef(<span class="code-quote">"validatingProcessor"</span>).to(<span
class="code-quote">"stream:out"</span>).to(<span class="code-quote">"activemq:personnel.records"</span>)
.doCatch(ValidationException.class).to(<span class="code-quote">"mock:invalid"</span>).end()
</pre>
</div></div>

<p>You can define custom exceptions before running, even in <a href="/confluence/display/CAMEL/Web+Console"
title="Web Console">Web Console</a> at runtime. However, We don't encourage to create
new classes in <a href="/confluence/display/CAMEL/Web+Console" title="Web Console">Web
Console</a> since that involves non-treatable inner-classes for Groovy Renderer and
you will find it hard to deal with through <a href="/confluence/display/CAMEL/Web+Console"
title="Web Console">Web Console</a>.</p>

<h3><a name="ContentBasedRoutingonCamel-SimpifyyourtestbyusingWebConsole"></a>Simpify
your test by using <a href="/confluence/display/CAMEL/Web+Console" title="Web Console">Web
Console</a></h3>

<p>To run a test, you can create a file holding the message above and throw it into
the directory. However, <a href="/confluence/display/CAMEL/Web+Console" title="Web Console">Web
Console</a> can help to simplify your operations. Go to the endpoints page by directing
your browser to: <a href="http://localhost:8080/endpoints" class="external-link" rel="nofollow">http://localhost:8080/endpoints</a>
and you can see the <a href="file://src/data?noop=true" class="external-link" rel="nofollow">file://src/data?noop=true</a>
endpoint. Just open it and then select the Send to this endpoint link for inputing your message.
<a href="/confluence/display/CAMEL/Web+Console" title="Web Console">Web Console</a>
will save your message as a file in the directory: src/data, and camel will process the message
file then.</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/Content+Based+Routing+on+Camel">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=2851523&revisedVersion=16&originalVersion=15">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Content+Based+Routing+on+Camel?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message