Intercepting JBoss Shutdown

In production environments, JVMs are killed and restarted either automatically by network management systems or manually by a network administrator. When a JVM is killed, it is often necessary to perform some cleanup work before the JVM finishes shutting down.

Intercepting JBoss AS shutdown can be useful in some circumstances, for example if you want to make sure that some housework is performed before shutting down. Here we will show some examples some of which are portable also on other containers.

Approach #1 Add a shutdown hook to your application server.

A shut down hook is simply a Thread that is registered with the JVM and run just before the JVM shuts down. You can add it to any Class that is instantiated by your container. In this example we will add it to the init() method of a Servlet which is registered as startup Servlet.
package sample;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 
public class ServletShutdownHook extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
     
    public ServletShutdownHook() {
        super();
        
    }

    public void init() {
    MyShutdown sh = new MyShutdown(this);
        Runtime.getRuntime().addShutdownHook(sh);
        System.out.println("Added shutdown hook");        
    }
     
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

     
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         
    }

    class MyShutdown extends Thread {
        public MyShutdown(ServletShutdownHook managedClass) {
            super();
            this.managedClass = managedClass;
        }
        private ServletShutdownHook managedClass;
        public void run() {
            System.out.println("MyShutDown Thread started");
            try {
                managedClass.freeResources();
            } catch (Exception ee) {
                ee.printStackTrace();
            }
        }
    }

    public void freeResources() {
        System.out.println("####################### Freeing resources here!");

    }

}
And here is its mapping:
<servlet>
    <servlet-name>ServletShutdownHook</servlet-name>
    <servlet-class>sample.ServletShutdownHook</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServletShutdownHook</servlet-name>
    <url-pattern>/hook</url-pattern>
  </servlet-mapping>
Here, the critical piece of code is in the init() method which registers the Shutdown hook on the class MyShutDown.

MyShutdown sh = new MyShutdown(this);
Runtime.getRuntime().addShutdownHook(sh);  

The run() method in the Class MyShutdown will be fired as soon as shutdown kicks in and recalls the method freeResources from the Servlet.

Approach #2 Add an MBean to your deployments and override the stopService method


This approach is less portable since it requires extending the ServiceMBeanSupport Class which exposes JBoss MBeans lifecycle methods.
package com.sample;

import org.jboss.system.ServiceMBeanSupport;


public class StartupService extends ServiceMBeanSupport 
implements 
StartupServiceMBean {

    public StartupService() { }


    @Override
    protected void startService() { }

    @Override
    protected void stopService() throws Exception
    {
        log.info("[StartupService ] Stopping Startup Mbean");
        freeResources();
    }
 
    public void freeResources() {
           // Do housework here
    }

}

package com.sample

import org.jboss.system.ServiceMBean;

public interface StartupServiceMBean extends ServiceMBean {
    public void clearSessions();
}

Here, when the shutdown sequence gets started, and before the MBean deployed is evicted from memory, the stopService method is invoked, which cares to free resources. 
Approach #3 Use @PreDestroy annotation to clean the house

If you application uses EJB to handle resources which need to be evicted at shutdown, then consider using the @PreDestroy annotation as a "finalizator".
@Stateless
public class SampleEJB {

    @PreDestroy
    public void destroy( ) {
      // Deallocate resources acquired here
    }

}

The destroy method will kick in as soon as the bean is being allocated, including JBoss shutdown. 

Just a word of caution: I've tested this one on JBoss successfully but it's not guaranteed to work the same on other application servers- but you don't want to switch to other AS, do you ? 

Related articles available on mastertheboss.com

JBoss MBeans POJO

JMX MBean services are the core building blocks of the JBoss Appl

How to a dump of JNDI tree with JMX console ?

#2 JBoss Howto. This is the recipe of the day

How to configure JBoss to bind to a different IP ?

  If you want to change the jboss.bind.address property use the 

How to shut down JBoss from remote ?

JBoss recipe of the day

How to create multiple instances of an Mbean?

  Supposing you need two instances of an MBean, let's say one fo

JBoss classloader issues

By default JBoss (prior to version 3.2) uses a flat class loading

Follow us on Twitter