1

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.

Beryllium
  • 12,808
  • 10
  • 56
  • 86
Michael Z
  • 3,883
  • 10
  • 43
  • 57
  • Please clarify/focus your question, it's too broad: Servlets only or EJBs as well? From an AsyncServlet, @Asynchronous and EJB MDB point of view I am tempted to say there is no need to search for such a location (and leave it to the container to manage threads). – Beryllium Aug 29 '13 at 13:47

2 Answers2

0

"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.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
0

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
    }
}
Beryllium
  • 12,808
  • 10
  • 56
  • 86