<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">
<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>
                                <td>
                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">Community</a></h1>
                                                                </td>
                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px; -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
JBoss EJB3 remote invocation from client app
</h3>
<span style="margin-bottom: 10px;">
created by <a href="http://community.jboss.org/people/zecas">zecas zecas</a> in <i>EJB 3.0</i> - <a href="http://community.jboss.org/message/545296#545296">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><p>Hi,</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>I'm trying to workout an EJB example with JBoss, but I'm having some trouble understanding some things, and making other things to work.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>My Environment:<br/>- Windows XP;<br/>- JBoss 5.1.0.GA;<br/>- Eclipse.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>The project is composed of an EAR with an EJB 3.0 and WAR module. Then I'll try to connect remotely in a client application to invoke the EJB.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>So for a start, my EJB definition is:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java"><font color="navy"><b>package</b></font> com.test;
 
<font color="navy"><b>import</b></font> javax.ejb.Stateless;
 
<font color="darkgreen">/**
*  Session Bean implementation class MyBean
*/</font>
@Stateless(name =  <font color="red">"my-name"</font>, mappedName = <font color="red">"my-map"</font>)
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> MyBean implements  MyRemote, MyLocal <font color="navy">{</font>
 
    <font color="darkgreen">/**
     * Default  constructor.
     */</font>
    <font color="navy"><b>public</b></font> MyBean() <font color="navy">{</font>
    <font color="navy">}</font>
 
     <font color="navy"><b>public</b></font> String getMyCommon() <font color="navy">{</font>
        <font color="navy"><b>return</b></font> <font color="red">"My-Common"</font>;
    <font color="navy">}</font>
 
     <font color="navy"><b>public</b></font> String getMyRemote() <font color="navy">{</font>
        <font color="navy"><b>return</b></font> <font color="red">"My-Remote"</font>;
    <font color="navy">}</font>
 
     <font color="navy"><b>public</b></font> String getMyLocal() <font color="navy">{</font>
        <font color="navy"><b>return</b></font> <font color="red">"My-Local"</font>;
    <font color="navy">}</font>
 
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java"><font color="navy"><b>package</b></font> com.test;
 
<font color="navy"><b>import</b></font> javax.ejb.Local;
 
@Local
public  <font color="navy"><b>interface</b></font> MyLocal <font color="navy">{</font>
 
    <font color="navy"><b>public</b></font> String getMyCommon();
     <font color="navy"><b>public</b></font> String getMyLocal();
 
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java"><font color="navy"><b>package</b></font> com.test;
 
<font color="navy"><b>import</b></font> javax.ejb.Remote;
 
@Remote
public  <font color="navy"><b>interface</b></font> MyRemote <font color="navy">{</font>
 
    <font color="navy"><b>public</b></font> String getMyCommon();
     <font color="navy"><b>public</b></font> String getMyRemote();
 
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><br/>My ejb-jar.xml in EJB module META-INF, has no definitions:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-xml"><span class="jive-xml-tag"><?xml version="1.0" encoding="ASCII"?></span>
<span class="jive-xml-tag"><span><ejb-jar  xmlns:xsi="</span><a class="jive-link-external-small" href="http://www.w3.org/2001/XMLSchema-instance" target="_blank">http://www.w3.org/2001/XMLSchema-instance</a><span>"  xmlns="</span><a class="jive-link-external-small" href="http://java.sun.com/xml/ns/javaee" target="_blank">http://java.sun.com/xml/ns/javaee</a><span>"  xmlns:ejb="</span><a class="jive-link-external-small" href="http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" target="_blank">http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd</a><span>"  xsi:schemaLocation="</span><a class="jive-link-external-small" href="http://java.sun.com/xml/ns/javaee" target="_blank">http://java.sun.com/xml/ns/javaee</a><span>  </span><a class="jive-link-external-small" href="http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" target="_blank">http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd</a><span>" version="3.0"></span></span>
   <span class="jive-xml-tag"><display-name></span>my-ejb<span class="jive-xml-tag"></display-name></span>
<span class="jive-xml-tag"></ejb-jar></span>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>My web.xml in WAR module has no reference to EJB:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-xml"><span class="jive-xml-tag"><?xml version="1.0" encoding="UTF-8"?></span>
<span class="jive-xml-tag"><span><web-app  xmlns:xsi="</span><a class="jive-link-external-small" href="http://www.w3.org/2001/XMLSchema-instance" target="_blank">http://www.w3.org/2001/XMLSchema-instance</a><span>"  xmlns="</span><a class="jive-link-external-small" href="http://java.sun.com/xml/ns/javaee" target="_blank">http://java.sun.com/xml/ns/javaee</a><span>"  xmlns:web="</span><a class="jive-link-external-small" href="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" target="_blank">http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd</a><span>"  xsi:schemaLocation="</span><a class="jive-link-external-small" href="http://java.sun.com/xml/ns/javaee" target="_blank">http://java.sun.com/xml/ns/javaee</a><span>  </span><a class="jive-link-external-small" href="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" target="_blank">http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd</a><span>" id="WebApp_ID"  version="2.5"></span></span>
  <span class="jive-xml-tag"><display-name></span>my-web<span class="jive-xml-tag"></display-name></span>
   <span class="jive-xml-tag"><welcome-file-list></span>
     <span class="jive-xml-tag"><welcome-file></span>index.html<span class="jive-xml-tag"></welcome-file></span>
   <span class="jive-xml-tag"></welcome-file-list></span>
  <span class="jive-xml-tag"><servlet></span>
     <span class="jive-xml-tag"><servlet-name></span>Faces Servlet<span class="jive-xml-tag"></servlet-name></span>
     <span class="jive-xml-tag"><servlet-class></span>javax.faces.webapp.FacesServlet<span class="jive-xml-tag"></servlet-class></span>
     <span class="jive-xml-tag"><load-on-startup></span>1<span class="jive-xml-tag"></load-on-startup></span>
  <span class="jive-xml-tag"></servlet></span>
   <span class="jive-xml-tag"><servlet-mapping></span>
    <span class="jive-xml-tag"><servlet-name></span>Faces  Servlet<span class="jive-xml-tag"></servlet-name></span>
     <span class="jive-xml-tag"><url-pattern></span>/faces/*<span class="jive-xml-tag"></url-pattern></span>
   <span class="jive-xml-tag"></servlet-mapping></span>
  <span class="jive-xml-tag"><servlet-mapping></span>
     <span class="jive-xml-tag"><servlet-name></span>Faces Servlet<span class="jive-xml-tag"></servlet-name></span>
     <span class="jive-xml-tag"><url-pattern></span>*.jsf<span class="jive-xml-tag"></url-pattern></span>
   <span class="jive-xml-tag"></servlet-mapping></span>
<span class="jive-xml-tag"></web-app></span>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>My application.xml describes the modules:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-xml"><span class="jive-xml-tag"><?xml version="1.0" encoding="UTF-8"?></span>
<span class="jive-xml-tag"><span><application  xmlns="</span><a class="jive-link-external-small" href="http://java.sun.com/xml/ns/javaee" target="_blank">http://java.sun.com/xml/ns/javaee</a><span>"  xmlns:xsi="</span><a class="jive-link-external-small" href="http://www.w3.org/2001/XMLSchema-instance" target="_blank">http://www.w3.org/2001/XMLSchema-instance</a><span>"  xsi:schemaLocation="</span><a class="jive-link-external-small" href="http://java.sun.com/xml/ns/javaee" target="_blank">http://java.sun.com/xml/ns/javaee</a><span>  </span><a class="jive-link-external-small" href="http://java.sun.com/xml/ns/javaee/application_5.xsd" target="_blank">http://java.sun.com/xml/ns/javaee/application_5.xsd</a><span>" version="5"></span></span>
   <span class="jive-xml-tag"><display-name></span>my-ear<span class="jive-xml-tag"></display-name></span>
  <span class="jive-xml-tag"><module></span>
     <span class="jive-xml-tag"><web></span>
       <span class="jive-xml-tag"><web-uri></span>my-web-0.0.1-SNAPSHOT.war<span class="jive-xml-tag"></web-uri></span>
       <span class="jive-xml-tag"><context-root></span>/my-web<span class="jive-xml-tag"></context-root></span>
    <span class="jive-xml-tag"></web></span>
   <span class="jive-xml-tag"></module></span>
  <span class="jive-xml-tag"><module></span>
     <span class="jive-xml-tag"><ejb></span>my-ejb-0.0.1-SNAPSHOT.jar<span class="jive-xml-tag"></ejb></span>
  <span class="jive-xml-tag"></module></span>
<span class="jive-xml-tag"></application></span>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>The WAR has a small test page, that injects the @EJB local interface, and successfully gets data, so I assume no other config should be necessary (for now, at least).</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>For deployment, I just copied the EAR to deploy directory. No errors, and checking JMX Console I can find the following info:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code">jboss.deployment
#  id="jboss.j2ee:ear=my-ear-0.0.1-SNAPSHOT.ear,jar=my-ejb-0.0.1-SNAPSHOT.jar,name=my-name,service=EJB3",type=Component
#   id="jboss.j2ee:ear=my-ear-0.0.1-SNAPSHOT.ear,jar=my-ejb-0.0.1-SNAPSHOT.jar,name=my-name,service=EJB3_endpoint",type=Component
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>I then created a small standalone project, with following code:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">        Hashtable environment = <font color="navy"><b>new</b></font> Hashtable();
 
         environment.put(Context.INITIAL_CONTEXT_FACTORY,  <font color="red">"org.jnp.interfaces.NamingContextFactory"</font>);
         environment.put(Context.URL_PKG_PREFIXES,  <font color="red">"org.jboss.naming:org.jnp.interfaces"</font>);
         environment.put(Context.PROVIDER_URL, <font color="red">"jnp://127.0.0.1:1099"</font>); <font color="darkgreen">// remote  machine IP</font>
 
        InitialContext context = <font color="navy"><b>null</b></font>;
         <font color="navy"><b>try</b></font> <font color="navy">{</font>
            context = <font color="navy"><b>new</b></font> InitialContext(environment);
             Object obj = context.lookup(<font color="red">"my-name"</font>);
             System.out.println(<font color="red">"-->> lookup object successfully"</font>);
 
         <font color="navy">}</font> <font color="navy"><b>catch</b></font>(Throwable ex) <font color="navy">{</font>
            ex.printStackTrace();
         <font color="navy">}</font> <font color="navy"><b>finally</b></font> <font color="navy">{</font>
            <font color="navy"><b>if</b></font>( context!=<font color="navy"><b>null</b></font> ) <font color="navy">{</font>
                 context.close();
            <font color="navy">}</font>
        <font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>When executing, I get the exception:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code">javax.naming.NameNotFoundException: my-name not bound
    at  org.jnp.server.NamingServer.getBinding(NamingServer.java:771)
    at  org.jnp.server.NamingServer.getBinding(NamingServer.java:779)
    at  org.jnp.server.NamingServer.getObject(NamingServer.java:785)
    at  org.jnp.server.NamingServer.lookup(NamingServer.java:443)
    at  sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at  sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
     at  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
     at java.lang.reflect.Method.invoke(Method.java:597)
    at  sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:305)
     at sun.rmi.transport.Transport$1.run(Transport.java:159)
    at  java.security.AccessController.doPrivileged(Native Method)
    at  sun.rmi.transport.Transport.serviceCall(Transport.java:155)
    at  sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
     at  sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
     at  sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
     at  java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
     at  java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
     at java.lang.Thread.run(Thread.java:619)
    at  sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:255)
     at  sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:233)
     at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:142)
    at  org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
    at  org.jnp.interfaces.NamingContext.lookup(NamingContext.java:528)
     at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:507)
     at javax.naming.InitialContext.lookup(InitialContext.java:392)
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Changing the lookup to:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">Object obj = context.lookup(<font color="red">"my-map"</font>);
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Works ok. Lookup goes successfull.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Replacing with:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">MyRemote myRemote = (MyRemote) context.lookup(<font color="red">"my-map"</font>);
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Will return:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">java.lang.ClassCastException: javax.naming.Reference cannot be cast to  com.test.MyRemote
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>My client application has dependency to the EJB client jar, which only has MyRemote and MyLocal classes, and a dependency to jbossall-client-3.2.3.jar.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>If I narrow:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">MyRemote myRemote = (MyRemote) PortableRemoteObject.narrow(
                 context.lookup(<font color="red">"my-map"</font>),
                MyRemote.class
                 );
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>I'll get:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">java.lang.ClassCastException
    at  com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:229)
     at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137)
     ... 2 more
Caused by: java.lang.ClassCastException:  javax.naming.Reference cannot be cast to org.omg.CORBA.Object
    at  com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:212)
     ... 3 more
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>What am I doing wrong?</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Thanks</p></div>
<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
<p style="margin: 0;">Reply to this message by <a href="http://community.jboss.org/message/545296#545296">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in EJB 3.0 at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2029">Community</a></p>
</div></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>