Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat ServletContextListener.contextDestroyed not called

We having multiple MemoryLeaks (found in the catalina.out), while reloading the context.

To clean up these threads, I created an implementation of ServletContextListener.

The contextInitialized() method is successfully called when a context is created, because I can see the log entries.

But the contextDestroyed() method is not called so my cleanup code is not invoked. Any ideas why this is happening?

Should I implement another Interface to be noticed when a context needs to be reloaded?

public class MyContextListener implements ServletContextListener {

    private static final Logger log = Logger.getLogger(MyContextListener.class);

    @Override
    public void contextDestroyed(final ServletContextEvent arg0) {
        MyContextListener.log.info("destroying Servlet Context");
        //Do stuff
        MyContextListener.log.info("Servlet Context destroyed");
    }

    @Override
    public void contextInitialized(final ServletContextEvent arg0) {
        try {
            MyContextListener.log.info("Creating Servlet Context");
            //Do stuff
        } finally {
            MyContextListener.log.info("Servlet Context created");
        }
    }
}
like image 834
Mirko Avatar asked May 02 '12 07:05

Mirko


Video Answer


1 Answers

I had the some problem, and I'm "fixed" it by use System.out.println.

  @WebListener
  public class App implements ServletContextListener {
      private static final Logger logger = LoggerFactory.getLogger(App.class);

      @Override
      public void contextInitialized(ServletContextEvent sce) {
          System.out.println("START");
          logger.info("START");
      }

      @Override
      public void contextDestroyed(ServletContextEvent sce) {
          System.out.println("STOP");
          logger.info("STOP");
      }
  }

log.txt:

[%thread] %-5level %logger{0}:%L - %msg%n
[localhost-startStop-1] INFO  App:16 - START

stdout:

Commons Daemon procrun stdout initialized
START
STOP

This is mean that contextDestroyed is called, but logger is already doesn't work... I use org.slf4j.Logger

To fix it, see: Logging from servlet context destroyed event

Sorry for my bad English

like image 165
Mikhail Ionkin Avatar answered Nov 23 '22 15:11

Mikhail Ionkin