4 Replies Latest reply on Jan 14, 2008 9:03 AM by jaikiran pai

    What is wrong with my local ejb JNDI lookup code (or the ejb

    j w Newbie

      I'm trying to perform a JNDI lookup on a local stateless session bean.

      When I use the jboss jmx console to check the JNDI name space I get the following:

      +- HandleDelegate (class: org.jboss.proxy.ejb.handle.HandleDelegateImpl)
      +- ORB (class: org.jacorb.orb.ORB)
      +- env (class: org.jnp.interfaces.NamingContext)
      | +- ejb (class: org.jnp.interfaces.NamingContext)
      | | +- DddEJBBeanLocal[link -> local/DddEJBBean@16619879] (class: javax.naming.LinkRef)
      | +- DddEJBRemoteHome[link -> DddEJBBean] (class: javax.naming.LinkRef)



      The code I am using for the JNDI lookup is as follows:
      private DddEJBLocal lookupDddEJBBean() {
       try {
       Context c = new InitialContext();
       DddEJBLocalHome rv = (DddEJBLocalHome) c.lookup("java:comp/env/ejb/DddEJBBeanLocal");
       return rv.create();
       } catch (NamingException ne) {
       java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ne);
       throw new RuntimeException(ne);
       } catch (CreateException ce) {
       java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ce);
       throw new RuntimeException(ce);
       }
       }



      The ejb-jar.xml is as follows:


      <?xml version="1.0" encoding="UTF-8"?>
      <ejb-jar version="2.1" 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/ejb-jar_2_1.xsd">
       <enterprise-beans>
       <session>
       <display-name>DddEJBSB</display-name>
       <ejb-name>DddEJBBean</ejb-name>
       <home>ddd.ejb.DddEJBRemoteHome</home>
       <remote>ddd.ejb.DddEJBRemote</remote>
       <local-home>ddd.ejb.DddEJBLocalHome</local-home>
       <local>ddd.ejb.DddEJBLocal</local>
       <ejb-class>ddd.ejb.DddEJBBean</ejb-class>
       <session-type>Stateless</session-type>
       <transaction-type>Container</transaction-type>
      
       <ejb-ref>
       <description>DddEJBBean</description>
       <ejb-link>dddEAR-ejb.jar#DddEJBBean</ejb-link>
       <ejb-ref-name>DddEJBRemoteHome</ejb-ref-name>
       <ejb-ref-type>Session</ejb-ref-type>
       <home>ddd.ejb.DddEJBRemoteHome</home>
       <remote>ddd.ejb.DddEJBRemote</remote>
       </ejb-ref>
      
       <ejb-local-ref>
       <description>DddEJBBean</description>
       <ejb-link>dddEAR-ejb.jar#DddEJBBean</ejb-link>
       <ejb-ref-name>ejb/DddEJBBeanLocal</ejb-ref-name>
       <ejb-ref-type>Session</ejb-ref-type>
       <local-home>ddd.ejb.DddEJBLocalHome</local-home>
       <local>ddd.ejb.DddEJBLocal</local>
       </ejb-local-ref>
      
       </session>
       </enterprise-beans>
       <assembly-descriptor>
       <container-transaction>
       <method>
       <ejb-name>DddEJBBean</ejb-name>
       <method-name>*</method-name>
       </method>
       <trans-attribute>Required</trans-attribute>
       </container-transaction>
       </assembly-descriptor>
       </ejb-jar>


      I get the the following error:

      javax.naming.NameNotFoundException: ejb not bound
      at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
      at org.jnp.server.NamingServer.getBinding(NamingServer.java:537)
      at org.jnp.server.NamingServer.getObject(NamingServer.java:543)
      at org.jnp.server.NamingServer.lookup(NamingServer.java:267)
      at org.jnp.server.NamingServer.lookup(NamingServer.java:270)
      at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:625)
      at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:716)
      at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
      at javax.naming.InitialContext.lookup(InitialContext.java:351)
      at ddd.ejb.DddHelper.lookupDddEJBBean(DddHelper.java:32)



      QUESTION#1:

      What is wrong with my lookup code or the ejb-jar.xml configuration?


      QUESTION#2

      Are proprietary JBoss configuration files (e.g., jboss.xml) required in order to successfully perform a JNDI lookup on the above mentioned bean?


      Thanks for any help! --sd



      [More information... if needed]

      ***ddd.ejb.DddEJBBean***
      package ddd.ejb;
      
      import javax.ejb.SessionBean;
      import javax.ejb.SessionContext;
      
      /**
       *
       * @author sairndain
       */
      public class DddEJBBean implements SessionBean {
      
       private SessionContext context;
      
       // <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click the + sign on the left to edit the code.">;
      
       // TODO Add code to acquire and use other enterprise resources (DataSource, JMS, enterprise bean, Web services)
       // TODO Add business methods or web service operations
      
       /**
       * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
       */
       public void setSessionContext(SessionContext aContext) {
       context = aContext;
       }
      
       /**
       * @see javax.ejb.SessionBean#ejbActivate()
       */
       public void ejbActivate() {
      
       }
      
       /**
       * @see javax.ejb.SessionBean#ejbPassivate()
       */
       public void ejbPassivate() {
      
       }
      
       /**
       * @see javax.ejb.SessionBean#ejbRemove()
       */
       public void ejbRemove() {
      
       }
      
       // </editor-fold>;
      
       /**
       * See section 7.10.3 of the EJB 2.0 specification
       * See section 7.11.3 of the EJB 2.1 specification
       */
       public void ejbCreate() {
       // TODO implement ejbCreate if necessary, acquire resources
       // This method has access to the JNDI context so resource aquisition
       // spanning all methods can be performed here such as home interfaces
       // and data sources.
       }
      
       private String field1 = "...value of field1 in DddEJBBean.java...";
       public String getField1() {
      System.out.println(".................DddEJBBean/getField1()..............");
      
       return this.field1;
       }
      
       // Add business logic below. (Right-click in editor and choose
       // "EJB Methods > Add Business Method" or "Web Service > Add Operation")
      
      }
      




      ***ddd.ejb.DddHelper***
      package ddd.ejb;
      
      import javax.ejb.CreateException;
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      
      /**
       *
       * @author sairndain
       */
      public class DddHelper {
      
      
       private String field1 = "...value of field1 in DddHelper.java...";
       public String getField1()
       {
      System.out.println(".................DddHelper/getField1()..............");
      
       DddEJBLocal localref = this.lookupDddEJBBean();
       return localref.getField1() + this.field1;
       }
      
       private DddEJBLocal lookupDddEJBBean() {
       try {
       Context c = new InitialContext();
       DddEJBLocalHome rv = (DddEJBLocalHome) c.lookup("java:comp/env/ejb/DddEJBBeanLocal");
       return rv.create();
       } catch (NamingException ne) {
       java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ne);
       throw new RuntimeException(ne);
       } catch (CreateException ce) {
       java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ce);
       throw new RuntimeException(ce);
       }
       }
      }
      



      ***ddd.web.WelcomeJSFBean***
      package ddd.web;
      
      import ddd.ejb.DddHelper;
      
      /**
       *
       * @author sairndain
       */
      
      public class WelcomeJSFBean {
      
       /** Creates a new instance of WelcomeJSFBean */
       public WelcomeJSFBean() {
       }
      
       private String field1 = "...value of field1 in WelcomeJSFBean.java...";
       public String getField1()
       {
      System.out.println(".................WelcomeJSFBean/getField1()..............");
       DddHelper dh = new DddHelper();
       return dh.getField1() + this.field1;
       }
      }



      ***web.xml***
      <?xml version="1.0" encoding="UTF-8"?>
      <web-app version="2.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/web-app_2_4.xsd">
       <servlet>
       <servlet-name>Faces Servlet</servlet-name>
       <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
       <load-on-startup>1</load-on-startup>
       </servlet>
       <servlet-mapping>
       <servlet-name>Faces Servlet</servlet-name>
       <url-pattern>*.faces</url-pattern>
       </servlet-mapping>
       <session-config>
       <session-timeout>
       30
       </session-timeout>
       </session-config>
       <welcome-file-list>
       <welcome-file>welcomeJSF.faces</welcome-file>
       </welcome-file-list>
      </web-app>
      



      ***

        • 1. Re: What is wrong with my local ejb JNDI lookup code (or the
          j w Newbie

          Well... It appears that the only way to get this to work is to configure the jboss.xml configuration file...

          I configured the jboss.xml file similarly to this:

          <?xml version="1.0" encoding="UTF-8"?>
          <jboss>
           <enterprise-beans>
           <session>
           <ejb-name>JjjEJBBean</ejb-name>
           <jndi-name>ejb/JjjEJBRemoteHome</jndi-name>
           <local-jndi-name>ejb/JjjEJBLocalHome</local-jndi-name>
           </session>
           </enterprise-beans>
          </jboss>
          


          ...and it worked.

          The question I still have is: Should this also be able to work without "jboss.xml" (or other proprietary) configuration files?

          Thanks again

          sd

          • 2. Re: What is wrong with my local ejb JNDI lookup code (or the
            wayne baylor Apprentice

            in my experience you can't do the lookup without jboss.xml.

            as far as JNDI goes, jboss.xml is used to override the default JNDI name of beans (which is what you did), and like jboss-web.xml it can provide the mapping from ENV names to the global JNDI namespace.

            • 3. Re: What is wrong with my local ejb JNDI lookup code (or the
              j w Newbie

              Apparently, jboss.xml, jboss-web.xml, jboss-application.xml are not required... at least in this simple "enterprise" project I created.

              What is not clear is whether the jboss*.xml configuration files allow JBoss to provide some other benefit -- other than short JNDI names.

              Anyway this is the code for those interested I've included the code in this post (see below)...

              If anyone knows what other benefits the "jboss*.xml" provide, please post it... -Thanks in advance!

              sd


              environment:
              Windows XP
              NetBeans 6.0 ide
              JBoss4.0.4GA
              EJB 2.1
              J2EE 1.4.x


              ----------EAR----------
              application.xml configuration file

              <?xml version="1.0" encoding="UTF-8"?>
              <application 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_1_4.xsd">
               <display-name>jjjEAR</display-name>
               <module>
               <web>
               <web-uri>jjjEAR-war.war</web-uri>
               <context-root>/jjjEAR-war</context-root>
               </web>
               </module>
               <module>
               <ejb>jjjEAR-ejb.jar</ejb>
               </module>
              </application>
              


              ----------EJB Module----------

              jjj.ejb.JjjEJBBean stateless session bean (I'm not including the remote and local interface files here because they are standard fare)
              package jjj.ejb;
              
              import javax.ejb.SessionBean;
              import javax.ejb.SessionContext;
              
              public class JjjEJBBean implements SessionBean {
              
               private SessionContext context;
              
               // <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click the + sign on the left to edit the code.">;
              
               // TODO Add code to acquire and use other enterprise resources (DataSource, JMS, enterprise bean, Web services)
               // TODO Add business methods or web service operations
              
               /**
               * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
               */
               public void setSessionContext(SessionContext aContext) {
               context = aContext;
               }
              
               /**
               * @see javax.ejb.SessionBean#ejbActivate()
               */
               public void ejbActivate() {
              
               }
              
               /**
               * @see javax.ejb.SessionBean#ejbPassivate()
               */
               public void ejbPassivate() {
              
               }
              
               /**
               * @see javax.ejb.SessionBean#ejbRemove()
               */
               public void ejbRemove() {
              
               }
              
               public void ejbCreate() {
               }
              
               private String field1 = "...value of field1 in jjj.ejb.JjjEJBBean (in EJB module)...";
               public String getField1() {
               return this.field1;
               }
              }
              


              ejb-jar.xml configuration file
              <?xml version="1.0" encoding="UTF-8"?>
              <ejb-jar version="2.1" 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/ejb-jar_2_1.xsd">
               <enterprise-beans>
               <session>
               <display-name>JjjEJBSB</display-name>
               <ejb-name>JjjEJBBean</ejb-name>
               <home>jjj.ejb.JjjEJBRemoteHome</home>
               <remote>jjj.ejb.JjjEJBRemote</remote>
               <local-home>jjj.ejb.JjjEJBLocalHome</local-home>
               <local>jjj.ejb.JjjEJBLocal</local>
               <ejb-class>jjj.ejb.JjjEJBBean</ejb-class>
               <session-type>Stateless</session-type>
               <transaction-type>Container</transaction-type>
               </session>
              
               <ejb-ref>
               <ejb-ref-name>JjjEJBBean</ejb-ref-name>
               <ejb-ref-type>Session</ejb-ref-type>
               <home>jjj.ejb.JjjEJBRemoteHome</home>
               <remote>jjj.ejb.JjjEJBRemote</remote>
               <ejb-link>jjjEAR-ejb.jar#JjjEJBBean</ejb-link>
               </ejb-ref>
               <ejb-local-ref>
               <ejb-ref-name>JjjEJBLocalHome</ejb-ref-name>
               <ejb-ref-type>Session</ejb-ref-type>
               <local-home>jjj.ejb.JjjEJBLocalHome</local-home>
               <local>jjj.ejb.JjjEJBLocal</local>
               <ejb-link>jjjEAR-ejb.jar#JjjEJBBean</ejb-link>
               </ejb-local-ref>
              
               </enterprise-beans>
               <assembly-descriptor>
               <container-transaction>
               <method>
               <ejb-name>JjjEJBBean</ejb-name>
               <method-name>*</method-name>
               </method>
               <trans-attribute>Required</trans-attribute>
               </container-transaction>
               </assembly-descriptor>
              </ejb-jar>
              



              ----------WEB Module----------

              jjj.web.JjjBean POJO
              package jjj.web;
              
              import java.rmi.RemoteException;
              import javax.ejb.CreateException;
              import javax.naming.Context;
              import javax.naming.InitialContext;
              import javax.naming.NamingException;
              import jjj.ejb.JjjEJBLocal;
              import jjj.ejb.JjjEJBLocalHome;
              import jjj.ejb.JjjEJBRemote;
              import jjj.ejb.JjjEJBRemoteHome;
              
              public class JjjBean {
              
               private String field1RemoteLookup = "...value of field1LocalLookup in jjj.web.JjjBean in WEB module...";
               public String getField1RemoteLookup() throws RemoteException
               {
               JjjEJBRemote remoteRef = this.lookupJjjEJBRemote();
               return remoteRef.getField1() + this.field1RemoteLookup;
               }
              
               private String field1LocalLookup = "...value of field1LocalLookup in jjj.web.JjjBean...";
               public String getField1LocalLookup()
               {
               JjjEJBLocal localRef = this.lookupJjjEJBLocal();
               return localRef.getField1() + this.field1LocalLookup;
               }
              
              
               private JjjEJBLocal lookupJjjEJBLocal() {
               try {
               Context c = new InitialContext();
               JjjEJBLocalHome rv = (JjjEJBLocalHome) c.lookup("java:comp/env/JjjEJBLocalHome");
               return rv.create();
               } catch (NamingException ne) {
               java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ne);
               throw new RuntimeException(ne);
               } catch (CreateException ce) {
               java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ce);
               throw new RuntimeException(ce);
               }
               }
              
               private JjjEJBRemote lookupJjjEJBRemote() {
               try {
               Context c = new InitialContext();
               Object remote = c.lookup("java:comp/env/JjjEJBBean");
               JjjEJBRemoteHome rv = (JjjEJBRemoteHome) javax.rmi.PortableRemoteObject.narrow(remote, jjj.ejb.JjjEJBRemoteHome.class);
               return rv.create();
               } catch (NamingException ne) {
               java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ne);
               throw new RuntimeException(ne);
               } catch (CreateException ce) {
               java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ce);
               throw new RuntimeException(ce);
               } catch (RemoteException re) {
               java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", re);
               throw new RuntimeException(re);
               }
               }
              }
              


              index.jsp web page
              <%@page contentType="text/html"%>
              <%@page pageEncoding="UTF-8"%>
              <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
              <html>
               <head>
               <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
               <title>JSP Page</title>
               </head>
               <body>
               <jsp:useBean id="jjjBean" class="jjj.web.JjjBean" scope="request" />
              
               <h1>value of field1 - remote lookup...</h1>
               <jsp:getProperty name="jjjBean" property="field1RemoteLookup" />
              
               <h1>value of field1 - local lookup...</h1>
               <jsp:getProperty name="jjjBean" property="field1LocalLookup" />
              
               </body>
              </html>
              


              web.xml configuration file
              <?xml version="1.0" encoding="UTF-8"?>
              <web-app version="2.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/web-app_2_4.xsd">
               <session-config>
               <session-timeout>
               30
               </session-timeout>
               </session-config>
               <welcome-file-list>
               <welcome-file>index.jsp</welcome-file>
               </welcome-file-list>
               <ejb-ref>
               <ejb-ref-name>JjjEJBBean</ejb-ref-name>
               <ejb-ref-type>Session</ejb-ref-type>
               <home>jjj.ejb.JjjEJBRemoteHome</home>
               <remote>jjj.ejb.JjjEJBRemote</remote>
               <ejb-link>jjjEAR-ejb.jar#JjjEJBBean</ejb-link>
               </ejb-ref>
               <ejb-local-ref>
               <ejb-ref-name>JjjEJBLocalHome</ejb-ref-name>
               <ejb-ref-type>Session</ejb-ref-type>
               <local-home>jjj.ejb.JjjEJBLocalHome</local-home>
               <local>jjj.ejb.JjjEJBLocal</local>
               <ejb-link>jjjEAR-ejb.jar#JjjEJBBean</ejb-link>
               </ejb-local-ref>
              </web-app>
              




              • 4. Re: What is wrong with my local ejb JNDI lookup code (or the
                jaikiran pai Master

                 

                "sairndain" wrote:


                If anyone knows what other benefits the "jboss*.xml" provide, please post it...



                You can configure classloader for your application through the jboss*.xml files.