13 Replies Latest reply on Jun 11, 2012 9:32 AM by jsebfranck

    Get IllegalStateException when lookup EJB in ContextListener of web module

    lwen ma Newbie

      env: Jboss-as-7.0.2-fianal

       

      In a web module's ContextListener, code is like bellow:

       

      public final class ContextListener implements ServletContextListener {
      
          public void contextInitialized(ServletContextEvent event) {
               TestTimerIfc testTimerEjb = (PushTimerIfc) Utils.lookupEJB(TestTimerIfc.jndi);
               testTimerEjb.scheduleTimer();
          }
      
          public void contextDestroyed(ServletContextEvent event) {
          }
      }
      
      

       

      Error log:

      java.lang.IllegalStateException        at org.jboss.msc.value.InjectedValue.getValue(InjectedValue.java:47)
              at org.jboss.as.naming.service.BinderService.getValue(BinderService.java:116)
              at org.jboss.as.naming.service.BinderService.getValue(BinderService.java:43)
              at org.jboss.msc.service.ServiceControllerImpl.getValue(ServiceControllerImpl.java:1216)
              at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:111)
              at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:73)
              at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:173)
              at org.jboss.as.naming.InitialContext.lookup(InitialContext.java:57)
              at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:209)
              at javax.naming.InitialContext.lookup(InitialContext.java:392) [:1.6.0_29]
              at com.xxx.Utils.lookupEJB(Utils.java:200) [xxx_util.jar:]
              at com.xxx.ContextListener.<init>(ContextListener.java:30) [classes:]
      

       

       

      Change the code to use EJB inject, it does work well:

       

      public final class ContextListener implements ServletContextListener {
          @EJB
          TestTimerIfc testTimerEjb;
      
          public void contextInitialized(ServletContextEvent event) {
               testTimerEjb.scheduleTimer();
          }
          public void contextDestroyed(ServletContextEvent event) {
          }
      }
      
        • 1. Re: Get IllegalStateException when lookup EJB in ContextListener of web module
          jaikiran pai Master

          Can you try this against 7.1.0.Beta1 which was released last week?

          • 2. Re: Get IllegalStateException when lookup EJB in ContextListener of web module
            tc7 Newbie

            I just had a similar issue (which brought me to your thread). However mine was trying to access a JDBC connection via JNDI for which I had omitted the <driver> section ...

             

            java.lang.IllegalStateException

                      at org.jboss.msc.value.InjectedValue.getValue(InjectedValue.java:47)

                      at org.jboss.as.naming.service.BinderService.getValue(BinderService.java:116)

                      at org.jboss.as.naming.service.BinderService.getValue(BinderService.java:43)

                      at org.jboss.msc.service.ServiceControllerImpl.getValue(ServiceControllerImpl.java:1216)

                      at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:111)

                      at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:73)

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

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

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

                      at javax.naming.InitialContext.lookup(InitialContext.java:411) [:1.7.0_01]

             

            Not sure if/how this could be related to your scenario. Perhaps the resource simply isn't available for some other reason.

            • 3. Re: Get IllegalStateException when lookup EJB in ContextListener of web module
              jaikiran pai Master

              By the way, looking at your context initialization code, it appears that you are doing some JNDI name lookup of some EJB. There's not guarantee that the correct dependency will be setup for the EJB to be available, unless you either use injection or use the deployment descriptor to setup a ejb-ref (which you can then use for the lookup).

              • 4. Re: Get IllegalStateException when lookup EJB in ContextListener of web module
                lwen ma Newbie

                jaikiran pai 编写:

                 

                By the way, looking at your context initialization code, it appears that you are doing some JNDI name lookup of some EJB. There's not guarantee that the correct dependency will be setup for the EJB to be available, unless you either use injection or use the deployment descriptor to setup a ejb-ref (which you can then use for the lookup).

                Yes, I see lots of hibernate scanner log (which mean entity-manger are not available) after context initialization mthod invoke. seems that use ejb inject is the best selution solution for EJB 3, and for EJB 2 add 'ejb-ref' according http://docs.oracle.com/cd/E12840_01/wls/docs103/webapp/web_xml.html#wp1020090

                • 5. Re: Get IllegalStateException when lookup EJB in ContextListener of web module
                  jsebfranck Newbie

                  It's weird because if we take a look on the Jboss test cases (https://github.com/jbossas/jboss-as/tree/7.1/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/ejb2/reference/global), there are no use of ejb-ref tag in the xml files. I have exactly the same configuration but the same problem (IllegalStateException on home.create() call). Unfortunately, it is not possible for me to migrate all my application to use injection.

                   

                  Are these tests really correct?

                  • 6. Re: Get IllegalStateException when lookup EJB in ContextListener of web module
                    jaikiran pai Master

                    jsebfranck wrote:

                     

                    It's weird because if we take a look on the Jboss test cases (https://github.com/jbossas/jboss-as/tree/7.1/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/ejb2/reference/global), there are no use of ejb-ref tag in the xml files.

                    None of those tests are doing the lookup/invocation during ServletContext initialization. They are looking up and invoking on the beans after the deployment is completely done.

                     

                     

                    jsebfranck wrote:

                     

                    I have exactly the same configuration but the same problem (IllegalStateException on home.create() call). Unfortunately, it is not possible for me to migrate all my application to use injection.

                     

                     

                    More details please. What exact version of AS7, what does your code look like and what's the entire exception stacktrace?

                    • 7. Re: Get IllegalStateException when lookup EJB in ContextListener of web module
                      jsebfranck Newbie

                      I'm working on Jboss 7.1.1. In my case, the lookup is done during a spring bean initialization (afterPropertiesSet) so the deployment is not completely done, but the EJB are deployed before the spring beans. My lookup code and the configuration files are very similar to the test cases :

                       

                      public static <T extends EJBHome> T lookupHome(final Class<T> homeClass, final String beanName) {

                        final EJBHomeLocator<T> locator = new EJBHomeLocator<T>(homeClass, "pm-main", "pm-main-ejb", beanName, "");

                        return EJBClient.createProxy(locator);

                      }

                       

                      And

                       

                      lookupHome(ProductCatalogHome.class, "ProductCatalogEjb").create();

                       

                      The create() call giving the following stacktrace :

                       

                      16:49:59,744 ERROR [org.jboss.ejb3.invocation] (MSC service thread 1-4) JBAS014134: EJB Invocation failed on component ProductCatalogEjb for method public abstract com...ProductCatalog com...ProductCatalogHome.create() throws java.rmi.RemoteException,javax.ejb.CreateException: javax.ejb.EJBException: java.lang.IllegalStateException

                          at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:166) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                          at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:230) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                          at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:304) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                          at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:190) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                          at org.jboss.as.ejb3.remote.EJBRemoteTransactionPropagatingInterceptor.processInvocation(EJBRemoteTransactionPropagatingInterceptor.java:80) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                          at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                          at org.jboss.as.ejb3.component.interceptors.LoggingInterceptor.processInvocation(LoggingInterceptor.java:59) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                          at org.jboss.as.ejb3.component.interceptors.EjbExceptionTransformingInterceptorFactories$1.processInvocation(EjbExceptionTransformingInterceptorFactories.java:65) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                          at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50) [jboss-as-ee-7.1.1.Final.jar:7.1.1.Final]

                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                          at org.jboss.as.ejb3.component.interceptors.AdditionalSetupInterceptor.processInvocation(AdditionalSetupInterceptor.java:32) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                          at org.jboss.as.ee.component.TCCLInterceptor.processInvocation(TCCLInterceptor.java:45) [jboss-as-ee-7.1.1.Final.jar:7.1.1.Final]

                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                          at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                          at org.jboss.as.ee.component.ViewService$View.invoke(ViewService.java:165) [jboss-as-ee-7.1.1.Final.jar:7.1.1.Final]

                          at org.jboss.as.ejb3.remote.LocalEjbReceiver.processInvocation(LocalEjbReceiver.java:179) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                          at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:179) [jboss-ejb-client-1.0.5.Final.jar:1.0.5.Final]

                          at org.jboss.ejb.client.TransactionInterceptor.handleInvocation(TransactionInterceptor.java:43) [jboss-ejb-client-1.0.5.Final.jar:1.0.5.Final]

                          at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:181) [jboss-ejb-client-1.0.5.Final.jar:1.0.5.Final]

                          at org.jboss.ejb.client.ReceiverInterceptor.handleInvocation(ReceiverInterceptor.java:128) [jboss-ejb-client-1.0.5.Final.jar:1.0.5.Final]

                          at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:181) [jboss-ejb-client-1.0.5.Final.jar:1.0.5.Final]

                          at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:136) [jboss-ejb-client-1.0.5.Final.jar:1.0.5.Final]

                          at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:121) [jboss-ejb-client-1.0.5.Final.jar:1.0.5.Final]

                          at org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:104) [jboss-ejb-client-1.0.5.Final.jar:1.0.5.Final]

                          at $Proxy29.create(Unknown Source)    at com.....StandardShippingService.afterPropertiesSet(StandardShippingService.java:56) [pm-main-ejb.jar:]

                          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477) [spring-beans-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417) [spring-beans-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) [spring-beans-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) [spring-beans-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293) [spring-beans-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) [spring-beans-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290) [spring-beans-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192) [spring-beans-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:848) [spring-beans-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:790) [spring-beans-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707) [spring-beans-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480) [spring-beans-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84) [spring-beans-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) [spring-beans-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074) [spring-beans-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) [spring-beans-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) [spring-beans-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293) [spring-beans-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) [spring-beans-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290) [spring-beans-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192) [spring-beans-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585) [spring-beans-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) [spring-context-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) [spring-context-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:282) [spring-web-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:204) [spring-web-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47) [spring-web-3.0.6.RELEASE.jar:3.0.6.RELEASE]

                          at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3392)

                          at org.apache.catalina.core.StandardContext.start(StandardContext.java:3850)

                          at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90)

                          at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)

                          at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)

                          at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [rt.jar:1.6.0_29]

                          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [rt.jar:1.6.0_29]

                          at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_29]

                      Caused by: java.lang.IllegalStateException

                          at org.jboss.msc.value.InjectedValue.getValue(InjectedValue.java:47)

                          at org.jboss.as.ejb3.component.interceptors.SessionBeanHomeInterceptorFactory$1.processInvocation(SessionBeanHomeInterceptorFactory.java:62) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                          at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:228) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                          ... 65 more

                       

                      Any idea? Thanks in advance !

                      • 8. Re: Get IllegalStateException when lookup EJB in ContextListener of web module
                        jsebfranck Newbie

                        I tried to do the same lookup (exactly the same configuration and same code) after jboss startup and it works. So my conclusion is that it is not possible to do a lookup during a servlet initialization or a spring bean initialization. On jboss 4 or jboss 5, it was possible so I have created the following jira : https://issues.jboss.org/browse/AS7-4973.

                        • 9. Re: Get IllegalStateException when lookup EJB in ContextListener of web module
                          jaikiran pai Master

                          Are the ServletContextListener and the EJBs part of the same (sub) deployment? Try packaging the EJBs within the same .war whose ServletContextListener is using that bean.

                          • 10. Re: Get IllegalStateException when lookup EJB in ContextListener of web module
                            jsebfranck Newbie

                            The ejb jar and the war file are in the same EAR archive. Following your advice, I tried to package the EJBs directly in the .war and in that case the lookup works during the servlet initialization.

                             

                            So for my EAR problem, what should I do? Should I try to do some stuff in jboss-deployment-structure.xml file? Should I put my ejb jar file in the ear/lib directory?

                             

                            Thanks in advance !

                            • 11. Re: Get IllegalStateException when lookup EJB in ContextListener of web module
                              jsebfranck Newbie

                              I also tried to package EJB3 sessions in the EAR archive, and in that case it is possible to do the lookup during the servlet initialization. So the problem is only related to the EJB2.

                              • 12. Re: Get IllegalStateException when lookup EJB in ContextListener of web module
                                jaikiran pai Master

                                Take a look at the details of initialize-in-order element here https://docs.jboss.org/author/display/AS71/Developer+Guide#DeveloperGuide-application.xml. Furthermore, the right and portable way to get things working for your servletcontextlistener is to use a ejb-ref in the web.xml for the EJB that you are trying to lookup in that listener. That way the server will know that it has to setup the correct dependencies to prevent the listener from being triggered before the EJB is available. If that isn't an option for you, then use the initialize-in-order in application.xml

                                • 13. Re: Get IllegalStateException when lookup EJB in ContextListener of web module
                                  jsebfranck Newbie

                                  Thank you very much, it works with the initialize-in-order tag in application.xml !