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 Fri, 17 Aug 2012 13:24: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/POJO+Consuming">POJO
Consuming</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (9)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">h4.
@MessageDriven or @Consume <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h4.
@Consume <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;">{info:title=@MessageDriven
is @deprecated} <br>@MessageDriven is deprecated in Camel 1.x. You should use @Consume
instead. Its removed in Camel 2.0. <br>{info} <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >To consume a message you use <span
class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">either</span>
the <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">[@MessageDriven|http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/MessageDriven.html]
annotation or from 1.5.0 the</span> [@Consume|http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Consume.html]
annotation to mark a particular method of a bean as being a consumer method. The uri of the
annotation defines the Camel [Endpoint] to consume from. <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-changed-lines" >e.g. lets invoke the <span
class="diff-changed-words"><span class="diff-added-chars"style="background-color: #dfd;">{{</span>onCheese()<span
class="diff-added-chars"style="background-color: #dfd;">}}</span></span> method
with the String body of the inbound JMS message from [ActiveMQ] on the cheese queue; this
will use the [Type Converter] to convert the JMS ObjectMessage or BytesMessage to a String
- or just use a TextMessage from JMS <br></td></tr>
            <tr><td class="diff-unchanged" > <br>{code} <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>{warning:title=When using
more than one CamelContext} <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">When
you use more than 1 [CamelContext] you might end up with each of them creating a [POJO Consuming].
 <br>In Camel 2.0 there is a new option on *@Consume* that allows you to specify which
[CamelContext] id/name you want it to apply for. <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">When
you use more than 1 [CamelContext] you might end up with each of them creating a [POJO Consuming];
therefore use the option {{context}} on *@Consume* that allows you to specify which [CamelContext]
id/name you want it to apply for. <br></td></tr>
            <tr><td class="diff-unchanged" >{warning} <br> <br>h3.
Using context option to apply only a certain CamelContext <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">*Available
as of Camel 2.0* <br></td></tr>
            <tr><td class="diff-unchanged" >See the warning above. <br>
<br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >h4. Which approach to use? <br>
<br></td></tr>
            <tr><td class="diff-changed-lines" >Using the <span class="diff-changed-words">@<span
class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">MessageDriven/@</span>Consume</span>
annotations are simpler when you are creating a simple route with a single well defined input
URI. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>However if you require
more complex routes or the same bean method needs to be invoked from many places then please
use the routing [DSL] as shown above. <br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h4><a name="POJOConsuming-@Consume"></a>@Consume</h4>

<p>To consume a message you use the <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Consume.html"
class="external-link" 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 <tt>onCheese()</tt> 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 />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>;
therefore use the option <tt>context</tt> 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.</td></tr></table></div>

<h3><a name="POJOConsuming-UsingcontextoptiontoapplyonlyacertainCamelContext"></a>Using
context option to apply only a certain CamelContext</h3>
<p>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 @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="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/POJO+Consuming">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=98504&revisedVersion=11&originalVersion=10">View
Changes</a>
                |
        <a href="https://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