9 Replies Latest reply on Oct 7, 2011 7:29 AM by Guenther Bodlak

    jndi lookup fails in user-thread

    Guenther Bodlak Newbie

      Hi,

       

      I have a ServletContextListener which creates and starts a new Thread when the servlet context gets initialized.

      In that Thread I'm trying to lookup an ejb. The jndi-lookup fails with a NameNotFoundException.

      This code works in Glassfish 3.1 but not in jboss 7.0.2?

      The code is attached.

       

      Is there a problem with my code or this a bug in jboss 7.0.2?

       

      Thanks for your help

      Günther

        • 1. Re: jndi lookup fails in user-thread
          Riccardo Pasquini Novice

          Quite strange that it works in glassfish, as far as i know, the jndi name is "modular" (same module...but war is a different module or do you deploy the ejb in the war?)... you should use "application" if they (jar and war) are packaged in the same ear or "global" if not...

           

          startup logs shows the jndi names available for that ejb...

           

          anyway, i would ilke to suggest you the timer service instead of running a thread in the context listener, in the listener lookup for the timed object and use the jee service... let the application server to be the owner of threading

           

          bye

          1 of 1 people found this helpful
          • 2. Re: jndi lookup fails in user-thread
            Guenther Bodlak Newbie

            Hi Riccardo,

             

            all 3 classes are deployed in a war. So the JNDI-Name should be o.k.

             

            In the application I am working on we need to have the control over some threads - it is not possible to use the timer service ...

             

            Günther

            • 3. Re: jndi lookup fails in user-thread
              Riccardo Pasquini Novice

              can you try the lookup in a managed bean? just for test purposes...

              • 4. Re: jndi lookup fails in user-thread
                Guenther Bodlak Newbie

                Hi,

                 

                I updated the code of StartupListener. It now calls StartupListener.doLookupTest() where I perform the same JNDI-lookup as in TestThread.callTestEjb().

                It works in StartupListener, but it doesn't work in TestThread.

                Here are parts of the server.log:

                 

                the jndi bindings:

                15:28:21,342 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-3) JNDI bindings for session bean named TestEjb in deployment unit deployment "webtest1.war" are as follows:

                    java:global/webtest1/TestEjb!gbo.ejb.TestEjb

                    java:app/webtest1/TestEjb!gbo.ejb.TestEjb

                    java:module/TestEjb!gbo.ejb.TestEjb

                    java:global/webtest1/TestEjb

                    java:app/webtest1/TestEjb

                    java:module/TestEjb

                ...

                the lookup performed in StartupListener which works:

                15:28:25,420 INFO  [gbo.servlet.StartupListener] (MSC service thread 1-1) before lookup of TestEjb

                15:28:25,435 INFO  [gbo.servlet.StartupListener] (MSC service thread 1-1) after lookup of TestEjb

                15:28:25,435 INFO  [gbo.servlet.TestThread] (Thread-20) TestThread started

                15:28:25,435 INFO  [gbo.servlet.TestThread] (Thread-20) before lookup of testEjb

                the lookup in the new Thread which doesn't work:

                15:28:25,435 WARNUNG [gbo.servlet.TestThread] (Thread-20) naming-Exception: javax.naming.NameNotFoundException: java:module/TestEjb

                    at org.jboss.as.naming.InitialContext.lookup(InitialContext.java:55)

                    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:209)

                    at javax.naming.InitialContext.lookup(InitialContext.java:392) [:1.6.0_27]

                    at gbo.servlet.TestThread.callTestEjb(TestThread.java:37) [classes:]

                    at gbo.servlet.TestThread.run(TestThread.java:24) [classes:]

                 

                Thanks for your help

                Günther

                • 5. Re: jndi lookup fails in user-thread
                  Riccardo Pasquini Novice

                  good you can pass the InitialContext instance to a concrete implementation of Runnable interface... build the Thread from that Runnable and... ?

                  • 6. Re: jndi lookup fails in user-thread
                    Guenther Bodlak Newbie

                    Hi,

                     

                    passing the InitialContext instance into the thread does not work.

                    It ends up in a NameNotFoundException

                     

                    Günther

                    • 8. Re: jndi lookup fails in user-thread
                      jaikiran pai Master

                      Creating threads from within the servlet or any EE component isn't recommended by the spec. The JNDI lookup of "app", "module" and "comp" won't work in those threads. See this for details http://lists.jboss.org/pipermail/jboss-as7-dev/2011-June/002292.html

                      1 of 1 people found this helpful
                      • 9. Re: jndi lookup fails in user-thread
                        Guenther Bodlak Newbie

                        Hi,

                         

                        It works with the global namespace: "java:global/webtest1/TestEjb"

                        I appologize not trying that earlier . Sorry!

                         

                        I'm new to JEE and do not really understand the different namespaces.

                        Do you know a good documentation about that topic?

                         

                        Do you think I can get problems using that global namespace in my user-thread?

                        In the user-thread I'd like to delegate all work to an ejb. That ejb depends on other ejbs (using hibernate or jdbc for db-access).

                         

                        Many thanks for your time!

                        Günther