3 Replies Latest reply on Dec 8, 2014 3:31 AM by mkouba

    EnhancedListener destroying CDI context too early

    thfr

      Hi!
      We are using Weld 2.2.1 in our web application running on Tomcat. Our application has 3 ServletContextListeners, one being the Weld listener, and their  order of execution does matter.
      To make sure the listeners are executed in a particular order, we defined all 3 listeners in our application's web.xml. It currently looks like this:

       

      <listener>

         <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>

      </listener>


      <listener>

         <listener-class>MyServletContextListener1</listener-class>

      </listener>


      <listener>

         <listener-class>MyServletContextListener2</listener-class>

      </listener>

       

       

       

       

       

       

       

       

      MyServletContextListener1 and MyServletContextListener2 require CDI, and during startup everything works well.

       

       

       

       

       

      After some research I found out, that Weld's EnhancedListener is registered automatically behind the scenes
      and is put at the end of the ordered list of listeners. Thus the execution order at startup becomes:

       

       

       

      Listener - MyServletContextListener1 - MyServletContextListener2 - EnhancedListener

       

       

       

      and at shutdown

       

       

       

      EnhancedListener - MyServletContextListener2 - MyServletContextListener1 - Listener

       

       

       

      Now the problem is that EnhancedListener.contextDestroyed destroys the BeanManager and thus no CDI beans
      are available to MyServletContextListener2 anymore during the shutdown phase.

       

       

       

       

       

      Is this a bug or a feature?

       

       

       

      What we really want is that
      - on startup Weld is initialiazed before all other listeners
      - on shutdown Weld is shutdown after all other listerners

       

       

       

       

       

      How can we achieve this? Would it help to disable EnhancedListener? If yes, is that possible at all?

       

       

       

      Thanks,
      Thilo