0 Replies Latest reply on Aug 14, 2014 8:15 AM by inc.yg

    Graceful Shutdown: Web-application / EJB tear down before AS Shutdown

    inc.yg

      Hi there,

       

      I'm migrating an existing J2EE (EJB 2.1) web-application from JBoss EAP 5.1 (JBoss AS 5.1.0.GA) to JBoss 7.1.1, and possibly to a later version like EAP 6.2 / JBoss 7.3.0.final.

       

      The web application has an Init-and-Destory servlet which is responsible for starting and shutting down the application. When the servlet's destroy method gets called during AS shutdown it invokes several stateless session beans to execute their business logic related shutdown code. Some of those EJBs make use of a Timer for regular data processing. This Timer based continuous data processing as well as other processing work needs to be shut down properly before the AS shuts downs, so these Timers are cancelled within that EJB business logic shutdown code.

       

      For JBoss 5.1.0.GA that servlet destroy approach works for shutting down the application before the JBoss shuts down. For AS 7.1.1 I get the "java.lang.IllegalStateException: No EJB client context is available" when the servlet's destroy method invokes EJBs containing the business logic shutdown code. I assume this exception indicates that the EJB layer does not exist anymore when the servlet's destroy method is invoked.

       

       

      My question is really how to properly stop/shutdown a web-application using multiple session beans (EJBs) doing continued data processing triggered by timers before a JBoss 7 shuts down for good.

       

      Assuming that JBoss 7.* now destroys the EJB layer of a web-application before the servlet layer, I wonder why this is done? It just feels like the wrong order to me.

       

       

      So far:

      I've looked at this thread https://community.jboss.org/thread/196856

      So, if I'd upgrade to EJB 3.1, create a @Startup/@Singleton bean with a @PreDestroy method that invokes my stateless session beans (also using @DependsOn and @EJB injection) and use at least JBoss 7.2.0.final, I could gracefully shutdown the application business logic before the AS shuts down. Right? For example this this code:

       

      @Startup
      @Singleton(name = "ShutdownBeanEJB")
      @DependsOn({"ServiceOne", "ServiceTwo", "ServiceThree"})
      public class ShutdownBean {
      
          private static final Logger log = Logger.getLogger(ShutdownBean.class);
      
          @EJB
          ServiceOne serviceOne;
      
          @EJB
          ServiceTwo serviceTwo;
      
          @EJB
          ServiceThree serviceThree;
      
          @PreDestroy
          private void shutdown() {
              log.info("Shuting down services...");
      
              serviceOne.shutdown();
              serviceTwo.shutdown();
              serviceThree.shutdown();
      
              log.info("Services shutdown");
           }
      }
      
      
      

       

      Other than that, I could implement an MBean (org.jboss.system.ServiceMBeanSupport) and override the stopService() method to call my EJBs to invoke their shutdown code. However, this would make my application JBoss AS dependent.

       

      Any thoughts are appreciated. Thank you.