camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > EJB
Date Tue, 01 Jun 2010 07:18:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/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="http://cwiki.apache.org/confluence/display/CAMEL/EJB">EJB</a></h2>
    <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (3)</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" >h3. Examples <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">TODO:
A couple of examples <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">In
the following examples we use the Greater EJB which is defined as follows: <br>{snippet:id=e1|lang=java|title=GreaterLocal.java|url=camel/trunk/components/camel-ejb/src/test/java/org/apache/camel/component/ejb/GreaterLocal.java}
<br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">And
the implementation <br>{snippet:id=e1|lang=java|title=GreaterImpl.java|url=camel/trunk/components/camel-ejb/src/test/java/org/apache/camel/component/ejb/GreaterImpl.java}
<br> <br>h4. Using Java DSL <br> <br>In this example we want to invoke
the {{hello}} method on the EJB. Since this example is based on an unit test using Apache
OpenEJB we have to set a {{JndiContext}} on the [EJB] component with the OpenEJB settings.
<br> <br>{snippet:id=e1|lang=java|url=camel/trunk/components/camel-ejb/src/test/java/org/apache/camel/component/ejb/GreaterCamelEjbTest.java}
<br> <br>Then we are ready to use the EJB in the Camel route: <br> <br>{snippet:id=e2|lang=java|url=camel/trunk/components/camel-ejb/src/test/java/org/apache/camel/component/ejb/GreaterCamelEjbTest.java}
<br> <br>{tip:title=In a real application server} <br>In a real application
server you most likely do not have to setup a {{JndiContext}} on the [EJB] component as it
will create a default {{JndiContext}} on the same JVM as the application server, which usually
allows it to access the JNDI registry and lookup the [EJB]s. <br>However if you need
to access a application server on a remote JVM or the likes, you have to prepare the properties
beforehand. <br>{tip} <br> <br>h4. Using Spring XML <br> <br>And
this is the same example using Spring XML instead: <br> <br>Again since this is
based on an unit test we need to setup the [EJB] component: <br> <br>{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-ejb/src/test/resources/org/apache/camel/component/ejb/SpringGreaterCamelEjbTest.xml}
<br> <br>Before we are ready to use [EJB] in the Camel routes: <br> <br>{snippet:id=e2|lang=xml|url=camel/trunk/components/camel-ejb/src/test/resources/org/apache/camel/component/ejb/SpringGreaterCamelEjbTest.xml}
<br> <br> <br> <br></td></tr>
            <tr><td class="diff-unchanged" >{include:Endpoint See Also} <br>*
[Bean] <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
        </table>
</div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="EJB-EJBComponent"></a>EJB Component</h2>
<p><b>Available as of Camel 2.4</b></p>

<p>The <b>ejb:</b> component binds EJBs to Camel message exchanges.</p>

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

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

<h3><a name="EJB-Options"></a>Options</h3>
<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 that bean will be invoked. If not provided,
Camel will try to pick the method itself. In case of ambiguity an exception is thrown. See
<a href="/confluence/display/CAMEL/Bean+Binding" title="Bean Binding">Bean Binding</a>
for more details. </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'> 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>


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

<p>The <a href="/confluence/display/CAMEL/EJB" title="EJB">EJB</a> component
extends the <a href="/confluence/display/CAMEL/Bean" title="Bean">Bean</a> component
in which most of the details from the <a href="/confluence/display/CAMEL/Bean" title="Bean">Bean</a>
component applies to this component as well.</p>

<h3><a name="EJB-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="EJB-Examples"></a>Examples</h3>

<p>In the following examples we use the Greater EJB which is defined as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>GreaterLocal.java</b></div><div
class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> <span
class="code-keyword">interface</span> GreaterLocal {

    <span class="code-object">String</span> hello(<span class="code-object">String</span>
name);

    <span class="code-object">String</span> bye(<span class="code-object">String</span>
name);

}
</pre>
</div></div>

<p>And the implementation</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>GreaterImpl.java</b></div><div
class="codeContent panelContent">
<pre class="code-java">@Stateless
<span class="code-keyword">public</span> class GreaterImpl <span class="code-keyword">implements</span>
GreaterLocal {

    <span class="code-keyword">public</span> <span class="code-object">String</span>
hello(<span class="code-object">String</span> name) {
        <span class="code-keyword">return</span> <span class="code-quote">"Hello
"</span> + name;
    }

    <span class="code-keyword">public</span> <span class="code-object">String</span>
bye(<span class="code-object">String</span> name) {
        <span class="code-keyword">return</span> <span class="code-quote">"Bye
"</span> + name;
    }

}
</pre>
</div></div>

