camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Wire Tap
Date Wed, 15 Feb 2012 22:36: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/Wire+Tap">Wire
Tap</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~mazzag">Glen
Mazza</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        Clarified WireTap options a bit.<br />
    </div>
        <br/>
                         <h4>Changes (5)</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" >h3. WireTap node <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">You
can also use a *wireTap* node to facilitate wiretaps. Camel will copy the original [Exchange]
and set its [Exchange Pattern] to *InOnly*, as we want the tapped [Exchange] to be sent in
a _fire and forget_ style. The tapped [Exchange] is then sent in a separate thread so it can
run in parallel with the original.   <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">Camel&#39;s
WireTap node supports two flavors when tapping an [Exchange]: <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">We
have extended *wireTap* to support two flavors when tapping an [Exchange]:  <br>- send
a copy of the original [Exchange] (the traditional wiretap)  <br>- send a new [Exchange],
allowing you to populate it beforehand  <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">-With
the traditional WireTap, Camel will copy the original [Exchange] and set its [Exchange Pattern]
to *InOnly*, as we want the tapped [Exchange] to be sent in a _fire and forget_ style. The
tapped [Exchange] is then sent in a separate thread so it can run in parallel with the original.
<br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">-Camel
also provides an option of sending a new [Exchange] allowing you to populate it with new values
<br> <br></td></tr>
            <tr><td class="diff-unchanged" >h4. Sending a copy (traditional wiretap)
<br>*Using the [Fluent Builders]* <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h3><a name="WireTap-WireTap"></a>Wire Tap</h3>

<p><a href="http://www.enterpriseintegrationpatterns.com/WireTap.html" class="external-link"
rel="nofollow">Wire Tap</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 separate location while they are being forwarded to the ultimate destination.</p>

<p><span class="image-wrap" style=""><img src="http://www.enterpriseintegrationpatterns.com/img/WireTap.gif"
style="border: 0px solid black" /></span></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>Streams</b><br
/>If you <a href="/confluence/display/CAMEL/Wire+Tap" title="Wire Tap">Wire Tap</a>
a stream message body then you should consider enabling <a href="/confluence/display/CAMEL/Stream+caching"
title="Stream caching">Stream caching</a> to ensure the message body can be read
at each endpoint. See more details at <a href="/confluence/display/CAMEL/Stream+caching"
title="Stream caching">Stream caching</a>.</td></tr></table></div>

<h3><a name="WireTap-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>uri</tt> </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> The URI of the endpoint to which the wire-tapped message will
be sent. You should use either <tt>uri</tt> or <tt>ref</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>ref</tt> </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> Reference identifier of the endpoint to which the wire-tapped
message will be sent. You should use either <tt>uri</tt> or <tt>ref</tt>.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>executorServiceRef</tt> </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> Reference identifier of a custom <a href="/confluence/display/CAMEL/Threading+Model"
title="Threading Model">Thread Pool</a> to use when processing the wire-tapped messages.
If not set, Camel will use a default thread pool. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>processorRef</tt> </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> Reference identifier of a custom <a href="/confluence/display/CAMEL/Processor"
title="Processor">Processor</a> to use for creating a new message (e.g., the "send
a new message" mode). See below. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>copy</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> <b>Camel 2.3</b>: Whether to copy the <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> before wire-tapping the message. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>onPrepareRef</tt> </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> <b>Camel 2.8:</b> Reference identifier of 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>
to be wire-tapped. This allows you to do any custom logic, such as deep-cloning the message
payload. </td>
</tr>
</tbody></table>
</div>
</div>


<h3><a name="WireTap-WireTapthreadpool"></a>WireTap thread pool</h3>

<p>The <a href="/confluence/display/CAMEL/Wire+Tap" title="Wire Tap">Wire Tap</a>
uses a thread pool to process the tapped messages. This thread pool will by default use the
settings detailed at <a href="/confluence/display/CAMEL/Threading+Model" title="Threading
Model">Threading Model</a>.  In particular, when the pool is exhausted (with all
threads utilized), further wiretaps will be executed synchronously by the calling thread.
 To remedy this, you can configure an explicit thread pool on the <a href="/confluence/display/CAMEL/Wire+Tap"
title="Wire Tap">Wire Tap</a> having either a different rejection policy, a larger
worker queue, or more worker threads.</p>

<h3><a name="WireTap-WireTapnode"></a>WireTap node</h3>

<p>Camel's WireTap node supports two flavors when tapping an <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a>:</p>

