8 Replies Latest reply on Nov 13, 2006 4:04 AM by ANDRIA-NTOANINA Herimiaina

    Problem when trying to access an EJB bean in an Enterprise A

    ANDRIA-NTOANINA Herimiaina Newbie

      I've tried to access my Enterprise bean in an Enterprise application client.
      Using this code :
      Code:

       Context c = new InitialContext();
       return (BeanSessionRemote) c.lookup("java:comp/env/ejb/BeanSessionBean");
      



      but when I run my application I got the next Exception:

      javax.naming.NamingException: Failed to find j2ee.clientName in jndi env
      



      May anybody help me?

      Thanks a lot!

      PS:
      I'm using NetBeans 5.5
      and I write in english very bad

        • 1. Re: Problem when trying to access an EJB bean in an Enterpri
          jaikiran pai Master

          What are the contents of the jndi.properties file? It should contain something like:

          java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
          java.naming.provider.url=jnp://localhost:1099
          java.naming.factory.url.pkgs=org.jboss.naming.client
          j2ee.clientName=TheNameOfYourApplicationClient







          • 2. a
            ANDRIA-NTOANINA Herimiaina Newbie

            Yes, and I'v put these code into C:\Program Files\jboss-4.0.3\server\default\conf\jndi.properties. But the result is the same. j2ee.clientName not bound.
            May be there is a something wrong. I see on the net that the jndi.properties must be in my CLASSPATH. And I don't know how to do that with NetBeans 5.5.

            Do you know also how to code application-client.xml?

            • 3. Re: Problem when trying to access an EJB bean in an Enterpri
              jaikiran pai Master

               

              Yes, and I'v put these code into C:\Program Files\jboss-4.0.3\server\default\conf\jndi.properties


              That's the wrong file which you edited. You will have to have the jndi.properties file in the application-client's classpath.

              Do you know also how to code application-client.xml?


              Had done it long time back so dont remember the exact contents but this is what i could think about:

              application-client.xml:
              <?xml version="1.0" encoding="UTF-8"?>
              
              <!DOCTYPE application-client PUBLIC '-//Sun Microsystems, Inc.//DTD J2EE Application Client 1.3//EN' 'http://java.sun.com/dtd/application-client_1_3.dtd'>
              
              <application-client>
               <display-name>My App Client</display-name>
               <ejb-ref>
               <ejb-ref-name>ejb/someEJB</ejb-ref-name>
               <ejb-ref-type>Session</ejb-ref-type>
               <home>package.HomeClassName</home>
               <remote>package.RemoteClassName</remote>
               </ejb-ref>
              
              </application-client>


              and the jboss-client.xml:

              <jboss-client>
               <jndi-name>SampleApplicationClient</jndi-name>
              
               <ejb-ref>
               <ejb-ref-name>ejb/someEJB</ejb-ref-name>
               <jndi-name>TheJndiNameOfTheEJB</jndi-name>
               </ejb-ref>
              
              
              
              </jboss-client>


              the jndi.properties in the application client's classpath:

              java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
              java.naming.provider.url=jnp://localhost:1099
              java.naming.factory.url.pkgs=org.jboss.naming.client
              j2ee.clientName=SampleApplicationClient



              • 4. Re:
                ANDRIA-NTOANINA Herimiaina Newbie

                Here's now my Application-client.xml file:

                <?xml version="1.0" encoding="UTF-8"?>
                <application-client version="1.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application-client_1_4.xsd">
                 <display-name>MFEA-client</display-name>
                 <ejb-ref>
                 <ejb-ref-name>ejb/BeanSessionBean</ejb-ref-name>
                 <ejb-ref-type>Session</ejb-ref-type>
                 <home>bean.BeanSessionLocal</home>
                 <remote>bean.BeanSessionRemote</remote>
                 </ejb-ref>
                </application-client>
                


                jboss-client.xml
                <?xml version="1.0" encoding="UTF-8"?>
                <jboss-client>
                 <jndi-name>MFEA-client</jndi-name>
                 <ejb-ref>
                 <ejb-ref-name>ejb/BeanSessionBean</ejb-ref-name>
                 <jndi-name>BeanSessionBean</jndi-name>
                 </ejb-ref>
                </jboss-client>
                


                my jndi.properties:
                java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
                java.naming.provider.url=jnp://localhost:1099
                java.naming.factory.url.pkgs=org.jboss.naming.client
                j2ee.clientName=MFEA-client
                


                My beans:


                *
                 * BeanSessionBean.java
                 *
                 * Created on 8 novembre 2006, 17:00
                 *
                 * To change this template, choose Tools | Template Manager
                 * and open the template in the editor.
                 */
                
                package bean;
                
                import javax.ejb.Remote;
                import javax.ejb.Stateless;
                
                /**
                 *
                 * @author hrm
                 */
                @Stateless
                public class BeanSessionBean implements bean.BeanSessionRemote, bean.BeanSessionLocal {
                
                 /** Creates a new instance of BeanSessionBean */
                 public BeanSessionBean() {
                 }
                 public double AriaryToFmg(double ariary)
                 {
                 return ariary/5;
                 }
                
                }
                


                My local:
                
                package bean;
                
                import javax.ejb.Local;
                
                
                /**
                 * This is the business interface for BeanSession enterprise bean.
                 */
                @Local
                public interface BeanSessionLocal {
                
                }


                My Remote
                
                package bean;
                
                import javax.ejb.Remote;
                import org.jboss.annotation.ejb.RemoteBinding;
                
                
                /**
                 * This is the business interface for BeanSession enterprise bean.
                 */
                @RemoteBinding(jndiBinding="MySession")
                public interface BeanSessionRemote {
                 public double AriaryToFmg(double ariary);
                }


                and now my AppClient:
                 public static void main(String[] args) {
                
                 try {
                 Context c = new InitialContext();
                 Context ce = (Context)c.lookup("java:comp/env/ejb");
                 Object b = c.lookup("java:comp/env/ejb/BeanSessionBean");
                
                
                 /*NamingEnumeration li = ce.list("");
                 while(li.hasMore())
                 {
                 NameClassPair a= (NameClassPair)li.next();
                 System.out.println("ClassName="+a.getClassName()+" Name="+a.getName());;
                 }*/
                 } catch (NamingException ex) {
                 ex.printStackTrace();
                 }
                 }
                


                And I got the next exception:
                javax.naming.NamingException: Could not dereference object [Root exception is javax.naming.NameNotFoundException: BeanSessionBean not bound]
                 at org.jnp.interfaces.NamingContext.resolveLink(NamingContext.java:1052)
                 at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:685)
                 at org.jboss.naming.client.java.javaURLContextFactory$EncContextProxy.invoke(javaURLContextFactory.java:120)
                 at $Proxy0.lookup(Unknown Source)
                 at javax.naming.InitialContext.lookup(InitialContext.java:351)
                 at myfirstenterpriseapplication.Main.main(Main.java:43)
                Caused by: javax.naming.NameNotFoundException: BeanSessionBean not bound
                 at org.jnp.server.NamingServer.getBinding(NamingServer.java:514)
                 at org.jnp.server.NamingServer.getBinding(NamingServer.java:522)
                 at org.jnp.server.NamingServer.getObject(NamingServer.java:528)
                 at org.jnp.server.NamingServer.lookup(NamingServer.java:281)
                 at sun.reflect.GeneratedMethodAccessor75.invoke(Unknown Source)
                 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                 at java.lang.reflect.Method.invoke(Method.java:585)
                 at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:294)
                 at sun.rmi.transport.Transport$1.run(Transport.java:153)
                 at java.security.AccessController.doPrivileged(Native Method)
                 at sun.rmi.transport.Transport.serviceCall(Transport.java:149)
                 at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:466)
                 at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:707)
                 at java.lang.Thread.run(Thread.java:595)
                 at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:247)
                 at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:223)
                 at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:126)
                 at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
                 at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:610)
                 at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:572)
                 at javax.naming.InitialContext.lookup(InitialContext.java:351)
                 at org.jnp.interfaces.NamingContext.resolveLink(NamingContext.java:1046)
                 ... 5 more
                


                • 5. Re: Problem when trying to access an EJB bean in an Enterpri
                  jaikiran pai Master

                   

                  <?xml version="1.0" encoding="UTF-8"?>
                  <jboss-client>
                   <jndi-name>MFEA-client</jndi-name>
                   <ejb-ref>
                   <ejb-ref-name>ejb/BeanSessionBean</ejb-ref-name>
                   <jndi-name>BeanSessionBean</jndi-name>
                   </ejb-ref>
                  </jboss-client>


                  Shouldn't the jndi name be "MySession" :

                  <?xml version="1.0" encoding="UTF-8"?>
                  <jboss-client>
                   <jndi-name>MFEA-client</jndi-name>
                   <ejb-ref>
                   <ejb-ref-name>ejb/BeanSessionBean</ejb-ref-name>
                   <jndi-name>MySession</jndi-name>
                   </ejb-ref>
                  </jboss-client>



                  • 6. Re: Problem when trying to access an EJB bean in an Enterpri
                    ANDRIA-NTOANINA Herimiaina Newbie

                    When running the jar
                    I change my jboss-client.xml as you indicated.
                    And I got : MySession not bound

                    Now I reput BeanSessionBean into my jndiname and change

                    @RemoteBinding(jndibinding="MySession")
                    
                    to
                    
                    @Remote
                    


                    In my main application, I try to see what's in the InitialContext by :
                    Context c = new InitialContext();
                     Context ce = (Context)c.lookup("java:comp/env/ejb");
                     //Object b = c.lookup("java:comp/env/ejb/BeanSessionBean");
                    
                     NamingEnumeration li = ce.list("");
                     while(li.hasMore())
                     {
                     NameClassPair a= (NameClassPair)li.next();
                     System.out.println("ClassName="+a.getClassName()+" Name="+a.getName());;
                     }
                    


                    in the output, I got:

                    ClassName=javax.naming.LinkRef Name=BeanSessionBean


                    there's my BeanSessionBean in the InitialContext.
                    But when I retry to get my Beans by:
                    Context c = new InitialContext();
                     Context ce = (Context)c.lookup("java:comp/env/ejb");
                     Object b = c.lookup("java:comp/env/ejb/BeanSessionBean");
                    


                    I got something like:
                    BeanSessionBean not bound

                    • 7. Re: Problem when trying to access an EJB bean in an Enterpri
                      jaikiran pai Master

                      I tried it out on my setup and this is what worked for me:

                      application.xml inside helloworld.ear:

                      <?xml version="1.0" encoding="UTF-8"?>
                      <!DOCTYPE application PUBLIC "-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN" "http://java.sun.com/dtd/application_1_3.dtd">
                      <application>
                      
                      
                       <module>
                       <java>
                       helloworld-appclient.jar
                       </java>
                       </module>
                      
                      <module>
                       <ejb>
                       helloworld-ejb.jar
                       </ejb>
                       </module>
                      </application>


                      application-client.xml in the helloworld-appclient.jar:
                      <?xml version="1.0" encoding="UTF-8"?>
                      
                      <!DOCTYPE application-client PUBLIC '-//Sun Microsystems, Inc.//DTD J2EE Application Client 1.3//EN'
                      'http://java.sun.com/dtd/application-client_1_3.dtd'>
                      
                      <application-client>
                       <display-name>My App Client</display-name>
                       <ejb-ref>
                       <ejb-ref-name>ejb/someEJB</ejb-ref-name>
                       <ejb-ref-type>Session</ejb-ref-type>
                       <home>com.helloworld.ejb.HelloWorldBeanRemoteHome</home>
                       <remote>com.helloworld.ejb.HelloWorldBeanRemote</remote>
                       </ejb-ref>
                      
                      </application-client>


                      jboss-client.xml inside helloworld-appclient.jar:
                      <jboss-client>
                       <jndi-name>SampleApplicationClient</jndi-name>
                      
                       <ejb-ref>
                       <ejb-ref-name>ejb/someEJB</ejb-ref-name>
                       <jndi-name>com.helloworld.ejb.HelloWorldBean</jndi-name>
                       </ejb-ref>
                      
                      
                      
                      </jboss-client>


                      All these files are deployed on the server and here's the packaging hierarchy:

                      helloworld.ear
                       |
                       |-------------- META-INF
                       | |---------- application.xml
                       |
                       |-------------- helloworld-appclient.jar
                       | |
                       | |----------- META-INF
                       | | |
                       | | |-------- application-client.xml
                       | | |-------- jboss-client.xml
                       |
                       |------------- helloworld-ejb.jar
                       | |
                       | |----------- META-INF
                       | | |
                       | | |-------- ejb-jar.xml
                       | | |-------- jboss.xml
                      


                      Now here's the code which does the lookup:

                      package org.myapp;
                      
                      import javax.naming.Context;
                      import javax.naming.InitialContext;
                      import javax.naming.NamingException;
                      import javax.rmi.PortableRemoteObject;
                      
                      import com.helloworld.ejb.HelloWorldBeanRemoteHome;
                      
                      public class MyAppClient {
                      
                       /**
                       * @param args
                       */
                       public static void main(String[] args) {
                       try {
                       Context ctx = new InitialContext();
                       Object beanHome = ctx.lookup("java:comp/env/ejb/someEJB");
                       System.out.println("Bean home: " + beanHome);
                       HelloWorldBeanRemoteHome home = (HelloWorldBeanRemoteHome) PortableRemoteObject.narrow(beanHome,HelloWorldBeanRemoteHome.class);
                      
                       System.out.println("Client successful");
                      
                       } catch (NamingException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                       }
                       }
                      
                      }
                      


                      Finally, the jndi.properties file which needs to be in the classpath of this class which does the lookup:

                      java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
                      java.naming.provider.url=jnp://localhost:1099
                      java.naming.factory.url.pkgs=org.jboss.naming.client
                      j2ee.clientName=SampleApplicationClient


                      This worked without any issues.

                      • 8. finally, it works!!!!
                        ANDRIA-NTOANINA Herimiaina Newbie

                        First, I look at my jmx-console and in the JNDIView service, I didn't found my EJB Beans. Why? I don't know. But I changed my plateform to J2EE 1.4(I first coded my beans using J2EE 5). Now, when I deploy the beans I found it in the jmx-console.
                        May be there is a problem using EJB3 with my JBoss 4.0.3. I don't know. The essential is it works.

                        CODING the client.

                        I create a simple Java Application. I m using Eclipse.
                        then I imported my ejb.jar and jbossall-client.jar into my project.

                        then there is my class implementation:

                        package maki.oim;
                        
                        import java.rmi.RemoteException;
                        import java.util.Properties;
                        
                        import javax.ejb.CreateException;
                        import javax.naming.Context;
                        import javax.naming.InitialContext;
                        import javax.naming.NamingException;
                        import javax.rmi.PortableRemoteObject;
                        
                        
                        
                        public class Test {
                        
                         private static String driver="org.jnp.interfaces.NamingContextFactory";
                         private static String provider="jnp://localhost:1099";
                         private static String prefixe="org.jboss.naming";
                         public static ConvertisseurRemote getConvertisseurRemote()
                         {
                         try {
                         Properties env = new Properties();
                         env.put(Context.INITIAL_CONTEXT_FACTORY,driver);
                         env.put(Context.PROVIDER_URL,provider);
                         env.put(Context.URL_PKG_PREFIXES,prefixe);
                         InitialContext jndiContext;
                        
                         jndiContext = new InitialContext(env);
                        
                         Object ref = jndiContext.lookup("ConvertisseurBean");
                         ConvertisseurRemoteHome home =
                        
                         (ConvertisseurRemoteHome)PortableRemoteObject.narrow(ref,
                        
                         ConvertisseurRemoteHome.class);
                         return home.create();
                         } catch (NamingException e) {
                         // TODO Auto-generated catch block
                         e.printStackTrace();
                         return null;
                         } catch (RemoteException e) {
                         // TODO Auto-generated catch block
                         e.printStackTrace();
                         return null;
                         } catch (CreateException e) {
                         // TODO Auto-generated catch block
                         e.printStackTrace();
                         return null;
                         }
                         }
                         /**
                         * @param args
                         */
                         public static void main(String[] args) {
                         try {
                         ConvertisseurRemote conv = getConvertisseurRemote();
                         System.out.println(conv.fmgToArriary(25));
                         } catch (RemoteException e) {
                         // TODO Auto-generated catch block
                         e.printStackTrace();
                         }
                         }
                        
                        }
                        


                        It works fine!! :)

                        I've tried coding an enterprise client using what jaikiran show but it crash.
                        May be there's something wrong in my code.Nethertheless, I'll retry and tell you if it success.

                        Thank you!!!