9 Replies Latest reply on Dec 5, 2003 1:32 PM by fordeeps

    How to use JNDi-Lookup?

    Dirk Newbie

      I just want to instantiate a very very simple EntityBean but I don't know how how the jndi-name must be set.

      Which *.xml files di I need?

      I presently have an ejb-jar.xml and a jbosscmp-jdbc.xml to use a MySql databaseand adapted the mysql-ds.xml

      Do I have to make refereneces between these xml files?

      Within the jboss.xml I have statet this:

      <enterprise-beans>

      <ejb-name>PersonBean</ejb-name>
      <jndi-name>ejb/PersonBean</jndi-name>

      </enterprise-beans>


      Thank you.

      Best Regards,

      Dirk

        • 1. Re: How to use JNDi-Lookup?
          Jon Barnett Master

          Read through http://www.amitysolutions.com.au/documents/JBossJNDI-technote.pdf and see if that explains how things are supposed to work. Otherwise repost if there are things that don't make sense or are unclear.

          • 2. Re: How to use JNDi-Lookup?
            Dirk Newbie

            Thank you, jonlee.

            It's an interesting pdf document.
            However, I couldn't work it out to get my tiny EJB run using JBoss.

            To provide you with more information, here's the code/text I use:

            1. jboss.xml (Do I need this at all?)

            <enterprise-beans>

            <ejb-name>PersonBean</ejb-name>
            <jndi-name>PersonBean</jndi-name>

            </enterprise-beans>


            2. ejb-jar.xml
            <ejb-jar>

            <ejb-name>PersonBean</ejb-name>
            de.duit.ejb.PersonHome
            de.duit.ejb.Person
            <ejb-class>de.duit.ejb.PersonBean</ejb-class>
            <persistence-type>Container</persistence-type>
            <prim-key-class>de.duit.ejb.PersonPK</prim-key-class>
            <primkey-field>person_ID</primkey-field>
            <abstract-schema-name>PersonBeanDB</abstract-schema-name>
            False
            <resource-ref>
            <res-ref-name>EjbTest</res-ref-name>
            <res-type>javax.sql.DataSource</res-type>
            <res-auth>Container</res-auth>
            </resource-ref>

            <cmp-version>2.x</cmp-version>

            <cmp-field>
            <field-name>firstName</field-name>
            </cmp-field>
            <cmp-field>
            <field-name>lastName</field-name>
            </cmp-field>

            <cmp-field>
            <field-name>person_ID</field-name>
            </cmp-field>



            </enterprise-beans>

            <assembly-descriptor>
            <container-transaction>

            <ejb-name>PersonBean</ejb-name>
            <method-name>*</method-name>

            <trans-attribute>Required</trans-attribute>
            </container-transaction>
            </assembly-descriptor>
            </ejb-jar>

            3. The client:
            public class EjbTest
            {
            Person person = null;

            public static void main(String[] args)
            {
            try
            {
            Context initialContext = new InitialContext();
            System.out.println("Calling EJB Externally");
            String lookupString = "PersonBean";
            //String lookupString = "java:comp/env/PersonBean";
            System.out.println("InitialContext with: " + lookupString);
            Object obj = initialContext.lookup(lookupString);
            PersonHome pHome =
            (PersonHome) javax.rmi.PortableRemoteObject.narrow(obj, PersonHome.class);
            Person person = pHome.create("Ulrich", "Dirk");
            System.out.println(person.getFirstName());

            //PersonHome personHome = (PersonHome)initCtx.lookup("java:comp/env/ejb/Example");
            }
            catch(Exception e)
            {
            System.err.println("ddddddddddd");
            e.printStackTrace();
            }
            }//end main
            }//end class EjbTest

            I hope I provided you with all the necessary information to enable you to answer my question.

            Thank you very much in advance,

            Dirk

            • 3. Re: How to use JNDi-Lookup?
              Jon Barnett Master

              OK. You seem to have the EJB definition properly defined. You checked the JNDI bindings for your EJB in jmx-console like the document says, right? And the bean deploys without an error?

              Assuming all this, your problem lies in the call from a remote system (the call comes from an object in a different JVM).

              When using a JVM that doesn't have a local JNDI, you cannot use "java:comp/env/...". This configuration assumes that you have created a local JNDI definition - which is not possible here because you don't have a local JNDI server (one running in the JVM from which you are making the call). Refer to my documentation about how "java:comp/env/..." redirects to the real JNDI reference.

              Anyway, you need to use the real JNDI name, as it is known on the JBoss JNDI server. However, you need to access this remote JNDI server to do the lookup. You do this by setting up the InitialContext.

              try
              {
               Properties jndiProps = new Properties();
               jndiProps.setProperty(Context.INITIAL_CONTEXT_FACTORY,
               "org.jnp.interfaces.NamingContextFactory");
               jndiProps.setProperty(Context.URL_PKG_PREFIXES,
               "org.jboss.naming:org.jnp.interface");
               jndiProps.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099")
               Object reference = (new InitialContext(jndiProps)).lookup
               ("PersonBean");
               PersonHome pHome = (PersonHome)PortableRemoteObject.narrow(reference,
               PersonHome.class);
               ...
              }
              


              You will need jbossall-client.jar in the classpath to run your client.

              Hope that helps.

              • 4. Re: How to use JNDi-Lookup?
                Dirk Newbie

                Thank you, jonlee.

                I came one step further with your help.
                Now I ever get this exception:

                java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
                java.rmi.ServerException: EJBException:; nested exception is:
                javax.ejb.EJBException: Method is not a known CMP field accessor, CMR field accessor, or ejbSelect method: methodName=setEntityContext
                at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:292)
                at sun.rmi.transport.Transport$1.run(Transport.java:148)
                ...

                It's thrown during this statement:
                Person person = pHome.create();

                Dirk

                • 5. Re: How to use JNDi-Lookup?
                  Jon Barnett Master

                  There is something wrong with your create method on the entity bean. Since we don't have any information on what you are doing, code wise, it is a bit difficult to judge the problem. However, it does seem related to the setEntityContext method in your EJB. You've defined it?

                  Note that setEntityContext is called by the container so you've managed to initiate the remote connection. The container is having trouble trying to create an entity instance.

                  • 6. Re: How to use JNDi-Lookup?
                    Dirk Newbie

                    This method looks like this which is generated from Eclipse 2.1 I use:

                    /* (non-Javadoc)
                    * @see javax.ejb.EntityBean#setEntityContext(javax.ejb.EntityContext)
                    */
                    public abstract void setEntityContext(EntityContext arg0)
                    throws EJBException, RemoteException;

                    • 7. Re: How to use JNDi-Lookup?
                      Jon Barnett Master

                      It should not be abstract - you should implement setEntityContext and unsetEntityContext as well as the ejbCreate, ejbStore, ejbLoad, etc.

                      • 8. Re: How to use JNDi-Lookup?
                        Dirk Newbie

                        OK. I will check this and return then. However, I'm sure that I read that this/these methods should be declared abstract when using CMP with EJB 2.0.

                        Dirk

                        • 9. Re: How to use JNDi-Lookup?
                          fordeeps Newbie

                          hi dirk,
                          I have encountered with exactly the same error as you have. I am trying to develop a bean managed entity bean using Jboss as the application server and MS Access as the database.

                          I was wondering if your problem has got solved and if your program is working fine. If so, can you please let me know what you did to make it work?

                          Thanks in advance
                          Deepa.