<p>-With the traditional WireTap, Camel will copy the original <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> and set its <a href="/confluence/display/CAMEL/Exchange+Pattern"
title="Exchange Pattern">Exchange Pattern</a> to <b>InOnly</b>, as we
want the tapped <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
to be sent in a <em>fire and forget</em> style. The tapped <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> is then sent in a separate thread so it can run in
parallel with the original.</p>

<p>-Camel also provides an option of sending a new <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> allowing you to populate it with new values</p>

<h4><a name="WireTap-Sendingacopy%28traditionalwiretap%29"></a>Sending a
copy (traditional wiretap)</h4>
<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">
<pre class="code-java">from(<span class="code-quote">"direct:start"</span>)
    .to(<span class="code-quote">"log:foo"</span>)
    .wireTap(<span class="code-quote">"direct:tap"</span>)
    .to(<span class="code-quote">"mock:result"</span>);
</pre>
</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">
<pre class="code-xml"><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">"log:foo"</span>/&gt;</span>
    <span class="code-tag">&lt;wireTap uri=<span class="code-quote">"direct:tap"</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>
</pre>
</div></div>

<h4><a name="WireTap-SendinganewExchange"></a>Sending a new <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a></h4>
<p><b>Using the <a href="/confluence/display/CAMEL/Fluent+Builders" title="Fluent
Builders">Fluent Builders</a></b><br/>
Camel supports either a processor or an <a href="/confluence/display/CAMEL/Expression"
title="Expression">Expression</a> to populate the new <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a>. Using a processor gives you full power over how the
<a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a> is
populated as you can set properties, headers, et cetera. An <a href="/confluence/display/CAMEL/Expression"
title="Expression">Expression</a> can only be used to set the IN body. </p>

<p>From <b>Camel 2.3</b> onwards the <a href="/confluence/display/CAMEL/Expression"
title="Expression">Expression</a> or <a href="/confluence/display/CAMEL/Processor"
title="Processor">Processor</a> is pre-populated with a copy of the original <a
href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>, which allows
you to access the original message when you prepare a new <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> to be sent. You can use the <tt>copy</tt>
option (enabled by default) to indicate whether you want this.  If you set <tt>copy=false</tt>,
then it works as in Camel 2.2 or older where the <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> will be empty.</p>

<p>Below is the processor variation. This example is from Camel 2.3, where we disable
<tt>copy</tt> by passing in <tt>false</tt> to create a new, empty
<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">from(<span class="code-quote">"direct:start"</span>)
    .wireTap(<span class="code-quote">"direct:foo"</span>, <span class="code-keyword">false</span>,
<span class="code-keyword">new</span> Processor() {
        <span class="code-keyword">public</span> void process(Exchange exchange)
<span class="code-keyword">throws</span> Exception {
            exchange.getIn().setBody(<span class="code-quote">"Bye World"</span>);
            exchange.getIn().setHeader(<span class="code-quote">"foo"</span>,
<span class="code-quote">"bar"</span>);
        }
    }).to(<span class="code-quote">"mock:result"</span>);


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

<p>Here is the <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a>
variation. This example is from Camel 2.3, where we disable <tt>copy</tt> by passing
in <tt>false</tt> to create a new, empty <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">from(<span class="code-quote">"direct:start"</span>)
    .wireTap(<span class="code-quote">"direct:foo"</span>, <span class="code-keyword">false</span>,
constant(<span class="code-quote">"Bye World"</span>))
    .to(<span class="code-quote">"mock:result"</span>);

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

<p><b>Using the <a href="/confluence/display/CAMEL/Spring+XML+Extensions" title="Spring
XML Extensions">Spring XML Extensions</a></b><br/>
The processor variation, which uses a <b>processorRef</b> attribute to refer to
a Spring bean by ID: </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;route&gt;</span>
    <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start2"</span>/&gt;</span>
    <span class="code-tag">&lt;wireTap uri=<span class="code-quote">"direct:foo"</span>
processorRef=<span class="code-quote">"myProcessor"</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>
</pre>
</div></div>

<p>Here is the <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a>
variation, where the expression is defined in the <b>body</b> tag: </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><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;wireTap uri=<span class="code-quote">"direct:foo"</span>&gt;</span>
        <span class="code-tag">&lt;body&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;/body&gt;</span>
    <span class="code-tag">&lt;/wireTap&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>
</pre>
</div></div>

<p>This variation accesses the body of the original message and creates a new <a
href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a> based on
the <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a>.
 It will create a new Exchange and have the body contain <tt>"Bye ORIGINAL BODY MESSAGE
