4 Replies Latest reply on Feb 15, 2008 7:44 AM by wingtung.leung

    EJB3: @Resource name only EJBContext, not in InitialContext

    wingtung.leung

      I have a simple EJB3 session bean with EJB3 annotations to define external resources in the JNDI tree. Problem is that the lookup only works if I use the SessionContext, without the "java:comp/env" prefix, but NOT with InitialContext(), including the prefix. The latter version throws a NamingException.

      Am I wrong if I assume that using InitialContext and EJBContext for lookup are equivalent, as long I add the prefix in the first case, and leave it out in the second one?

      I used class level annotations to define the mapping:

      @Resources( {
       @Resource(type = Queue.class, name = "jms/vpoToEmotiveQueue", mappedName = "jms/queue/vpoToEmotiveQueue"),
       @Resource(type = Queue.class, name = "jms/varQueue", mappedName = "jms/queue/lemansVarToEmotiveQueue") })
      


      This works fine:
      @Resource private SessionContext sessionContext;
      ...
      sessionContext.lookup("jms/vpoToEmotiveQueue");
      


      But following code raises a NamingException:
      new InitialContext().lookup("java:comp/env/jms/vpoToEmotiveQueue");
      





        • 1. Re: EJB3: @Resource name only EJBContext, not in InitialCont
          jaikiran

           

          new InitialContext().lookup("java:comp/env/jms/vpoToEmotiveQueue");


          It depends on where you are doing the lookup using the InitialContext. Is this being done in the same bean where the @Resource is defined? In which case, this should work. However, if you are doing the lookup in some other component of your application, then this wont work.


          • 2. Re: EJB3: @Resource name only EJBContext, not in InitialCont
            wingtung.leung

             

            "jaikiran" wrote:

            It depends on where you are doing the lookup using the InitialContext. Is this being done in the same bean where the @Resource is defined? In which case, this should work. However, if you are doing the lookup in some other component of your application, then this wont work.


            The lookup using InitialContext is located inside the bean with the @Resource annotation. Exactly the same location as the lookup with SessionContext.

            I also expect it to work, but it does not. :-(

            • 3. Re: EJB3: @Resource name only EJBContext, not in InitialCont
              jaikiran

              Which version of JBoss do you use? I tested this on my local JBoss-4.2.2 setup with an sample application:

              @Stateless
              @Remote ({UserManager.class})
              @RemoteBinding (jndiBinding = "RemoteUserManagerBean")
              @Resources( {
               @Resource(type = Queue.class, name = "jms/A", mappedName = "queue/A")
               })
              
              public class UserManagerBean implements UserManager {
              
               /**
               * Instance of logger
               */
               private static Logger logger = Logger.getLogger(UserManagerBean.class);
              
               @PersistenceContext
               private EntityManager entityManager;
              
               @Resource
               private SessionContext sessionContext;
              
               /**
               * @see org.myapp.ejb.UserManager#getUser(long)
               */
               public User getUser(long id) {
               System.out.println("Entity manager is " + entityManager);
               return null;
               }
              
               /**
               * @see org.myapp.ejb.UserManager#getUsers(java.lang.String)
               */
               public List<User> getUsers(String name) {
               Object obj = sessionContext.lookup("jms/A");
               System.out.println("Looked up through session context = " + obj);
               try {
               Context ctx = new InitialContext();
               Object obj1 = ctx.lookup("java:comp/env/jms/A");
               System.out.println("Looked up java:comp/env/jms/A = " + obj1);
               } catch (Exception e) {
               e.printStackTrace();
               }
               //blah blah blah
               }
              
              


              Both the lookups (through sessionContext and InitialContext) return me the queue object.

              Can you post your code and also the entire exception stacktrace that you are seeing?

              • 4. Re: EJB3: @Resource name only EJBContext, not in InitialCont
                wingtung.leung

                 

                "jaikiran" wrote:
                Which version of JBoss do you use? I tested this on my local JBoss-4.2.2 setup with an sample application:


                JBoss-4.0.5 with extra EJB3 support.

                I have tested it on JBoss-4.2.2, and it works fine there. So we will need to update our EJB3 component, or upgrade JBoss completely.

                The original exception:

                Exception in thread "Main Thread" javax.naming.NameNotFoundException: env 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.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)
                



                Thanks for the solution!