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 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() {

    public void init() {
    MyShutdown sh = new MyShutdown(this);
        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) {
            this.managedClass = managedClass;
        private ServletShutdownHook managedClass;
        public void run() {
            System.out.println("MyShutDown Thread started");
            try {
            } catch (Exception ee) {

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


And here is its 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);

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 
StartupServiceMBean {

    public StartupService() { }

    protected void startService() { }

    protected void stopService() throws Exception
    {"[StartupService ] Stopping Startup Mbean");
    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”.

public class SampleEJB {

    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 ?