camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Using getIn or getOut methods on Exchange
Date Tue, 27 Mar 2012 08:00: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/Using+getIn+or+getOut+methods+on+Exchange">Using
getIn or getOut methods on Exchange</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        try to fix the image on the static webpage<br />
    </div>
        <br/>
                         <h4>Changes (2)</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" >h2. Flow of an exchange through a
route <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">!Message
flow in Route.png! <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">!flow.png!
<br></td></tr>
            <tr><td class="diff-unchanged" > <br>- The out message from
each step is used as the in message for th next step <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="UsinggetInorgetOutmethodsonExchange-MesssageexchangepatternsandtheExchangeobject"></a>Messsage
exchange patterns and the Exchange object</h2>

<p>The Camel API is influenced by APIs such as <a href="http://en.wikipedia.org/wiki/Java_Business_Integration"
class="external-link" rel="nofollow">JBI specification</a>, <a href="http://cxf.apache.org/"
class="external-link" rel="nofollow">CXF</a> which defines a concept called Message
Exchange Patterns (MEP for short).</p>

<p>The MEP defines the messaging style used such as one-way (<a href="/confluence/display/CAMEL/Event+Message"
title="Event Message">InOnly</a>) or request-reply (<a href="/confluence/display/CAMEL/Request+Reply"
title="Request Reply">InOut</a>),<br/>
which means you have IN and optionally OUT messages. This closely maps to other APIs such
as WS, WSDL, REST, JBI and the likes.</p>

<p>The <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
API provides two methods to get a message, either <tt>getIn</tt> or <tt>getOut</tt>.<br/>
Obviously the <tt>getIn</tt> gets the IN message, and the <tt>getOut</tt>
gets the OUT message.</p>

<h2><a name="UsinggetInorgetOutmethodsonExchange-Flowofanexchangethrougharoute"></a>Flow
of an exchange through a route</h2>

<p><span class="image-wrap" style=""><img src="/confluence/download/attachments/23338837/flow.png?version=1&amp;modificationDate=1332835106588"
style="border: 0px solid black" /></span></p>

<ul class="alternate" type="square">
	<li>The out message from each step is used as the in message for th next step</li>
	<li>if there is no out message then the in message is used instead</li>
	<li>For the InOut MEP the out from the last step in the route is returned to the producer.
In case of InOnly the last out is thrown away</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><b>Beware of getOut to check
if there is an out message</b><br />exchange.getOut creates an out message if
there is none. So if you want to check if there is an out message then you should use exchange.hasOut
instead</td></tr></table></div>

<h2><a name="UsinggetInorgetOutmethodsonExchange-UsinggetInorgetOutmethodsonExchange"></a>Using
getIn or getOut methods on Exchange</h2>

<p>Now suppose you want to use a Camel <a href="/confluence/display/CAMEL/Processor"
title="Processor">Processor</a> to adjust a message. This can be done as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<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(<span
class="code-object">String</span>.class);
   <span class="code-comment">// change the message to say Hello
</span>   exchange.getOut().setBody(<span class="code-quote">"Hello "</span>
+ body);
}
</pre>
</div></div>

<p>This seems intuitive and is what you would expect is the <em>right</em>
approach to change a message from a <a href="/confluence/display/CAMEL/Processor" title="Processor">Processor</a>.<br/>
However there is an big issue - the <tt>getOut</tt> method will create a new <a
href="/confluence/display/CAMEL/Message" title="Message">Message</a>, which means
any other information<br/>
from the IN message will not be propagated; which means you will lose that data.<br/>
To remedy this we'll have to copy the data which is done as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<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(<span
class="code-object">String</span>.class);
   <span class="code-comment">// change the message to say Hello
</span>   exchange.getOut().setBody(<span class="code-quote">"Hello "</span>
+ body);
   <span class="code-comment">// copy headers from IN to OUT to propagate them
</span>   exchange.getOut().setHeaders(exchange.getIn().getHeaders());
}
</pre>
</div></div>

<p>Well that is not all, a <a href="/confluence/display/CAMEL/Message" title="Message">Message</a>
can also contain attachments so to be sure you need to propagate those as well:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<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(<span
class="code-object">String</span>.class);
   <span class="code-comment">// change the message to say Hello
</span>   exchange.getOut().setBody(<span class="code-quote">"Hello "</span>
+ body);
   <span class="code-comment">// copy headers from IN to OUT to propagate them
</span>   exchange.getOut().setHeaders(exchange.getIn().getHeaders();
   <span class="code-comment">// copy attachements from IN to OUT to propagate them
</span>   exchange.getOut().setAttachments(exchange.getIn().getAttachments());
}
</pre>
</div></div>

<p>Now we ensure that all additional data is propagated on the new OUT message. But
its a shame we need 2 code lines to ensure data is propagated.<br/>
What you can do instead is to change the IN message instead, as shown below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<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(<span
class="code-object">String</span>.class);
   <span class="code-comment">// change the existing message to say Hello
</span>   exchange.getIn().setBody(<span class="code-quote">"Hello "</span>
+ body);
}
</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>Consider using getIn</b><br
/>As shown above you most often need to alter the existing IN message, than creating a
totally new OUT message.<br/>
And therefore it's often easier just to adjust the IN message directly.</td></tr></table></div>

<p>Changing the IN message directly is possible in Camel as it don't mind. Camel will
detect that the <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
has no OUT message<br/>
and therefore use the IN message instead.</p>

<h3><a name="UsinggetInorgetOutmethodsonExchange-AboutMessageExchangePatternandgetOut"></a>About
Message Exchange Pattern and getOut</h3>

<p>If the <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
is using <tt>InOnly</tt> as the MEP, then you may think that the <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> has no OUT message.<br/>
But you can still invoke the <tt>getOut</tt> method on <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a>; Camel will not barf.</p>

<p>So the example code above is possible for any kind of MEP. The MEP is <em>just</em>
a flag on the <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
which the Consumer and Producer adhere to.<br/>
You can change the MEP on the <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
using the <tt>setPattern</tt> method. And likewise there is DSL to change it as
well.</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/Using+getIn+or+getOut+methods+on+Exchange">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=23338837&revisedVersion=12&originalVersion=11">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Using+getIn+or+getOut+methods+on+Exchange?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message