camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Intercept
Date Mon, 16 Jan 2012 11:20: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/Intercept">Intercept</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~raulvk">Raul
Kripalani</a>
    </h4>
        <br/>
                         <h4>Changes (1)</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>{tip:title=Conditional
skipping} <br></td></tr>
            <tr><td class="diff-changed-lines" >The combination of <span class="diff-changed-words">{{skip<span
class="diff-added-chars"style="background-color: #dfd;">SendToEndpoint</span>}}</span>
with a {{when}} predicate behaves differently depending on the Camel version: <br></td></tr>
            <tr><td class="diff-unchanged" >* *Before Camel 2.10:* the skipping
is applied unconditionally whether the {{when}} predicate is matched or not, i.e. the {{when}}
predicate only determines whether the body of the interception will execute, but it does not
control skipping behaviour <br>* *As of Camel 2.10:* the skipping only occurs if the
{{when}} predicate is matched, leading to more natural logic altogether. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="Intercept-Intercept"></a>Intercept</h2>

<p>The intercept feature in Camel supports intercepting <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a>s while they are <em>on route</em>. <br/>
We have overhauled the <a href="/confluence/display/CAMEL/Intercept" title="Intercept">Intercept</a>
in Camel 2.0 so the following information is based on Camel 2.0.</p>

<p>Camel supports three kinds of interceptors:</p>
<ul class="alternate" type="square">
	<li><tt>intercept</tt> that intercepts each and every processing step while
routing an <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
in the route.</li>
	<li><tt>interceptFrom</tt> that intercepts incoming <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> in the route.</li>
	<li><tt>interceptSendToEndpoint</tt> that intercepts when an <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> is about to be sent to the given <a href="/confluence/display/CAMEL/Endpoint"
title="Endpoint">Endpoint</a>.</li>
</ul>


<p>These interceptors supports the following features:</p>
<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/Predicate" title="Predicate">Predicate</a>
using <tt>when</tt> to only trigger the interceptor in certain conditions</li>
	<li><tt>stop</tt> forces to stop continue routing the <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> and mark it as completed successful. Camel will by
default <b>not stop</b>.</li>
	<li><tt>skip</tt> when used with <tt>interceptSendToEndpoint</tt>
will <b>skip</b> sending the <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
to the original intended endpoint. Camel will by default <b>not skip</b>.</li>
	<li><tt>interceptFrom</tt> and <tt>interceptSendToEndpoint</tt>
supports endpoint URI matching by: exact uri, wildcard, regular expression. See advanced section.</li>
	<li>The intercepted endpoint uri is stored as message header <tt>Exchange.INTERCEPTED_ENDPOINT</tt>.</li>
</ul>


<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><tt>stop</tt> can
be used in general, it does not have to be used with an <a href="/confluence/display/CAMEL/Intercept"
title="Intercept">Intercept</a> you can use it in regular routing as well.

<p>You can also instruct Camel to <tt>stop</tt> continue routing your message
if you set the <tt>Exchange.ROUTE_STOP</tt> property to <tt>"true"</tt>
or <tt>Boolean.TRUE</tt> on the <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a>. You can for instance do this from regular Java code
in a <a href="/confluence/display/CAMEL/Pojo" title="Pojo">Pojo</a> or <a href="/confluence/display/CAMEL/Processor"
title="Processor">Processor</a>.</p></td></tr></table></div>

<h3><a name="Intercept-Intercept"></a>Intercept</h3>
<p><tt>Intercept</tt> is like a regular interceptor that is applied each
each processing step the <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
undergo while its being routed. You can think of it as a <em>AOP before</em> that
is applied at each DSL keyword you have defined in your route. </p>

<p>The classic Hello World example would be:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
intercept().to(<span class="code-quote">"log:hello"</span>);

from(<span class="code-quote">"jms:queue:order"</span>).to(<span class="code-quote">"bean:validateOrder"</span>).to(<span
class="code-quote">"bean:processOrder"</span>);
</pre>
</div></div>

<p>What happens is that the <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
is intercepted before each processing step, that means that it will be intercepted before</p>
<ul class="alternate" type="square">
	<li><tt>.to("bean:validateOrder")</tt></li>
	<li><tt>.to("bean:processOrder")</tt></li>
</ul>


<p>So in this sample we intercept the <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> twice.</p>

