我的Tomcat 7报告我的webapp可能存在内存泄漏
SEVERE: The web application [/mywebapp] appears to have started a
thread named [pool-1-thread-1] but has failed to stop it. This is
very likely to create a memory leak.
Run Code Online (Sandbox Code Playgroud)
我的webapp中有一个长时间运行的任务,当webapp启动时会被初始化.
public class MyContextListener implements ServletContextListener{
Scheduler scheduler = null;
public MyContextListener(){
scheduler = new Scheduler();
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
scheduler.stop();
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
scheduler.start();
}
}
Run Code Online (Sandbox Code Playgroud)
..和我的Scheduler.java
public class Scheduler {
private final ScheduledExecutorService fScheduler;
public Scheduler() {
fScheduler = Executors.newScheduledThreadPool(1);
} …Run Code Online (Sandbox Code Playgroud) 在我的基于 Servlet 的应用程序中,我想记录启动和关闭的事件。
我试图实现ServletContextListener接口来做到这一点:
public class DiagnosticListener
implements ServletContextListener {
private static final Logger LOG = LogManager.getLogger(DiagnosticListener.class);
@Override
public void contextInitialized( final ServletContextEvent sce ) {
LOG.info("Context initialized.");
}
@Override
public void contextDestroyed( final ServletContextEvent sce ) {
LOG.info("Context destroyed.");
}
}
Run Code Online (Sandbox Code Playgroud)
初始化事件按预期记录,但销毁事件从未出现。我假设这与 log4j2 如何使用类似的侦听器管理其生命周期有关,日志基础结构在此事件期间不再可用。
有没有办法记录正在关闭的应用程序的事件?