views:

25

answers:

2

Hi,

I have an EJB app. which basically has to execute a SQL query when it is shutdown. A shutdown hook would presumably work, but that way I can't use injected entitymanager/datasource etc.

Is there a way to provide a shutdown hook that can invoke methods on EJB bean?

Our EJB container is JBoss5.1.

Thanks!

A: 

It looks like this works:

public class InitializerServlet extends GenericServlet {

    @Override
    public void init() throws ServletException {
        try {
            InitialContext ctx = new InitialContext();
            ManagerRemote manager = 
                            (ManagerRemote)ctx.lookup("someapp/manager/remote");
            initer.initialize();
            ctx.close();
        } catch(NamingException e) {
            throw new ServletException(e);
        }
    }


    @Override
    public void destroy() {
        try {
            InitialContext ctx = new InitialContext();
            ManagerRemote manager = 
                            (ManagerRemote)ctx.lookup("someapp/manager/remote");
            initer.cleanup();
            ctx.close();
        } catch(NamingException e) {
            throw new ServletException(e);
        }

    }
}
Enno Shioji
A: 

for ejbs the approach would be same as what zwei has mentioned, but to add an initializaiton or cleanup ejb with a method having annotation PreDestroy

keshav.veerapaneni