camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > NMR
Date Wed, 25 May 2011 09:33: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/NMR">NMR</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~cmoulliard">charles
Moulliard</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        Add more examples + unit test<br />
    </div>
        <br/>
                         <h4>Changes (8)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-unchanged" >h2. NMR Component <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">The
*nmr* component is an adapter to the Normalized Message Router (NMR) in [ServiceMix|http://servicemix.apache.org/home.html],
which is intended for use by Camel applications deployed directly into the OSGi container.
By contrast, the [JBI] component is intended for use by Camel applications deployed into the
ServiceMix JBI container. <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">The
*nmr* component is an adapter to the Normalized Message Router (NMR) in [ServiceMix|http://servicemix.apache.org/home.html],
which is intended for use by Camel applications deployed directly into the OSGi container.
You can exchange objects with NMR and not only XML like this is the case with the JBI specification.
The interest of this component is that you can interconnect camel routes deployed in different
OSGI bundles.  <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">By
contrast, the [JBI] component is intended for use by Camel applications deployed into the
ServiceMix JBI container. <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h3. Installing <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >h4. Examples <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">Consumer
<br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">from(&quot;nmr:MyServiceEndpoint&quot;)
<br>from(&quot;nmr:MyServiceEndpoint?synchronous=true&quot;).to(&quot;nmr:AnotherEndpoint&quot;)
<br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">from(&quot;nmr:MyServiceEndpoint&quot;)
// consume nmr exchanges asynchronously <br>from(&quot;nmr:MyServiceEndpoint?synchronous=true&quot;).to()
// consume nmr exchanges synchronously and use the same thread as defined by NMR ThreadPool
<br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">Producer
<br>{code} <br>from()...to(&quot;nmr:MyServiceEndpoint&quot;) // produce
nmr exchanges asynchronously <br>from()...to(&quot;nmr:MyServiceEndpoint?timeout=10000&quot;)
// produce nmr exchanges synchronously and wait till 10s to receive response <br>{code}
<br> <br></td></tr>
            <tr><td class="diff-unchanged" >h3. Using Stream bodies <br>If
you are using a stream type as the message body, you should be aware that a stream is only
capable of being read once. So if you enable {{DEBUG}} logging, the body is usually logged
and thus read. To deal with this, Camel has a {{streamCaching}} option that can cache the
stream, enabling you to read it multiple times. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >In *Camel 2.0* we store big input
streams (by default, over 64K) in a {{temp}} file using {{CachedOutputStream}}. When you close
the input stream, the temp file will be deleted. <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h4.
Testing <br>NMR camel routes can be tested using the camel unit test approach even if
they will be deployed next in different bundles on an OSGI runtime. With this aim in view,
you will extend the ServiceMixNMR Mock class {{org.apache.servicemix.camel.nmr.AbstractComponentClass}}
which will create a NMR bus, register the Camel NMR Component and the endpoints defined into
the Camel routes. <br> <br>{code} <br>public class ExchangeUsingNMRTest
extends AbstractComponentTest { <br> <br>    @Test <br>    public void testProcessing()
throws InterruptedException { <br>        MockEndpoint mock = getMockEndpoint(&quot;mock:simple&quot;);
<br>        mock.expectedBodiesReceived(&quot;Simple message body&quot;); <br>
<br>        template.sendBody(&quot;direct:simple&quot;, &quot;Simple message
body&quot;); <br> <br>        assertMockEndpointsSatisfied(); <br> <br>
   } <br> <br>    @Override <br>    protected RouteBuilder createRouteBuilder()
throws Exception { <br>        return new RouteBuilder() { <br> <br>   
        @Override <br>            public void configure() throws Exception { <br>
               from(&quot;direct:simple&quot;).to(&quot;nmr:simple&quot;);
<br>                from(&quot;nmr:simple?synchronous=true&quot;).to(&quot;mock:simple&quot;);
<br>            } <br>        }; <br>    } <br>} <br>{code}
<br> <br> <br></td></tr>
            <tr><td class="diff-unchanged" >{include:Endpoint See Also} <br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="NMR-NMRComponent"></a>NMR Component</h2>

<p>The <b>nmr</b> component is an adapter to the Normalized Message Router
(NMR) in <a href="http://servicemix.apache.org/home.html" class="external-link" rel="nofollow">ServiceMix</a>,
which is intended for use by Camel applications deployed directly into the OSGi container.
You can exchange objects with NMR and not only XML like this is the case with the JBI specification.
The interest of this component is that you can interconnect camel routes deployed in different
OSGI bundles. </p>

<p>By contrast, the <a href="/confluence/display/CAMEL/JBI" title="JBI">JBI</a>
component is intended for use by Camel applications deployed into the ServiceMix JBI container.</p>

<h3><a name="NMR-Installing"></a>Installing</h3>

<p>The NMR component is provided with Apache ServiceMix. It is <b>not</b>
distributed with Camel. To install the NMR component in ServiceMix, enter the following command
in the ServiceMix console window:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
features install nmr
</pre>
</div></div>

<p>You also need to instantiate the NMR component. You can do this by editing your Spring
configuration file, <tt>META-INF/spring/*.xml</tt>, and adding the following <tt>bean</tt>
instance:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;beans <span class="code-keyword">xmlns:osgi</span>=<span
class="code-quote">"http://www.springframework.org/schema/osgi"</span> ... &gt;</span>
    ...
    <span class="code-tag">&lt;bean id=<span class="code-quote">"nmr"</span>
class=<span class="code-quote">"org.apache.servicemix.camel.nmr.ServiceMixComponent"</span>&gt;</span>
        <span class="code-tag">&lt;property name=<span class="code-quote">"nmr"</span>&gt;</span>
            <span class="code-tag">&lt;osgi:reference interface=<span class="code-quote">"org.apache.servicemix.nmr.api.NMR"</span>
/&gt;</span>
        <span class="code-tag">&lt;/property&gt;</span>
    <span class="code-tag">&lt;/bean&gt;</span>
    ...
<span class="code-tag">&lt;/beans&gt;</span>
</pre>
</div></div>

<h3><a name="NMR-NMRconsumerandproducerendpoints"></a>NMR consumer and producer
endpoints</h3>

<p>The following code:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"nmr:MyServiceEndpoint"</span>)
</pre>
</div></div>

<p>Automatically exposes a new endpoint to the bus with endpoint name <tt>MyServiceEndpoint</tt>
(see <a href="#NMR-URIformat">URI&#45;format</a>).</p>

<p>When an NMR endpoint appears at the end of a route, for example:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
to(<span class="code-quote">"nmr:MyServiceEndpoint"</span>)
</pre>
</div></div>

<p>The messages sent by this producer endpoint are sent to the already deployed JBI
endpoint.</p>

<p><a name="NMR-URIformat"></a></p>
<h3><a name="NMR-URIformat"></a>URI format</h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
nmr:endpointName
</pre>
</div></div>

<h4><a name="NMR-URIOptions"></a>URI Options</h4>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Option </th>
<th class='confluenceTh'> Default Value </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>runAsSubject</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Apache ServiceMix 4.4:</b> When this is set
to <b><tt>true</tt></b> on a consumer endpoint, the endpoint will
be invoked on behalf of the <tt>Subject</tt> that is set on the <tt>Exchange</tt>
(i.e. the call to <tt>Subject.getSubject(AccessControlContext)</tt> will return
the <tt>Subject</tt> instance) </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>synchronous</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> When this is set to <b><tt>true</tt></b>
on a consumer endpoint, an incoming, synchronous NMR Exchange will be handled on the sender's
thread instead of being handled on a new thread of the NMR endpoint's thread pool </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>timeout</tt> </td>
<td class='confluenceTd'> <tt>0</tt> </td>
<td class='confluenceTd'> <b>Apache ServiceMix 4.4:</b> When this is set
to a value greater than 0, the producer endpoint will timeout if it doesn't receive a response
from the NMR within the given timeout period (in milliseconds).  Configuring a timeout value
will switch to using synchronous interactions with the NMR instead of the usual asynchronous
messaging. </td>
</tr>
</tbody></table>
</div>
</div>

<h4><a name="NMR-Examples"></a>Examples</h4>

<p>Consumer</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"nmr:MyServiceEndpoint"</span>) <span class="code-comment">//
consume nmr exchanges asynchronously
</span>from(<span class="code-quote">"nmr:MyServiceEndpoint?synchronous=<span
class="code-keyword">true</span>"</span>).to() <span class="code-comment">//
consume nmr exchanges synchronously and use the same thread as defined by NMR ThreadPool</span>
</pre>
</div></div>

<p>Producer</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from()...to(<span class="code-quote">"nmr:MyServiceEndpoint"</span>) <span
class="code-comment">// produce nmr exchanges asynchronously
</span>from()...to(<span class="code-quote">"nmr:MyServiceEndpoint?timeout=10000"</span>)
<span class="code-comment">// produce nmr exchanges synchronously and wait till 10s
to receive response</span>
</pre>
</div></div>

<h3><a name="NMR-UsingStreambodies"></a>Using Stream bodies</h3>
<p>If you are using a stream type as the message body, you should be aware that a stream
is only capable of being read once. So if you enable <tt>DEBUG</tt> logging, the
body is usually logged and thus read. To deal with this, Camel has a <tt>streamCaching</tt>
option that can cache the stream, enabling you to read it multiple times.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"nmr:MyEndpoint"</span>).streamCaching().to(<span
class="code-quote">"xslt:transform.xsl"</span>, <span class="code-quote">"bean:doSomething"</span>);
</pre>
</div></div>

<p>From <b>Camel 1.5</b> onwards, the stream caching is default enabled,
so it is not necessary to set the <tt>streamCaching()</tt> option.<br/>
In <b>Camel 2.0</b> we store big input streams (by default, over 64K) in a <tt>temp</tt>
file using <tt>CachedOutputStream</tt>. When you close the input stream, the temp
file will be deleted.</p>

<h4><a name="NMR-Testing"></a>Testing</h4>
<p>NMR camel routes can be tested using the camel unit test approach even if they will
be deployed next in different bundles on an OSGI runtime. With this aim in view, you will
extend the ServiceMixNMR Mock class <tt>org.apache.servicemix.camel.nmr.AbstractComponentClass</tt>
which will create a NMR bus, register the Camel NMR Component and the endpoints defined into
the Camel routes.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">public</span> class ExchangeUsingNMRTest <span class="code-keyword">extends</span>
AbstractComponentTest {

    @Test
    <span class="code-keyword">public</span> void testProcessing() <span class="code-keyword">throws</span>
InterruptedException {
        MockEndpoint mock = getMockEndpoint(<span class="code-quote">"mock:simple"</span>);
        mock.expectedBodiesReceived(<span class="code-quote">"Simple message body"</span>);

        template.sendBody(<span class="code-quote">"direct:simple"</span>, <span
class="code-quote">"Simple message body"</span>);

        assertMockEndpointsSatisfied();

    }

    @Override
    <span class="code-keyword">protected</span> RouteBuilder createRouteBuilder()
<span class="code-keyword">throws</span> Exception {
        <span class="code-keyword">return</span> <span class="code-keyword">new</span>
RouteBuilder() {

            @Override
            <span class="code-keyword">public</span> void configure() <span
class="code-keyword">throws</span> Exception {
                from(<span class="code-quote">"direct:simple"</span>).to(<span
class="code-quote">"nmr:simple"</span>);
                from(<span class="code-quote">"nmr:simple?synchronous=<span class="code-keyword">true</span>"</span>).to(<span
class="code-quote">"mock:simple"</span>);
            }
        };
    }
}
</pre>
</div></div>


<h3><a name="NMR-SeeAlso"></a>See Also</h3>
<ul>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
Camel</a></li>
	<li><a href="/confluence/display/CAMEL/Component" title="Component">Component</a></li>
	<li><a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a></li>
	<li><a href="/confluence/display/CAMEL/Getting+Started" title="Getting Started">Getting
Started</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/NMR">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=3473729&revisedVersion=7&originalVersion=6">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/NMR?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message