camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Bean
Date Thu, 05 Jul 2012 11:38: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/Bean">Bean</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~hekonsek">Henryk
Konsek</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        Added section regarding Java DSL syntax for Bean component<br />
    </div>
        <br/>
                         <h4>Changes (1)</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" >Camel will use [Bean Binding] to invoke
the {{sayHello}} method, by converting the Exchange&#39;s In body to the {{String}} type
and storing the output of the method on the Exchange Out body. <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h3.
Java DSL bean syntax <br> <br>Java DSL comes with syntactic sugar for the [Bean]
component. Instead of specifying the bean explicitly as the endpoint (i.e. {{to(&quot;bean:beanName&quot;)}})
you can use the following syntax: <br> <br>{code:java} <br>// Send message
to the bean endpoint <br>// and invoke method resolved using Bean Binding. <br>from(&quot;direct:start&quot;).beanRef(&quot;beanName&quot;);
<br> <br>// Send message to the bean endpoint <br>// and invoke given method.
<br>from(&quot;direct:start&quot;).beanRef(&quot;beanName&quot;, &quot;methodName&quot;);
<br>{code} <br> <br>Instead of passing name of the reference to the bean
(so that Camel will lookup for it in the registry), you can specify the bean itself: <br>
<br>{code:java} <br>// Send message to the given bean instance. <br>from(&quot;direct:start&quot;).bean(new
ExampleBean()); <br> <br>// Explicit selection of bean method to be invoked. <br>from(&quot;direct:start&quot;).bean(new
ExampleBean(), &quot;methodName&quot;); <br> <br>// Camel will create
the instance of bean and cache it for you. <br>from(&quot;direct:start&quot;).bean(ExampleBean.class);
<br>{code} <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h3. Bean Binding <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="Bean-BeanComponent"></a>Bean Component</h2>

<p>The <b>bean:</b> component binds beans to Camel message exchanges.</p>

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

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
bean:beanID[?options]
</pre>
</div></div>
<p>Where <b>beanID</b> can be any string which is used to look up the bean
in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a></p>

<h3><a name="Bean-Options"></a>Options</h3>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Type </th>
<th class='confluenceTh'> Default </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>method</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> The method name from the bean that will be invoked. If not
provided, Camel will try to determine the method itself. In case of ambiguity an exception
will be thrown. See <a href="/confluence/display/CAMEL/Bean+Binding" title="Bean Binding">Bean
Binding</a> for more details. From <b>Camel 2.8</b> onwards you can specify
type qualifiers to pin-point exact method to use for overloaded methods. From <b>Camel
2.9</b> onwards you can specify parameter values directly in the method syntax. See
more details at <a href="/confluence/display/CAMEL/Bean+Binding" title="Bean Binding">Bean
Binding</a>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>cache</tt> </td>
<td class='confluenceTd'> <tt>boolean</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> If enabled, Camel will cache the result of the first <a
href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a> look-up.
Cache can be enabled if the bean in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>
is defined as a singleton scope. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>multiParameterArray</tt> </td>
<td class='confluenceTd'> <tt>boolean</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 1.5:</b> How to treat the parameters
which are passed from the message body; if it is <tt>true</tt>, the In message
body should be an array of parameters. </td>
</tr>
</tbody></table>
</div>
</div>

<p>You can append query options to the URI in the following format, <tt>?option=value&amp;option=value&amp;...</tt></p>

<h3><a name="Bean-Using"></a>Using</h3>

<p>The object instance that is used to consume messages must be explicitly registered
with the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>.
For example, if you are using Spring you must define the bean in the Spring configuration,
<tt>spring.xml</tt>; or if you don't use Spring, by registering the bean in JNDI.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// lets populate the context
with the services we need
</span><span class="code-comment">// note that we could just use a spring.xml
file to avoid <span class="code-keyword">this</span> step
</span>JndiContext context = <span class="code-keyword">new</span> JndiContext();
context.bind(<span class="code-quote">"bye"</span>, <span class="code-keyword">new</span>
SayService(<span class="code-quote">"Good Bye!"</span>));

CamelContext camelContext = <span class="code-keyword">new</span> DefaultCamelContext(context);
</pre>
</div></div>

<p>Once an endpoint has been registered, you can build Camel routes that use it to process
exchanges.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// lets add simple route
</span>camelContext.addRoutes(<span class="code-keyword">new</span> RouteBuilder()
{
    <span class="code-keyword">public</span> void configure() {
        from(<span class="code-quote">"direct:hello"</span>).to(<span class="code-quote">"bean:bye"</span>);
    }
});
</pre>
</div></div>

<p>A <b>bean:</b> endpoint cannot be defined as the input to the route;
i.e. you cannot consume from it, you can only route from some inbound message <a href="/confluence/display/CAMEL/Endpoint"
title="Endpoint">Endpoint</a> to the bean endpoint as output.  So consider using
a <b>direct:</b> or <b>queue:</b> endpoint as the input.  </p>

