camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > POJO Consuming
Date Tue, 02 Feb 2010 12:22:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/CAMEL/POJO+Consuming">POJO
Consuming</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
     
          <br/>
     <div class="notificationGreySide">
         <h4><a name="POJOConsuming-@MessageDrivenor@Consume"></a>@MessageDriven
or @Consume</h4>

<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>@MessageDriven
is @deprecated</b><br /><p>@MessageDriven is deprecated in Camel 1.x. You
should use @Consume instead. Its removed in Camel 2.0.</p></td></tr></table></div>

<p>To consume a message you use either the <a href="http://activemq.apache.org/camel/maven/camel-core/apidocs/org/apache/camel/MessageDriven.html"
rel="nofollow">@MessageDriven</a> annotation or from 1.5.0 the <a href="http://activemq.apache.org/camel/maven/camel-core/apidocs/org/apache/camel/Consume.html"
rel="nofollow">@Consume</a> annotation to mark a particular method of a bean as being
a consumer method. The uri of the annotation defines the Camel <a href="/confluence/display/CAMEL/Endpoint"
title="Endpoint">Endpoint</a> to consume from. </p>

<p>e.g. lets invoke the onCheese() method with the String body of the inbound JMS message
from <a href="/confluence/display/CAMEL/ActiveMQ" title="ActiveMQ">ActiveMQ</a>
on the cheese queue; this will use the <a href="/confluence/display/CAMEL/Type+Converter"
title="Type Converter">Type Converter</a> to convert the JMS ObjectMessage or BytesMessage
to a String - or just use a TextMessage from JMS</p>

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

  @Consume(uri=<span class="code-quote">"activemq:cheese"</span>)
  <span class="code-keyword">public</span> void onCheese(<span class="code-object">String</span>
name) {
    ...
  }
}
</pre>
</div></div>

<p>The <a href="/confluence/display/CAMEL/Bean+Binding" title="Bean Binding">Bean
Binding</a> is then used to convert the inbound <a href="/confluence/display/CAMEL/Message"
title="Message">Message</a> to the parameter list used to invoke the method .</p>

<p>What this does is basically create a route that looks kinda like this</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(uri).bean(theBean, <span class="code-quote">"methodName"</span>);
</pre>
</div></div>

<div class='panelMacro'><table class='warningMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/forbidden.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>When using
more than one CamelContext</b><br /><p>When you use more than 1 <a href="/confluence/display/CAMEL/CamelContext"
title="CamelContext">CamelContext</a> you might end up with each of them creating
a <a href="/confluence/display/CAMEL/POJO+Consuming" title="POJO Consuming">POJO Consuming</a>.
<br/>
In Camel 2.0 there is a new option on <b>@Consume</b> that allows you to specify
which <a href="/confluence/display/CAMEL/CamelContext" title="CamelContext">CamelContext</a>
id/name you want it to apply for.</p></td></tr></table></div>

<h3><a name="POJOConsuming-UsingcontextoptiontoapplyonlyacertainCamelContext"></a>Using
context option to apply only a certain CamelContext</h3>
<p><b>Available as of Camel 2.0</b><br/>
See the warning above.</p>

<p>You can use the <tt>context</tt> option to specify which <a href="/confluence/display/CAMEL/CamelContext"
title="CamelContext">CamelContext</a> the consumer should only apply for. For example:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
  @Consume(uri=<span class="code-quote">"activemq:cheese"</span>, context=<span
class="code-quote">"camel-1"</span>)
  <span class="code-keyword">public</span> void onCheese(<span class="code-object">String</span>
name) {
</pre>
</div></div>

<p>The consumer above will only be created for the <a href="/confluence/display/CAMEL/CamelContext"
title="CamelContext">CamelContext</a> that have the context id = <tt>camel-1</tt>.
You set this id in the XML tag:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;camelContext id=<span class="code-quote">"camel-1"</span>
...&gt;</span>
</pre>
</div></div>

<h4><a name="POJOConsuming-Usinganexplicitroute"></a>Using an explicit route</h4>

<p>If you want to invoke a bean method from many different endpoints or within different
complex routes in different circumstances you can just use the normal routing <a href="/confluence/display/CAMEL/DSL"
title="DSL">DSL</a> or the <a href="/confluence/display/CAMEL/Spring" title="Spring">Spring</a>
XML configuration file.</p>

<p>For example</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(uri).beanRef(<span class="code-quote">"myBean"</span>, <span class="code-quote">"methodName"</span>);
</pre>
</div></div>


<p>which will then look up in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>
and find the bean and invoke the given bean name. (You can omit the method name and have Camel
figure out the right method based on the method annotations and body type).</p>

<h4><a name="POJOConsuming-UsetheBeanendpoint"></a>Use the Bean endpoint</h4>

<p>You can always use the bean endpoint</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(uri).to(<span class="code-quote">"bean:myBean?method=methodName"</span>);
</pre>
</div></div>


<h4><a name="POJOConsuming-Whichapproachtouse%3F"></a>Which approach to
use?</h4>

<p>Using the @MessageDriven/@Consume annotations are simpler when you are creating a
simple route with a single well defined input URI. </p>

<p>However if you require more complex routes or the same bean method needs to be invoked
from many places then please use the routing <a href="/confluence/display/CAMEL/DSL" title="DSL">DSL</a>
as shown above.</p>

     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/CAMEL/POJO+Consuming">View
Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=98504&revisedVersion=9&originalVersion=8">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CAMEL/POJO+Consuming?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message