<p>The <tt>when</tt> predicate is also support on the <tt>intercept</tt>
so we can attach a <a href="/confluence/display/CAMEL/Predicate" title="Predicate">Predicate</a>
to only trigger the interception under certain conditions.<br/>
For instance in the sample below we only intercept if the message body contains the string
word <b>Hello</b>:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">intercept().when(body().contains(<span class="code-quote">"Hello"</span>)).to(<span
class="code-quote">"mock:intercepted"</span>);

from(<span class="code-quote">"direct:start"</span>)
    .to(<span class="code-quote">"mock:foo"</span>, <span class="code-quote">"mock:bar"</span>,
<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<p>And in the route below we want to stop in certain conditions, when the message contains
the word 'Hello':</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">intercept().when(body().contains(<span class="code-quote">"Hello"</span>)).to(<span
class="code-quote">"mock:intercepted"</span>).stop();

from(<span class="code-quote">"direct:start"</span>)
    .to(<span class="code-quote">"mock:foo"</span>, <span class="code-quote">"mock:bar"</span>,
<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<h4><a name="Intercept-UsingfromSpringDSL"></a>Using from Spring DSL</h4>
<p>The same hello world sample in Spring DSL would be:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;camelContext ...&gt;</span>
    <span class="code-tag">&lt;intercept&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"log:hello"</span>/&gt;</span>
    <span class="code-tag">&lt;/intercept&gt;</span>

    <span class="code-tag">&lt;route&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"jms:queue:order"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"bean:validateOrder"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"bean:handleOrder"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<p>And the sample for using the <b>when</b> predicate would be:</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>
    &lt;!-- here we intercept each processing step in the routing and do a detour
         routing where we route the exhange to the mock:intercepted endpoint.
         We have applied a when predicate so the interceptor only applies
         if the message body contains the string word 'Hello' --&gt;
    <span class="code-tag">&lt;intercept&gt;</span>
        <span class="code-tag">&lt;when&gt;</span>
            <span class="code-tag">&lt;simple&gt;</span>${in.body} contains
'Hello'<span class="code-tag">&lt;/simple&gt;</span>
        <span class="code-tag">&lt;/when&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:intercepted"</span>/&gt;</span>
    <span class="code-tag">&lt;/intercept&gt;</span>

    <span class="code-tag"><span class="code-comment">&lt;!-- here we have
a very simple route --&gt;</span></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;to uri=<span class="code-quote">"mock:foo"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:bar"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<p>And the sample for using the <b>when</b> and <b>stop</b>
would be:</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;intercept&gt;</span>
        <span class="code-tag"><span class="code-comment">&lt;!-- only trigger
this interceptor if the message body contains the word Hello --&gt;</span></span>
        <span class="code-tag">&lt;when&gt;</span>
            <span class="code-tag">&lt;simple&gt;</span>${in.body} contains
'Hello'<span class="code-tag">&lt;/simple&gt;</span>
        <span class="code-tag">&lt;/when&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:intercepted"</span>/&gt;</span>
        <span class="code-tag"><span class="code-comment">&lt;!-- stop continue
routing --&gt;</span></span>
        <span class="code-tag">&lt;stop/&gt;</span>
    <span class="code-tag">&lt;/intercept&gt;</span>

    <span class="code-tag"><span class="code-comment">&lt;!-- here we have
a very simple route --&gt;</span></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;to uri=<span class="code-quote">"mock:foo"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:bar"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>


<h3><a name="Intercept-InterceptFrom"></a>InterceptFrom</h3>
<p><tt>InterceptFrom</tt> is for intercepting any incoming <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a>, in any route (it intercepts all the <tt>from</tt>
DSLs). This allows you to do some custom behavior for received <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a>s. You can provide a specific uri for a given <a
href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a> then it only
applies for that particular route.</p>

<p>So lets start with the logging example. We want to log all the <b>incoming</b>
requests so we use <tt>interceptFrom</tt> to route to the <a href="/confluence/display/CAMEL/Log"
title="Log">Log</a> component. As <tt>proceed</tt> is default then the
<a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a> will
continue its route, and thus it will continue to <tt>mock:first</tt>.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// intercept all incomming routes
and log it
</span>interceptFrom().to(<span class="code-quote">"log:received"</span>);

<span class="code-comment">// and here we have a couple of routes
</span>from(<span class="code-quote">"direct:start"</span>).to(<span
class="code-quote">"mock:first"</span>).to(<span class="code-quote">"seda:bar"</span>);

from(<span class="code-quote">"seda:bar"</span>).to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<p>You can also attach a <a href="/confluence/display/CAMEL/Predicate" title="Predicate">Predicate</a>
to only trigger if certain conditions is meet. For instance in the route below we intercept
when a test message is send to us, so we can do some custom processing before we continue
routing:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">interceptFrom()
    .when(header(<span class="code-quote">"usertype"</span>).isEqualTo(<span
class="code-quote">"test"</span>))
    .process(<span class="code-keyword">new</span> MyTestServiceProcessor())
    .to(<span class="code-quote">"mock:intercepted"</span>);

<span class="code-comment">// and here is our route
</span>from(<span class="code-quote">"direct:start"</span>).to(<span
class="code-quote">"seda:bar"</span>).to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<p>And if we want to filter out certain messages we can use the <tt>stop()</tt>
to instruct Camel to stop continue routing the <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a>:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">interceptFrom()
    .when(header(<span class="code-quote">"usertype"</span>).isEqualTo(<span
class="code-quote">"test"</span>))
    <span class="code-comment">// here we use stop() to tell Camel to NOT <span class="code-keyword">continue</span>
routing the message.
</span>    <span class="code-comment">// <span class="code-keyword">this</span>
let us act as a filter, to drop certain messages.
</span>    .stop();

<span class="code-comment">// and here is our route
</span>from(<span class="code-quote">"direct:start"</span>).to(<span
class="code-quote">"seda:bar"</span>).to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<p>And if want to only apply a specific endpoint, as the <b>seda:bar</b>
endpoint in the sample below, we can do it like this:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// only trigger when incoming
from seda:bar endpoint
</span>interceptFrom(<span class="code-quote">"seda:bar"</span>).to(<span
class="code-quote">"mock:bar"</span>);

<span class="code-comment">// and here we have a couple of routes
</span>from(<span class="code-quote">"direct:start"</span>).to(<span
class="code-quote">"mock:first"</span>).to(<span class="code-quote">"seda:bar"</span>);

from(<span class="code-quote">"seda:bar"</span>).to(<span class="code-quote">"mock:result"</span>);

from(<span class="code-quote">"seda:foo"</span>).to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>


<h4><a name="Intercept-UsingfromSpringDSL"></a>Using from Spring DSL</h4>
<p>Intercept is of course also available using Spring DSL as shown in the sample below:</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>
    &lt;!-- intercept incoming messages and route them to the mock:middle1 endpoint
         before we proceed and continue routing from the point of interceptions, that
         is mock:end will be the next target --&gt;
    <span class="code-tag">&lt;interceptFrom&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:middle1"</span>/&gt;</span>
    <span class="code-tag">&lt;/interceptFrom&gt;</span>

    <span class="code-tag"><span class="code-comment">&lt;!-- here we have
a very simple route --&gt;</span></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;to uri=<span class="code-quote">"mock:end"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</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>Notice:</b> <tt>stop</tt>
is also supported in <tt>interceptFrom</tt> so you can intercept from certain
endpoints and route then elsewhere and <tt>stop</tt> to not continue routing in
the original intended route path.</td></tr></table></div>

<h3><a name="Intercept-InterceptSendToEndpoint"></a>InterceptSendToEndpoint</h3>
<p><b>Available as of Camel 2.0</b></p>

<p>Intercept send to endpoint is triggered when an <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> is being sent to the intercepted endpoint. This allows
you to route the <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
to a <a href="/confluence/display/CAMEL/Detour" title="Detour">Detour</a> or do
some custom processing before the <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
is sent to the original intended destination. You can also skip sending to the intended destination.
By default Camel will send to the original intended destination after the intercepted route
completes. And as the regular intercept you can also define an <tt>when</tt> <a
href="/confluence/display/CAMEL/Predicate" title="Predicate">Predicate</a> so we
only intercept if the <a href="/confluence/display/CAMEL/Predicate" title="Predicate">Predicate</a>
evaluates to <b>true</b>. This allows you do do a bit of filtering, to only intercept
when certain criteria is meet.</p>

<p>Let start with a simple example, where we want to intercept when an <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> is being sent to <tt>mock:foo</tt>:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// we intercept by endpoint,
that means that whenever an exchange is about to be sent to
</span><span class="code-comment">// <span class="code-keyword">this</span>
endpoint, its intercepted and routed with <span class="code-keyword">this</span>
detour route beforehand
</span><span class="code-comment">// afterwards its send to the original intended
destination. So <span class="code-keyword">this</span> is kinda AOP before.
</span><span class="code-comment">// That means mock:foo will receive the message
(Bye World).
</span>interceptSendToEndpoint(<span class="code-quote">"mock:foo"</span>)
    .to(<span class="code-quote">"mock:detour"</span>).transform(constant(<span
class="code-quote">"Bye World"</span>));

from(<span class="code-quote">"direct:first"</span>)
    .to(<span class="code-quote">"mock:bar"</span>)
    .to(<span class="code-quote">"mock:foo"</span>)
    .to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<p>And this time we add the <a href="/confluence/display/CAMEL/Predicate" title="Predicate">Predicate</a>
so its only when the message body is <tt>Hello World</tt> we intercept.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// we can also attach a predicate
to the endpoint interceptor. So in <span class="code-keyword">this</span> example
the exchange is
</span><span class="code-comment">// only intercepted <span class="code-keyword">if</span>
the body is Hello World
</span>interceptSendToEndpoint(<span class="code-quote">"mock:foo"</span>).when(body().isEqualTo(<span
class="code-quote">"Hello World"</span>))
    .to(<span class="code-quote">"mock:detour"</span>).transform(constant(<span
class="code-quote">"Bye World"</span>));

from(<span class="code-quote">"direct:second"</span>)
    .to(<span class="code-quote">"mock:bar"</span>)
    .to(<span class="code-quote">"mock:foo"</span>)
    .to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<p>And to skip sending to the <tt>mock:foo</tt> endpoint we use the *<tt>skip()</tt>
DSL in the route at the end to instruct Camel to skip sending to the original intended endpoint.
</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// since we use the skipSendToOriginalEndpoint()
we instruct Camel to skip
</span><span class="code-comment">// sending the exchange to the original intended
destination after the intercept
</span><span class="code-comment">// route is complete.
</span><span class="code-comment">// That means that mock:foo will NOT receive
the message, but the message
</span><span class="code-comment">// is skipped and continued in the original
route, so mock:result will receive
</span><span class="code-comment">// the message.
</span>interceptSendToEndpoint(<span class="code-quote">"mock:foo"</span>).skipSendToOriginalEndpoint()
    .transform(constant(<span class="code-quote">"Bye World"</span>)).to(<span
class="code-quote">"mock:detour"</span>);

from(<span class="code-quote">"direct:third"</span>)
    .to(<span class="code-quote">"mock:bar"</span>)
    .to(<span class="code-quote">"mock:foo"</span>)
    .to(<span class="code-quote">"mock:result"</span>);
</pre>
</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>Conditional skipping</b><br
/>The combination of <tt>skipSendToEndpoint</tt> with a <tt>when</tt>
predicate behaves differently depending on the Camel version:
<ul>
	<li><b>Before Camel 2.10:</b> the skipping is applied unconditionally whether
the <tt>when</tt> predicate is matched or not, i.e. the <tt>when</tt>
predicate only determines whether the body of the interception will execute, but it does not
control skipping behaviour</li>
	<li><b>As of Camel 2.10:</b> the skipping only occurs if the <tt>when</tt>
predicate is matched, leading to more natural logic altogether.</li>
</ul>
</td></tr></table></div>

<h4><a name="Intercept-UsingfromSpringDSL"></a>Using from Spring DSL</h4>
<p>Intercept endpoint is of course also available using Spring DSL. </p>

<p>We start with the first example from above in Spring DSL:</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>

    &lt;!-- we intercept by endpoint, that means that whenever an exchange is about to
be sent to
         this endpoint, its intercepted and routed with this detour route beforehand
         afterwards its send to the original intended destination. So this is kinda AOP before.
         That means mock:foo will receive the message (Bye World). --&gt;
    <span class="code-tag">&lt;interceptSendToEndpoint uri=<span class="code-quote">"mock:foo"</span>&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:detour"</span>/&gt;</span>
        <span class="code-tag">&lt;transform&gt;</span>
            <span class="code-tag">&lt;constant&gt;</span>Bye World<span
class="code-tag">&lt;/constant&gt;</span>
        <span class="code-tag">&lt;/transform&gt;</span>
    <span class="code-tag">&lt;/interceptSendToEndpoint&gt;</span>

    <span class="code-tag">&lt;route&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:first"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:bar"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:foo"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<p>And the 2nd. Notice how we can leverage the <a href="/confluence/display/CAMEL/Simple"
title="Simple">Simple</a> language for the <a href="/confluence/display/CAMEL/Predicate"
title="Predicate">Predicate</a>:</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;interceptSendToEndpoint uri=<span class="code-quote">"mock:foo"</span>&gt;</span>
        <span class="code-tag">&lt;when&gt;</span><span class="code-tag">&lt;simple&gt;</span>${body}
== 'Hello World'<span class="code-tag">&lt;/simple&gt;</span><span
class="code-tag">&lt;/when&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:detour"</span>/&gt;</span>
        <span class="code-tag">&lt;transform&gt;</span>
            <span class="code-tag">&lt;constant&gt;</span>Bye World<span
class="code-tag">&lt;/constant&gt;</span>
        <span class="code-tag">&lt;/transform&gt;</span>
    <span class="code-tag">&lt;/interceptSendToEndpoint&gt;</span>

    <span class="code-tag">&lt;route&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:second"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:bar"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:foo"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<p>And the 3rd with the <tt>skip</tt>, notice skip is set with the <tt>skipSendToOriginalEndpoint</tt>
attribute on the <b>interceptSendToEndpoint</b> tag:</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>

    &lt;!-- since we set the skipSendToOriginalEndpoint attribute to true we instruct
         Camel to skip sending the exchange to the original intended destination.
         That means that mock:foo will NOT receive the message, but the message
         is skipped and continued in the original route, so mock:result will receive
         the message. --&gt;
    <span class="code-tag">&lt;interceptSendToEndpoint uri=<span class="code-quote">"mock:foo"</span>
skipSendToOriginalEndpoint=<span class="code-quote">"true"</span>&gt;</span>
        <span class="code-tag">&lt;transform&gt;</span>
            <span class="code-tag">&lt;constant&gt;</span>Bye World<span
class="code-tag">&lt;/constant&gt;</span>
        <span class="code-tag">&lt;/transform&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:detour"</span>/&gt;</span>
    <span class="code-tag">&lt;/interceptSendToEndpoint&gt;</span>

    <span class="code-tag">&lt;route&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:third"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:bar"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:foo"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>


<h3><a name="Intercept-AdvancedusageofIntercpt"></a>Advanced usage of Intercpt</h3>
<p>The <tt>interceptFrom</tt> and <tt>interceptSendToEndpoint</tt>
supports endpoint URI matching by the following rules in the given order:</p>
<ul class="alternate" type="square">
	<li>match by exact URI name. This is the sample we have seen above.</li>
	<li>match by wildcard</li>
	<li>match by regular expression.</li>
</ul>


<p>The real endpoint that was intercepted is stored as uri in the message IN header
with the key <tt>Exchange.INTERCEPTED_ENDPOINT</tt>.<br/>
This allows you to get hold of this information, when you for instance match by wildcard.
Then you know the real endpoint that was intercepted and can react accordingly.</p>

<h4><a name="Intercept-Matchbywildcard"></a>Match by wildcard</h4>
<p>Match by wildcard allows you to match a range of endpoint or all of a given type.
For instance use <tt>uri="file:*"</tt> will match all <a href="/confluence/display/CAMEL/File"
title="File">File</a> based endpoints.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
intercept(<span class="code-quote">"jms:*"</span>).to(<span class="code-quote">"log:fromjms"</span>);
</pre>
</div></div>
<p>Wildcards is match that the text before the * is matched against the given endpoint
and if it also starts with the same characters its a match. For instance you can do:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
intercept(<span class="code-quote">"file:<span class="code-comment">//order/inbox/*"</span>).to(<span
class="code-quote">"log:newfileorders"</span>);</span>
</pre>
</div></div>
<p>To intercept any files received from the <tt>order/inbox</tt> folder.</p>

<h4><a name="Intercept-Matchbyregularexpression"></a>Match by regular expression</h4>
<p>Match by regular expression is just like match by wildcard but using regex instead.
So if we want to intercept incoming messages from gold and silver JMS queues we can do:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
intercept(<span class="code-quote">"jms:queue:(gold|silver)"</span>).to(<span
class="code-quote">"seda:handleFast"</span>);
</pre>
</div></div>

<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>About dynamic
and static behavior of interceptFrom and interceptSendToEndpoint</b><br />The
<tt>interceptSendToEndpoint</tt> is dynamic hence it will also trigger if a dynamic
URI is constructed that Camel was not aware of at startup time.<br/>
The <tt>interceptFrom</tt> is not dynamic as it only intercepts input to routes
registered as routes in <tt>CamelContext</tt>. So if you dynamic construct a <tt>Consumer</tt>
using the Camel API and consumes an <a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a>
then the <tt>interceptFrom</tt> is not triggered.</td></tr></table></div>

<h3><a name="Intercept-SeeAlso"></a>See Also</h3>
<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/Architecture" title="Architecture">Architecture</a></li>
	<li><a href="/confluence/display/CAMEL/AOP" title="AOP">AOP</a></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/Intercept">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=115857&revisedVersion=20&originalVersion=19">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Intercept?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message