| Intercepting JBoss Shutdown |
|
|
|
| Written by F.Marchioni | |||||
| Monday, 25 January 2010 16:59 | |||||
|
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.
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!");
}
}
<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. 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.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 ? ![]()
JBoss.org Search
Custom Search
Only registered users can write comments!
Powered by !JoomlaComment 3.26
3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved." |
|||||
| Last Updated ( Tuesday, 26 January 2010 10:17 ) |






