AWS SDK - AmazonS3Client doesn't shutdown

时间:2015-05-08 10:03:40

标签: java amazon-web-services

I find that creating an AmazonS3Client means my process hangs around even when it's doing nothing. I'm doing a file upload, but I've trimmed it down to just this.

When I run the following code (with working credentials) it prints "Simple is finished" but the process doesn't exit until eventually maven:exec tells me:

ArticlesController

The code:

<?php
$db_host        = 'localhost';
$db_user        = 'root';
$db_pass        = 'root';
$db_database    = 'drmahima_com';
$link = mysqli_connect($db_host,$db_user,$db_pass,$db_database) or die('Unable to establish a DB connection');
mysqli_query($link, "SET names UTF8");
function temp($link) {
    $patient = mysqli_fetch_assoc(mysqli_query($link, "SELECT name,dob FROM patients WHERE id='69'"));
    echo $patient['name'];
}
temp($link);
?>

Is it supposed to work like this? Is there a way to kill it off?

edit: add version information:

Simple is finished
[WARNING] thread Thread[java-sdk-http-connection-reaper,5,Simple] was interrupted but is still alive after waiting at least 15000msecs
[WARNING] thread Thread[java-sdk-http-connection-reaper,5,Simple] will linger despite being asked to die via interruption
[WARNING] NOTE: 1 thread(s) did not finish despite being asked to  via interruption. This is not a problem with exec:java, it is a problem with the running code. Although not serious, it should be remedied.
[WARNING] Couldn't destroy threadgroup org.codehaus.mojo.exec.ExecJavaMojo$IsolatedThreadGroup[name=Simple,maxpri=10]
java.lang.IllegalThreadStateException

3 个答案:

答案 0 :(得分:5)

其中任何一个似乎都有效:

((AmazonS3Client) s3client).shutdown();

try {
    com.amazonaws.http.IdleConnectionReaper.shutdown();
} catch (Throwable t) {
    // etc
}

虽然我不确定他们是多么正确。

答案 1 :(得分:1)

上述解决方案有效,其他两个方面:

  • 手动调用System.gc()(在AmazonS3超出范围后)
  • 或者,在运行maven时添加标志exec.cleanupDaemonThreads = false

我遇到了同样的问题。如果GC运行,AmazonS3Client实际上将通过垃圾收集进行清理;但是,根据你的jvm,这可能永远不会发生。在我的情况下,即使使用-Dexec.daemonThreadJoinTimeout = -1运行,这使得maven无限期地等待线程关闭,它也永远不会关闭。

aws-java-sdk-s3版本1.10.24

答案 2 :(得分:1)

我想对推荐此解决方案的答案给出更多背景信息,因为我无法发表评论:

((AmazonS3Client) s3client).shutdown();

根据aws developer guide,在客户端被垃圾回收之前,实际上不会清理连接池资源。调用shutdown会强制客户端释放其资源,这就是此解决方案有效的原因。

以下是AmazonWebServiceClient.java(AmazonS3Client的父类)的关闭方法:

/**
 * Shuts down this client object, releasing any resources that might be held
 * open. This is an optional method, and callers are not expected to call
 * it, but can if they want to explicitly release any open resources. Once a
 * client has been shutdown, it should not be used to make any more
 * requests.
 */
public void shutdown() {
    client.shutdown();
}