<p>You can use the <tt>createProxy()</tt> methods on <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/component/bean/ProxyHelper.html"
class="external-link" rel="nofollow">ProxyHelper</a> to create a proxy that will
generate BeanExchanges and send them to any endpoint:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">Endpoint endpoint = camelContext.getEndpoint(<span class="code-quote">"direct:hello"</span>);
ISay proxy = ProxyHelper.createProxy(endpoint, ISay.class);
<span class="code-object">String</span> rc = proxy.say();
assertEquals(<span class="code-quote">"Good Bye!"</span>, rc);
</pre>
</div></div>

<p>And the same route using Spring DSL:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;route&gt;</span>
   <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:hello"</span>&gt;</span>
   <span class="code-tag">&lt;to uri=<span class="code-quote">"bean:bye"</span>/&gt;</span>
<span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>

<h3><a name="Bean-Beanasendpoint"></a>Bean as endpoint</h3>
<p>Camel also supports invoking <a href="/confluence/display/CAMEL/Bean" title="Bean">Bean</a>
as an Endpoint. In the route below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;camelContext xmlns=<span
class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
  <span class="code-tag">&lt;route&gt;</span>
    <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"myBean"</span>/&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:results"</span>/&gt;</span>
  <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>

<span class="code-tag">&lt;bean id=<span class="code-quote">"myBean"</span>
class=<span class="code-quote">"org.apache.camel.spring.bind.ExampleBean"</span>/&gt;</span>
</pre>
</div></div>
<p>What happens is that when the exchange is routed to the <tt>myBean</tt>
Camel will use the <a href="/confluence/display/CAMEL/Bean+Binding" title="Bean Binding">Bean
Binding</a> to invoke the bean.<br/>
The source for the bean is just a plain POJO:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class ExampleBean
{

    <span class="code-keyword">public</span> <span class="code-object">String</span>
sayHello(<span class="code-object">String</span> name) {
        <span class="code-keyword">return</span> <span class="code-quote">"Hello
"</span> + name + <span class="code-quote">"!"</span>;
    }
}
</pre>
</div></div>
<p>Camel will use <a href="/confluence/display/CAMEL/Bean+Binding" title="Bean Binding">Bean
Binding</a> to invoke the <tt>sayHello</tt> method, by converting the Exchange's
In body to the <tt>String</tt> type and storing the output of the method on the
Exchange Out body.</p>

<h3><a name="Bean-JavaDSLbeansyntax"></a>Java DSL bean syntax</h3>

<p>Java DSL comes with syntactic sugar for the <a href="/confluence/display/CAMEL/Bean"
title="Bean">Bean</a> component. Instead of specifying the bean explicitly as the
endpoint (i.e. <tt>to("bean:beanName")</tt>) you can use the following syntax:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// Send message to the bean endpoint
</span><span class="code-comment">// and invoke method resolved using Bean Binding.
</span>from(<span class="code-quote">"direct:start"</span>).beanRef(<span
class="code-quote">"beanName"</span>);

<span class="code-comment">// Send message to the bean endpoint
</span><span class="code-comment">// and invoke given method.
</span>from(<span class="code-quote">"direct:start"</span>).beanRef(<span
class="code-quote">"beanName"</span>, <span class="code-quote">"methodName"</span>);
</pre>
</div></div>

<p>Instead of passing name of the reference to the bean (so that Camel will lookup for
it in the registry), you can specify the bean itself:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-comment">// Send message to the given bean instance.
</span>from(<span class="code-quote">"direct:start"</span>).bean(<span
class="code-keyword">new</span> ExampleBean());

<span class="code-comment">// Explicit selection of bean method to be invoked.
</span>from(<span class="code-quote">"direct:start"</span>).bean(<span
class="code-keyword">new</span> ExampleBean(), <span class="code-quote">"methodName"</span>);

<span class="code-comment">// Camel will create the instance of bean and cache it <span
class="code-keyword">for</span> you.
</span>from(<span class="code-quote">"direct:start"</span>).bean(ExampleBean.class);
</pre>
</div></div>

<h3><a name="Bean-BeanBinding"></a>Bean Binding</h3>

<p>How bean methods to be invoked are chosen (if they are not specified explicitly through
the <b>method</b> parameter) and how parameter values are constructed from the
<a href="/confluence/display/CAMEL/Message" title="Message">Message</a> are all
defined by the <a href="/confluence/display/CAMEL/Bean+Binding" title="Bean Binding">Bean
Binding</a> mechanism which is used throughout all of the various <a href="/confluence/display/CAMEL/Bean+Integration"
title="Bean Integration">Bean Integration</a> mechanisms in Camel.</p>

<h3><a name="Bean-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>

<ul>
	<li><a href="/confluence/display/CAMEL/Class" title="Class">Class</a> component</li>
	<li><a href="/confluence/display/CAMEL/Bean+Binding" title="Bean Binding">Bean
Binding</a></li>
	<li><a href="/confluence/display/CAMEL/Bean+Integration" title="Bean Integration">Bean
Integration</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/Bean">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=54789&revisedVersion=30&originalVersion=29">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Bean?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message