Tomcat ServletContextListener.contextDestroyed not called

We have several MemoryLeaks (found in the catalina.out file), reloading the context.

To clear these threads, I created an implementation of ServletContextListener.

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

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

Do I have to implement another interface that I need to notice when I need to reload the context?

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");
        }
    }
}
+6
source share
2 answers

As far as I can see, there are several problems:

  • contextDestroyed, . , Tomcat , .: -/
  • , (, destroy ), , contextDestroyed - , ,: - (
0

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

:

Commons Daemon procrun stdout initialized
START
STOP

, contextDestroyed , ... org.slf4j.Logger

, :

0

All Articles