HERE"</tt></p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><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;wireTap uri=<span class="code-quote">"direct:foo"</span>&gt;</span>
        <span class="code-tag">&lt;body&gt;</span><span class="code-tag">&lt;simple&gt;</span>Bye
${body}<span class="code-tag">&lt;/simple&gt;</span><span class="code-tag">&lt;/body&gt;</span>
    <span class="code-tag">&lt;/wireTap&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>
</pre>
</div></div>

<h4><a name="WireTap-FurtherExample"></a>Further Example</h4>

<p>For another example of this pattern, refer to the <a href="http://svn.apache.org/repos/asf/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/WireTapTest.java"
class="external-link" rel="nofollow">wire tap test case</a>.</p>

<h3><a name="WireTap-SendinganewExchangeandsetheadersinDSL"></a>Sending
a new <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
and set headers in DSL</h3>
<p><b>Available as of Camel 2.8</b></p>

<p>If you send a new message using <a href="/confluence/display/CAMEL/Wire+Tap" title="Wire
Tap">Wire Tap</a>, then you could only set the message body using an <a href="/confluence/display/CAMEL/Expression"
title="Expression">Expression</a> from the DSL. If you also need to set headers,
you would have to use a <a href="/confluence/display/CAMEL/Processor" title="Processor">Processor</a>.
In Camel 2.8 onwards, you can now set headers as well in the DSL.</p>

<p>The following example sends a new message which has</p>
<ul class="alternate" type="square">
	<li>"Bye World" as message body</li>
	<li>a header with key "id" with the value 123</li>
	<li>a header with key "date" which has current date as value</li>
</ul>


<h4><a name="WireTap-JavaDSL"></a>Java DSL</h4>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:start"</span>)
    <span class="code-comment">// tap a <span class="code-keyword">new</span>
message and send it to direct:tap
</span>    <span class="code-comment">// the <span class="code-keyword">new</span>
message should be Bye World with 2 headers
</span>    .wireTap(<span class="code-quote">"direct:tap"</span>)
        <span class="code-comment">// create the <span class="code-keyword">new</span>
tap message body and headers
</span>        .newExchangeBody(constant(<span class="code-quote">"Bye World"</span>))
        .newExchangeHeader(<span class="code-quote">"id"</span>, constant(123))
        .newExchangeHeader(<span class="code-quote">"date"</span>, simple(<span
class="code-quote">"${date:now:yyyyMMdd}"</span>))
    .end()
    <span class="code-comment">// here we <span class="code-keyword">continue</span>
routing the original messages
</span>    .to(<span class="code-quote">"mock:result"</span>);

<span class="code-comment">// <span class="code-keyword">this</span> is
the tapped route
</span>from(<span class="code-quote">"direct:tap"</span>)
    .to(<span class="code-quote">"mock:tap"</span>);
</pre>
</div></div>

<h4><a name="WireTap-XMLDSL"></a>XML DSL</h4>
<p>The XML DSL is slightly different than Java DSL in how you configure the message
body and headers using &lt;body&gt; and &lt;setHeader&gt;:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><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"><span class="code-comment">&lt;!-- tap a new message
and send it to direct:tap --&gt;</span></span>
    <span class="code-tag"><span class="code-comment">&lt;!-- the new message
should be Bye World with 2 headers --&gt;</span></span>
    <span class="code-tag">&lt;wireTap uri=<span class="code-quote">"direct:tap"</span>&gt;</span>
        <span class="code-tag"><span class="code-comment">&lt;!-- create the
new tap message body and headers --&gt;</span></span>
        <span class="code-tag">&lt;body&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;/body&gt;</span>
        <span class="code-tag">&lt;setHeader headerName=<span class="code-quote">"id"</span>&gt;</span><span
class="code-tag">&lt;constant&gt;</span>123<span class="code-tag">&lt;/constant&gt;</span><span
class="code-tag">&lt;/setHeader&gt;</span>
        <span class="code-tag">&lt;setHeader headerName=<span class="code-quote">"date"</span>&gt;</span><span
class="code-tag">&lt;simple&gt;</span>${date:now:yyyyMMdd}<span class="code-tag">&lt;/simple&gt;</span><span
class="code-tag">&lt;/setHeader&gt;</span>
    <span class="code-tag">&lt;/wireTap&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- here we continue
routing the original message --&gt;</span></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>
</pre>
</div></div>


<h3><a name="WireTap-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="WireTap-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/Wire+Tap">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=49214&revisedVersion=26&originalVersion=25">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Wire+Tap?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message