Where to invoke ExecutorService.shutdown() in web application

891 views Asked by At

I wonder what is correct place to shutdown ExecutorService in a web application?

According to docs ExecutorService should be shutdown, but what is correct place in the code to do that in a web application?

UPDATE: Sorry for unclarity. Let's consider under Java EE a web based application with MVC (for instance Spring MVC if that matter). It has Controllers->Facades->Services. It doesnt have EJBs.

2

There are 2 answers

0
chrylis -cautiouslyoptimistic- On

"Java EE" covers a wide array of technologies. If you're talking about a servlet, the container will call destroy() when it's being shut down, and you could close out your ExecutorService there. If the service is owned by a managed bean, you could use @PreDestroy to mark a method to do so.

0
Beryllium On

You can get a hook using a ServletContextListener:

@WebListener
public class MyServletContextListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent e) {
        log.info("Hello");
    }

    public void contextDestroyed(ServletContextEvent e) {
        log.info("Bye");

        // Do cleanups here
    }
}