JavaExample.net | Detect shutdown on tomcat or Jboss AS

there are many way to detect when a tomcat or jboss application is going to shutdown.

1) servlet method: If you are using a web application you can use a destroy method on a servlet

public void destroy()
{
// things to do at shutdown
}

2) listener method (useful when our web application is packed into a .war):
we define a listener in the web.xml of our web application

my.package.mylistener

we can use following method to hook the startup of application

public void contextInitialized(ServletContextEvent event)
    {
        try
        {
            this.myInitialLogic();
        }
        catch(ServletException se)
        {
            se.printStackTrace();
        }
    }

3) java standard hook:
another approach that we can use in a simple java class is the following

Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
   // thinks to do at shutdown
}
});

Tagged with →  
Share →

Leave a Reply

Your email address will not be published. Required fields are marked *