9 Replies Latest reply on Sep 13, 2010 11:18 AM by Wolfgang Knauf

    Ejb3.0 jndi lookup problem

    santhosh pulichinthala Newbie

      /*
      * To change this template, choose Tools | Templates
      * and open the template in the editor.
      */

      package ejb;

      import javax.ejb.EJB;

      /**
      *
      * @author santhosh reddy
      */
      public class HelloClient {

      @EJB 
      private static StatelessSessionExampleRemote sessionBeanBean;

          public static void main(String[] args) {
              System.out.println("Displaying Message using EJB:");
              System.out.println("=================================");
              System.err.println("Name of the Company is : =" + sessionBeanBean.getCompanyname());
              System.err.println("Address of the Company is : =" + sessionBeanBean.getAddress());
              System.err.println("Message is : =" + sessionBeanBean.getResult());
              System.out.println("=================================");
          }

      }

       

       

       

       

      /*
      * To change this template, choose Tools | Templates
      * and open the template in the editor.
      */

      package ejb;

      import javax.ejb.Stateless;

      /**
      *
      * @author santhosh reddy
      */
      @Stateless
      public class StatelessSessionExample implements StatelessSessionExampleRemote {
         
          // Add business logic below. (Right-click in editor and choose
          // "Insert Code > Add Business Method")
           public String getResult() {
              return "Hello World";
          }
          public String getAddress() {
              return "Sec-3,D-16/116,Rohini";
          }
          public String getCompanyname() {
              return "Roseindia.net Pvt.Ltd.";
          }


      }

       

       

       

      /*
      * To change this template, choose Tools | Templates
      * and open the template in the editor.
      */

      package ejb;

      import javax.ejb.Remote;

      /**
      *
      * @author santhosh reddy
      */
      @Remote
      public interface StatelessSessionExampleRemote {
          String getResult();

          String getAddress();

          String getCompanyname();

      }

       

       

      error log:

      ============

      init:
      deps-jar:
      compile-single:
      run-main:
      Displaying Message using EJB:
      Exception in thread "main" java.lang.NullPointerException
      =================================
              at ejb.HelloClient.main(HelloClient.java:22)
      Java Result: 1
      BUILD SUCCESSFUL (total time: 0 seconds)

       

       

      HelloClient.java:22 is

       

       

      sessionBeanBean.getCompanyname()

      i think it is jndi problem.how to setup jndi in 3.0.Actually in 2.0 we will set it in vendor specifiec.xml like jboss.xml.

        • 1. Re: Ejb3.0 jndi lookup problem
          Wolf-Dieter Fink Master

          I suppose you 'HelloClient' is a different JVM, not JBossAppServer.

           

          Do you have a jndi.properties file with the servers information?

          SystemProperties set?

          If you do a lookup in you code do you receive the reference and can call it?

           

          jboss.xml is a deployment descriptor, not for jndi naming.

          • 2. Re: Ejb3.0 jndi lookup problem
            Wolfgang Knauf Master

            Hi initial poster,

             

            how do you start your application client?

            To use injection in an application client, read this: http://community.jboss.org/wiki/HowtouseanapplicationclientinJBoss-5

             

            Hope this helps

             

            Wolfgang

            • 3. Re: Ejb3.0 jndi lookup problem
              santhosh pulichinthala Newbie

              Hi Wolf-Dieter,

              Thanks for the response.Im using java 1.5 and jboss 4.2.2 GA.Im using eclipse which has server pulgin so i added the jboss to the eclipse and i depoly the ejb application on jboss through eclise then i called the client example.I dont set any jndi.properties file.If i want to test with lookup i need the jndi name but i dont know  where to  define my jndi name in 3.0.

               

              Thanks

              santhosh

              • 4. Re: Ejb3.0 jndi lookup problem
                santhosh pulichinthala Newbie

                Hi Wolfgang,

                Im new ejb3.0.I used to work on ejb2.0 the i used to add jndi name in deployment descriptor.But now in ejb3.0 someone is putting the jndi name in mappedname.someone is saying if you use @Ejb it will automatically create th jndi name and lookup creation.I want to know if i want to

                run the ejb applications localy what type jndi name is the best i mean mapped name or @Ejb.But if ejb application is running remotly where to create the jndi name and lookup.Please help me with this.I saw your url but i tried the example it was giving same error as above.

                Thanks

                santhosh

                • 5. Re: Ejb3.0 jndi lookup problem
                  Wolfgang Knauf Master

                  Hi,

                   

                  it is your decision whether you want to use injection (using the "@EJB" annotation) or whether you want to perform a JNDI lookup.

                   

                  A file "jboss.xml" is not needed in both ways.

                   

                  If you want to use injection: import the sample attached to the wiki document ("Stateless.ear") into Eclipse. Now you will find a project consisting of three modules. Take a look at the code and the configuration, and then try to run my sample as described. You might take a look at this page: http://www.cs.hs-rm.de/~knauf/KomponentenArchitekturen2008/stateless/index.html#appclientstart  - it is german, but there are some screenshots which will show you some important settings.

                   

                  If you don't want to inject, but perform a JNDI lookup, you need this code snippet:

                   

                   

                  Properties props = new Properties();
                          props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
                          props.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming.client");
                          props.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099");
                         
                          InitialContext initialContext = new InitialContext(props);
                          StatelessSessionExampleRemote sessionBeanBean = (StatelessSessionExampleRemote) initialContext.lookup("java:InsertYourBeanNameHere");

                   

                   

                  This should do it. Instead of passing Properties to the "InitialContext", you could use a "jndi.properties" file, as Wolf-Dieter suggests.

                  The JNDI name of the bean defaults to "insertearname/insertbeanclass/remote".

                   

                  Hope this helps

                   

                  Wolfgang

                  • 6. Re: Ejb3.0 jndi lookup problem
                    Wolfgang Knauf Master

                    By the way: I think JBoss 4.2.x does not support EJB injection in the client. So, here you will have to do a JNDI lookup.

                     

                    If you upgrade to JBoss 5.1, injection will work. I would suggest you use the most recent JBoss version, as the 4.2 branch is no longer maintained.

                     

                    Best regards

                     

                    Wolfgang

                    • 7. Re: Ejb3.0 jndi lookup problem
                      santhosh pulichinthala Newbie

                      Hi wolfgang,

                      Now im getting this error.

                      Exception in thread "main"

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

                      at org.jboss.naming.client.java.javaURLContextFactory.getObjectInstance(

                      javaURLContextFactory.java:81)

                      at javax.naming.spi.NamingManager.getURLObject(Unknown Source)

                      at javax.naming.spi.NamingManager.getURLContext(Unknown Source)

                      at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)

                      at javax.naming.InitialContext.lookup(Unknown Source)

                      at co.za.eskom.HelloClient.main(

                      HelloClient.java:32)

                       

                      Thanks

                      santhosh

                      • 8. Re: Ejb3.0 jndi lookup problem
                        Wolf-Dieter Fink Master

                        The default naming in EJB3 will be

                        <name of the ear file>/<Bean name>/remote

                         

                        E.g. you deploy application.ear with a bean 'MySLSBean' the default naming is 'application/MySLSBean/remote'

                         

                        If you want to change the JNDI name you might add @org.jboss.annotation.ejb.RemoteBinding(...) to your Bean-class.

                        But it is a non standard (EJB) jboss annotation that change from 4.x=>5.x

                        • 9. Re: Ejb3.0 jndi lookup problem
                          Wolfgang Knauf Master

                          Hi Santhosh,

                           

                          it seems I forgot an important piece of configuration ;-): you need a "j2ee.clientName" property.

                          Take a look at this: http://community.jboss.org/wiki/J2EEClient

                           

                          When initializing the properties, you have to add this one:

                                  Properties props = new Properties();

                                  ...

                                  props.setProperty("j2ee.clientName", "MyClientName");

                           

                          And you have to add a file "jboss-client.xml" to your client app (in "META-INF"):

                          <?xml version="1.0" encoding="UTF-8"?>
                              <!DOCTYPE jboss-client PUBLIC
                                  "-//JBoss//DTD Application Client 4.2//EN"
                                  "http://www.jboss.org/j2ee/dtd/jboss-client_4_2.dtd">
                              <jboss-client>
                                  <jndi-name>MyClientName</jndi-name>
                              </jboss-client>

                           

                          Hope this helps

                           

                          Wolfgang