1 Reply Latest reply on Nov 29, 2014 12:04 AM by Jay SenSharma

    Access simple bean (non-EJB) using JNDI

    Srinivas Kothuri Newbie

      I'm not able to access my bean class (not EJB) using a given JNDI name in JBoss 5.0.1, where the same is working in Tommcat 7.0.

       

      META-INF/context.xml

          <Context>

              <Resource name="test/TestDAO" auth="Container"

                  type="ws.jndi.MyBeanDAO"

                  factory="org.apache.naming.factory.BeanFactory"/>

          </Context>

       

      WEB_INF/web.xml

        <resource-ref>

          <description>MyBeanDAO</description>

          <res-ref-name>test/TestDAO</res-ref-name>

          <res-type>ws.jndi.MyBeanDAO</res-type>

          <res-auth>Container</res-auth>

        </resource-ref>

       

      Servlet Java Client (in the same web app):

          Context initCtx = new InitialContext();

          MyBeanDAO beanDAO = (MyBeanDAO) initCtx.lookup("java:comp/env/test/TestDAO");

          response.getWriter().println(beanDAO.getMyBean(null).getLastName());

       

      And additionally for JBoss,

      WEB_INF/jboss-web.xml

          <jboss-web>

              <context-root>JNDILookup</context-root>

              <resource-ref>

                  <res-ref-name>test/TestDAO</res-ref-name>

                  <res-type>ws.jndi.MyBeanDAO</res-type>

                  <jndi-name>test/TestDAO</jndi-name>

              </resource-ref>

         </jboss-web>

       

      I've been getting the below exception

          javax.naming.NameNotFoundException: test 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:396)

               at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:722)

               at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:682)

               at javax.naming.InitialContext.lookup(InitialContext.java:411)

               at ws.web.TestJNDI.doGet(TestJNDI.java:43)

      Please let me know if I'm missing anything and suggest me the right way of doing it in JBoss.

        • 1. Re: Access simple bean (non-EJB) using JNDI
          Jay SenSharma Master

          "context.xml" file based approach will not work in AS7/Wildfly.  So if you want to add a custom object in the JNDI tree of your JBoss for remote access then you may need to use the standard options like creating a class (example: MyCustomObjectFactory) by implementing "javax.naming.spi.ObjectFactory"  Then it can be easily accessed from the JNDI tree, locally or remotely.

           

           

          <subsystem xmlns="urn:jboss:domain:naming:1.1">
              <bindings>
                  <object-factory name="java:jboss/exported/test2" module="test.jndi.demo" class="test.jndi.demo.MyCustomObjectFactory"/>
              </bindings>
          </subsystem>
          

           

          The JNDI framework allows for object implementations to be loaded in dynamically via object factories. For example, when looking up a printer bound in the name space, if the print service binds printer names to References, the printer Reference could be used to create a printer object, so that the caller of lookup can directly operate on the printer object after the lookup. An ObjectFactory is responsible for creating objects of a specific type. refer to https://docs.oracle.com/javase/6/docs/api/javax/naming/spi/ObjectFactory.html

           

          Refer to [1] for a simple demo on the same:

           

          [1] Binding & Remote Lookup of Custom Objects in JNDI tree of JBoss AS7.1.1

               http://middlewaremagic.com/jboss/?p=1690