我正在使用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.
所以我用contextDestroyed()方法编写了一个ServletContextListener,调用:
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.
答案 0 :(得分:2)
这是AWS SDK中的一个错误,它会打开两个HttpClients并仅关闭其中一个。 我的问题略有不同,因为我想在不重新启动webapp的情况下启动和停止各个地区和帐户的客户端。
我已经解决了这个问题(我已向他们报告,但他们似乎并不倾向于修复):
public class MyAmazonEC2Client extends AmazonEC2AsyncClient {
private boolean shutdownCalled = false;
public MyAmazonEC2Client(final AWSCredentials awsCredentials) {
super(awsCredentials);
}
public MyAmazonEC2Client(final AWSCredentials awsCredentials, final ClientConfiguration clientConfiguration) {
this(awsCredentials, clientConfiguration, Executors.newCachedThreadPool());
}
public MyAmazonEC2Client(
final AWSCredentials awsCredentials,
final ClientConfiguration clientConfiguration,
final ExecutorService executor) {
super(awsCredentials, clientConfiguration, executor);
}
/**
* Shuts down this client object, releasing any resources that might be held open.<br />
*/
@Override
public synchronized void shutdown() {
try {
shutdownCalled = true;
// Call the proper shutdown method. This is in the base type and only shuts down one of the HttpClients created (the one that's never used)
super.shutdown(); // close the HttpClient in AmazonWebServiceClient
}
finally {
// Fix a bug in Amazon's implementation where they've duplicated the HttpClients
try {
super.client.shutdown(); // close the HttpClient in AmazonEC2Client
}
catch (Throwable t) {
// ignore failures
}
}
}
}
答案 1 :(得分:2)
更新到AWS Java SDK 1.2.1版解决了这个问题。自我于2011年5月26日发布以来,我在发布问题时尚未提供此版本。
AWS SDK 1.2。*使用2009年发布的HttpClient 4,但AWS Java SDK仍在1.1。*版本中使用版本3。自从我的问题解决后,我不会进一步详细分析他们的代码更改。
答案 2 :(得分:0)
我在axis2 web服务中也遇到了类似的错误,发现它是轴bug,在axis2 1.6版本中已修复。
虽然这个问题已经回答了AWS,但我已经为那些正在寻找类似的axis2错误的人提供了答案。