<h4><a name="EJB-UsingJavaDSL"></a>Using Java DSL</h4>

<p>In this example we want to invoke the <tt>hello</tt> method on the EJB.
Since this example is based on an unit test using Apache OpenEJB we have to set a <tt>JndiContext</tt>
on the <a href="/confluence/display/CAMEL/EJB" title="EJB">EJB</a> component with
the OpenEJB settings.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">@Override
<span class="code-keyword">protected</span> CamelContext createCamelContext()
<span class="code-keyword">throws</span> Exception {
    CamelContext answer = <span class="code-keyword">new</span> DefaultCamelContext();

    <span class="code-comment">// enlist EJB component using the JndiContext
</span>    EjbComponent ejb = answer.getComponent(<span class="code-quote">"ejb"</span>,
EjbComponent.class);
    ejb.setContext(createEjbContext());

    <span class="code-keyword">return</span> answer;
}

<span class="code-keyword">private</span> <span class="code-keyword">static</span>
Context createEjbContext() <span class="code-keyword">throws</span> NamingException
{
    <span class="code-comment">// here we need to define our context factory to use
OpenEJB <span class="code-keyword">for</span> our testing
</span>    Properties properties = <span class="code-keyword">new</span>
Properties();
    properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, <span class="code-quote">"org.apache.openejb.client.LocalInitialContextFactory"</span>);

    <span class="code-keyword">return</span> <span class="code-keyword">new</span>
InitialContext(properties);
}
</pre>
</div></div>

<p>Then we are ready to use the EJB in the Camel route:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:start"</span>)
    <span class="code-comment">// invoke the greeter EJB using the local <span class="code-keyword">interface</span>
and invoke the hello method
</span>    .to(<span class="code-quote">"ejb:GreaterImplLocal?method=hello"</span>)
    .to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>In a real application
server</b><br />In a real application server you most likely do not have to setup
a <tt>JndiContext</tt> on the <a href="/confluence/display/CAMEL/EJB" title="EJB">EJB</a>
component as it will create a default <tt>JndiContext</tt> on the same JVM as
the application server, which usually allows it to access the JNDI registry and lookup the
<a href="/confluence/display/CAMEL/EJB" title="EJB">EJB</a>s.<br/>
However if you need to access a application server on a remote JVM or the likes, you have
to prepare the properties beforehand.</td></tr></table></div>

<h4><a name="EJB-UsingSpringXML"></a>Using Spring XML</h4>

<p>And this is the same example using Spring XML instead:</p>

<p>Again since this is based on an unit test we need to setup the <a href="/confluence/display/CAMEL/EJB"
title="EJB">EJB</a> component:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag"><span class="code-comment">&lt;!--
setup Camel EJB component --&gt;</span></span>
<span class="code-tag">&lt;bean id=<span class="code-quote">"ejb"</span>
class=<span class="code-quote">"org.apache.camel.component.ejb.EjbComponent"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"properties"</span>
ref=<span class="code-quote">"jndiProperties"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

<span class="code-tag"><span class="code-comment">&lt;!-- use OpenEJB context
factory --&gt;</span></span>
<span class="code-tag">&lt;p:properties id=<span class="code-quote">"jndiProperties"</span>&gt;</span>
    <span class="code-tag">&lt;prop key=<span class="code-quote">"java.naming.factory.initial"</span>&gt;</span>org.apache.openejb.client.LocalInitialContextFactory<span
class="code-tag">&lt;/prop&gt;</span>
<span class="code-tag">&lt;/p:properties&gt;</span>
</pre>
</div></div>

<p>Before we are ready to use <a href="/confluence/display/CAMEL/EJB" title="EJB">EJB</a>
in the Camel routes:</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">"ejb:GreaterImplLocal?method=hello"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>



<h3><a name="EJB-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/Bean" title="Bean">Bean</a></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="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="http://cwiki.apache.org/confluence/display/CAMEL/EJB">View Online</a>
        |
        <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=21791734&revisedVersion=2&originalVersion=1">View
Changes</a>
                |
        <a href="http://cwiki.apache.org/confluence/display/CAMEL/EJB?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message