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 Language
Date Sat, 16 Jan 2010 12:51: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/Bean+Language">Bean
Language</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">
         <h2><a name="BeanLanguage-BeanLanguage"></a>Bean Language</h2>

<p>The purpose of the Bean Language is to be able to implement an <a href="/confluence/display/CAMEL/Expression"
title="Expression">Expression</a> or <a href="/confluence/display/CAMEL/Predicate"
title="Predicate">Predicate</a> using a simple method on a bean.</p>

<p>So the idea is you specify a bean name which will then be resolved in the <a href="/confluence/display/CAMEL/Registry"
title="Registry">Registry</a> such as the <a href="/confluence/display/CAMEL/Spring"
title="Spring">Spring</a> ApplicationContext then a method is invoked to evaluate
the <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a>
or <a href="/confluence/display/CAMEL/Predicate" title="Predicate">Predicate</a>.</p>

<p>If no method name is provided then one is attempted to be chosen using the rules
for <a href="/confluence/display/CAMEL/Bean+Binding" title="Bean Binding">Bean Binding</a>;
using the type of the message body and using any annotations on the bean methods.</p>

<p>The <a href="/confluence/display/CAMEL/Bean+Binding" title="Bean Binding">Bean
Binding</a> rules are used to bind the <a href="/confluence/display/CAMEL/Message"
title="Message">Message</a> Exchange to the method parameters; so you can annotate
the bean to extract headers or other expressions such as <a href="/confluence/display/CAMEL/XPath"
title="XPath">XPath</a> or <a href="/confluence/display/CAMEL/XQuery" title="XQuery">XQuery</a>
from the message.</p>

<h3><a name="BeanLanguage-UsingBeanExpressionsfromtheJavaDSL"></a>Using
Bean Expressions from the Java DSL</h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"activemq:topic:OrdersTopic"</span>).
  filter().method(<span class="code-quote">"myBean"</span>, <span class="code-quote">"isGoldCustomer"</span>).
    to(<span class="code-quote">"activemq:BigSpendersQueue"</span>);
</pre>
</div></div>

<h3><a name="BeanLanguage-UsingBeanExpressionsfromXML"></a>Using Bean Expressions
from XML</h3>

<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">"activemq:topic:OrdersTopic"</span>/&gt;</span>
  <span class="code-tag">&lt;filter&gt;</span>
    <span class="code-tag">&lt;method bean=<span class="code-quote">"myBean"</span>
method=<span class="code-quote">"isGoldCustomer"</span>/&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"activemq:BigSpendersQueue"</span>/&gt;</span>
  <span class="code-tag">&lt;/filter&gt;</span>
<span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>

<h3><a name="BeanLanguage-Writingtheexpressionbean"></a>Writing the expression
bean</h3>

<p>The bean in the above examples is just any old Java Bean with a method called isGoldCustomer()
that returns some object that is easily converted to a <b>boolean</b> value in
this case, as its used as a predicate.</p>

<p>So we could implement it like this...</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">public</span> class MyBean {
  <span class="code-keyword">public</span> <span class="code-object">boolean</span>
isGoldCustomer(Exchange exchange) {
  	...
  }
}
</pre>
</div></div>

<p>We can also use the <a href="/confluence/display/CAMEL/Bean+Integration" title="Bean
Integration">Bean Integration</a> annotations. For example you could do...</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-object">boolean</span>
isGoldCustomer(<span class="code-object">String</span> body) {...}
</pre>
</div></div>

<p>or</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-object">boolean</span>
isGoldCustomer(@Header(name = <span class="code-quote">"foo"</span>) <span
class="code-object">Integer</span> fooHeader) {...}
</pre>
</div></div>

<p>So you can bind parameters of the method to the Exchange, the <a href="/confluence/display/CAMEL/Message"
title="Message">Message</a> or individual headers, properties, the body or other
expressions.</p>

<h3><a name="BeanLanguage-Nonregistrybeans"></a>Non registry beans</h3>
<p>As of Camel 1.5 the <a href="/confluence/display/CAMEL/Bean+Language" title="Bean
Language">Bean Language</a> also supports invoking beans that isn't registered in
the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>.
This is usable for quickly to invoke a bean from Java DSL where you don't need to register
the bean in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>
such as the <a href="/confluence/display/CAMEL/Spring" title="Spring">Spring</a>
ApplicationContext.</p>

<p>Camel can instantiate the bean and invoke the method if given a class or invoke an
already existing instance. This is illustrated from the example below:<br/>
<b>NOTE</b> This bean DSL is supported since Camel 2.0-M2</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
        from(<span class="code-quote">"activemq:topic:OrdersTopic"</span>).
                filter().expression(BeanLanguage(MyBean.class, <span class="code-quote">"isGoldCustomer"</span>)).
                to(<span class="code-quote">"activemq:BigSpendersQueue"</span>);
</pre>
</div></div>

<p>The 2nd parameter <tt>isGoldCustomer</tt> is an optional parameter to
explicit set the method name to invoke. If not provided Camel will try to invoke the best
suited method. If case of ambiguity Camel will thrown an Exception. In these situations the
2nd parameter can solve this problem. Also the code is more readable if the method name is
provided. The 1st parameter can also be an existing instance of a Bean such as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
   <span class="code-keyword">private</span> MyBean my;

        from(<span class="code-quote">"activemq:topic:OrdersTopic"</span>).
                filter().expression(BeanLanguage.bean(my, <span class="code-quote">"isGoldCustomer"</span>)).
                to(<span class="code-quote">"activemq:BigSpendersQueue"</span>);
</pre>
</div></div>

<p>In Camel 2.2 onwards you can avoid the <tt>BeanLanguage</tt> and have
it just as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
   <span class="code-keyword">private</span> MyBean my;

        from(<span class="code-quote">"activemq:topic:OrdersTopic"</span>).
                filter().expression(bean(my, <span class="code-quote">"isGoldCustomer"</span>)).
                to(<span class="code-quote">"activemq:BigSpendersQueue"</span>);
</pre>
</div></div>

<p>Which also can be done in a bit shorter and nice way:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
   <span class="code-keyword">private</span> MyBean my;

        from(<span class="code-quote">"activemq:topic:OrdersTopic"</span>).
                filter().method(my, <span class="code-quote">"isGoldCustomer"</span>).
                to(<span class="code-quote">"activemq:BigSpendersQueue"</span>);
</pre>
</div></div>


<h3><a name="BeanLanguage-Otherexamples"></a>Other examples</h3>

<p>We have some test cases you can look at if it'll help</p>

<ul>
	<li><a href="http://svn.apache.org/repos/asf/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/MethodFilterTest.java"
rel="nofollow">MethodFilterTest </a> is a JUnit test case showing the Java <a
href="/confluence/display/CAMEL/DSL" title="DSL">DSL</a> use of the bean expression
being used in a filter</li>
	<li><a href="http://svn.apache.org/repos/asf/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aggregator.xml"
rel="nofollow">aggregator.xml</a> is a Spring XML test case for the <a href="/confluence/display/CAMEL/Aggregator"
title="Aggregator">Aggregator</a> which uses a bean method call to test for the completion
of the aggregation.</li>
</ul>


<h3><a name="BeanLanguage-Dependencies"></a>Dependencies</h3>
<p>The Bean language is part of <b>camel-core</b>.</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/Bean+Language">View
Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=70888&revisedVersion=14&originalVersion=13">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CAMEL/Bean+Language?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message