启动了名为MultiThreadedHttpConnectionManager清理的线程,但未能将其停止

我正在使用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),所以我在方法的第一个语句上放置了一个断点,停止服务器并且断点被点击我逐步执行了该方法确保不会引发exception,并且方法的每一行都没有问题地执行。 这是我的源代码:

 @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. 

这是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.
*/ @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 } } } }

更新到AWS Java SDK 1.2.1版解决了这个问题。 自我于2011年5月26日发布以来,我在发布问题时尚未提供此版本。

AWS SDK 1.2。*使用2009年发布的HttpClient 4,但AWS Java SDK仍在1.1。*版本中使用版本3。 自从我的问题解决以来,我不会进一步详细分析他们的代码更改。

我在axis2 web服务中也遇到了类似的错误,发现它是一个在axis2 1.6版本中修复的轴错误 。

虽然这个问题是针对AWS的,但我已经为那些正在寻找类似的axis2错误的人提供了答案。