【发布时间】:2011-05-23 15:33:15
【问题描述】:
我正在使用 Amazon Web Services AWS Java SDK 编写一个 Web 应用程序。 Apache commons HttpClient 版本 3 在幕后使用。我已经包含了 commons-httpclient-3.0.1.jar。
我的 catalina.out 中有以下警告
SEVERE: The web application [/MyAppName] appears to have started a thread named
[MultiThreadedHttpConnectionManager cleanup] but has failed to stop it. This is
very likely to create a memory leak.
所以我写了一个 ServletContextListener 调用 contextDestroyed() 方法:
MultiThreadedHttpConnectionManager.shutdownAll();
但是,尽管调用了该方法,警告仍然显示。我还应该做些什么来确保清理干净?
编辑:我想绝对确定 contextDestroyed() 确实被调用了(根据 nos 的建议),所以我在方法的第一个语句上放置了一个断点,停止了服务器和断点被击中我一步一步地执行该方法以确保没有引发异常,并且该方法的每一行都没有问题地执行。这是我的源代码:
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("contextDestroyed() start");
MyMemCache.shutDown();
MultiThreadedHttpConnectionManager.shutdownAll();
ClassLoader contextClassLoader=Thread.currentThread().getContextClassLoader();
LogFactory.release(contextClassLoader);
java.beans.Introspector.flushCaches();
System.out.println("contextDestroyed() end");
}
热插拔时:
INFO: Reloading Context with name [/MyAppName] has started
contextDestroyed() start
contextDestroyed() end
05 24, 11 3:11:00 PM org.apache.catalina.loader.WebappClassLoader
clearReferencesThreads
SEVERE: The web application [/MyAppName] appears to have started a thread
named [MultiThreadedHttpConnectionManager cleanup] but has failed to
stop it. This is very likely to create a memory leak.
【问题讨论】:
-
你 100% 确定你的 contextDestroyed() 被调用了吗?
-
把 try catch 放到 contextDestroyed 方法中。
-
好的,我正在为 Exception 添加一个 try catch,并打印堆栈跟踪。但是,如果引发异常,则不会打印字符串“contextDestroyed() end”。如果有什么被抓到我会更新...
-
更新是:我添加了try/catch,我继续开发其他东西,经历了许多重新部署,我从未见过该方法抛出异常。
-
实际上用 MultiThreadedHttpConnectionManager.shutdownAll() 监听;为我做了这项工作。线程正确关闭..
标签: java tomcat amazon-web-services