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 Producing
Date Sat, 14 Apr 2012 08: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/POJO+Producing">POJO
Producing</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~bvahdat">Babak
Vahdat</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        CamelTemplate has been removed in Camel 2.x onwards<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" >h4. @EndpointInject <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">To
allow sending of messages from POJOs you can use [@EndpointInject() annotation|http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/EndpointInject.html].
This will inject either a  [ProducerTemplate|http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/ProducerTemplate.html]
or [CamelTemplate |http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/CamelTemplate.html]
so that the bean can send message exchanges. <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">To
allow sending of messages from POJOs you can use [@EndpointInject |http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/EndpointInject.html]
annotation. This will inject a [ProducerTemplate|http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/ProducerTemplate.html]
so that the bean can send message exchanges. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>e.g. lets send a message
to the *foo.bar* queue in [ActiveMQ] at some point <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <p>There are two different ways to send messages to any Camel <a href="/confluence/display/CAMEL/Endpoint"
title="Endpoint">Endpoint</a> from a POJO</p>

<h4><a name="POJOProducing-@EndpointInject"></a>@EndpointInject</h4>

<p>To allow sending of messages from POJOs you can use <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/EndpointInject.html"
class="external-link" rel="nofollow">@EndpointInject </a> annotation. This will inject
a <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/ProducerTemplate.html"
class="external-link" rel="nofollow">ProducerTemplate</a> so that the bean can send
message exchanges.</p>

<p>e.g. lets send a message to the <b>foo.bar</b> queue in <a href="/confluence/display/CAMEL/ActiveMQ"
title="ActiveMQ">ActiveMQ</a> at some point</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 {
  @EndpointInject(uri=<span class="code-quote">"activemq:foo.bar"</span>)
  ProducerTemplate producer;

  <span class="code-keyword">public</span> void doSomething() {
    <span class="code-keyword">if</span> (whatever) {
      producer.sendBody(<span class="code-quote">"&lt;hello&gt;world!&lt;/hello&gt;"</span>);
    }
  }
}
</pre>
</div></div>

<p>The downside of this is that your code is now dependent on a Camel API, the ProducerTemplate.
The next section describes how to remove this</p>

<h3><a name="POJOProducing-HidingtheCamelAPIsfromyourcodeusing@Produce"></a>Hiding
the Camel APIs from your code using @Produce</h3>

<p>We recommend <a href="/confluence/display/CAMEL/Hiding+Middleware" title="Hiding
Middleware">Hiding Middleware</a> APIs from your application code so the next option
might be more suitable.<br/>
 You can add the @Produce annotation to an injection point (a field or property setter) using
a ProducerTemplate <b>or</b> using some interface you use in your business logic.
e.g.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">public</span> <span class="code-keyword">interface</span>
MyListener {
    <span class="code-object">String</span> sayHello(<span class="code-object">String</span>
name);
}

<span class="code-keyword">public</span> class MyBean {
    @Produce(uri = <span class="code-quote">"activemq:foo"</span>)
    <span class="code-keyword">protected</span> MyListener producer;

    <span class="code-keyword">public</span> void doSomething() {
        <span class="code-comment">// lets send a message
</span>        <span class="code-object">String</span> response = producer.sayHello(<span
class="code-quote">"James"</span>);
    }
}
</pre>
</div></div>

<p>Here Camel will automatically inject a smart client side proxy at the @Produce annotation
- an instance of the MyListener instance. When we invoke methods on this interface the method
call is turned into an object and using the Camel <a href="/confluence/display/CAMEL/Spring+Remoting"
title="Spring Remoting">Spring Remoting</a> mechanism it is sent to the endpoint
- in this case the <a href="/confluence/display/CAMEL/ActiveMQ" title="ActiveMQ">ActiveMQ</a>
endpoint to queue <b>foo</b>; then the caller blocks for a response.</p>

<p>If you want to make asynchronous message sends then use <a href="/confluence/display/CAMEL/Using+Exchange+Pattern+Annotations"
title="Using Exchange Pattern Annotations">an @InOnly annotation on the injection point</a>.</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+Producing">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=98501&revisedVersion=6&originalVersion=5">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/POJO+Producing?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message