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 Tue, 19 Jul 2011 05:46: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/~davsclaus">Claus
Ibsen</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" >!http://www.enterpriseintegrationpatterns.com/img/WireTap.gif!
<br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{info:title=Streams}
<br>If you [Wire Tap] a stream message body then you should consider enable [Stream
Caching] to ensure the message body can be re-read. See more details at [Stream Caching] <br>{info}
<br></td></tr>
            <tr><td class="diff-unchanged" > <br>h3. Options <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>The <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 tap location while it is 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 enable <a href="/confluence/display/CAMEL/Stream+caching"
title="Stream caching">Stream caching</a> to ensure the message body can be re-read.
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 endpoint uri where to send the wire tapped message. 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'> Refers to the endpoint where to send the wire tapped message.
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'> Refers to a custom <a href="/confluence/display/CAMEL/Threading+Model"
title="Threading Model">Thread Pool</a> to be used when processing the wire tapped
messages. If not set then Camel uses a default thread pool. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>processorRef</tt> </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> Refers to a custom <a href="/confluence/display/CAMEL/Processor"
title="Processor">Processor</a> to be used for creating a new message (eg 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>: Should a copy of the <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> to used when 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> Refers to 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 if that's needed etc. </td>
</tr>
</tbody></table>
</div>
</div>


<h3><a name="WireTap-WireTapnode"></a>WireTap node</h3>
<p><b>Available as of Camel 2.0</b></p>

<p>In Camel 2.0 we have introduced a new <b>wireTap</b> node for properly
doing wire taps. 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 as a <em>fire and forget</em> style. The tapped <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> is then send in a separate thread so it can run in
parallel with the original </p>

<p>We have extended the <b>wireTap</b> to support two flavors when tapping
an <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a></p>
<ul class="alternate" type="square">
	<li>send a copy of the original <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
(the traditional wire tap)</li>
	<li>send a new <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>,
allowing you to populate the <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
beforehand</li>
</ul>


<h4><a name="WireTap-Sendingacopy%28traditionalwiretap%29"></a>Sending a
copy (traditional wire tap)</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 processor gives you full power how the <a
href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a> is populated
as you can set properties, headers etc. The <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 the new <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> to be sent. You can use the <tt>copy</tt>
option to indicate if you want this or not (default is enabled). If your turn <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> always will be empty.</p>

<p>Below is the processor variation shown. This example is from Camel 2.3, where we
disable <tt>copy</tt> by passing in <tt>false</tt>. This will 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>And 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>. This will 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, notice we use a <b>processorRef</b> attribute to refer
to a spring bean with this 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>And 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>And this variation accesses the body of the original message and creates a new <a
href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a> which is
based on the <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a>.<br/>
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>

<h3><a name="WireTap-Camel1.x"></a>Camel 1.x</h3>
<p>The following example shows how to route a request from an input <b>queue:a</b>
endpoint to the wire tap location <b>queue:tap</b> it is received by <b>queue:b</b></p>

<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">RouteBuilder builder = <span class="code-keyword">new</span>
RouteBuilder() {
    <span class="code-keyword">public</span> void configure() {
        errorHandler(deadLetterChannel(<span class="code-quote">"mock:error"</span>));

        from(<span class="code-quote">"seda:a"</span>)
            .multicast().to(<span class="code-quote">"seda:tap"</span>, <span
class="code-quote">"seda:b"</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;camelContext errorHandlerRef=<span
class="code-quote">"errorHandler"</span> xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
    <span class="code-tag">&lt;route&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"seda:a"</span>/&gt;</span>
        <span class="code-tag">&lt;multicast&gt;</span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"seda:tap"</span>/&gt;</span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"seda:b"</span>/&gt;</span>
        <span class="code-tag">&lt;/multicast&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

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

<p>For another example of this pattern in use you could look at 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 messages using the <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 new headers you would have to use a <a href="/confluence/display/CAMEL/Processor"
title="Processor">Processor</a> for that. So in Camel 2.8 onwards we have improved
this situation so 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 as how you configure the message
body and headers. In XML you use &lt;body&gt; and &lt;setHeader&gt; as shown:</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=22&originalVersion=21">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