簡體   English   中英

AWS開發工具包-AmazonS3Client不會關閉

[英]AWS SDK - AmazonS3Client doesn't shutdown

我發現創建一個AmazonS3Client意味着我的流程即使在什么都不做的情況下也會死機。 我正在上傳文件,但已將其縮減為僅此一個。

當我運行以下代碼(使用有效的憑據)時,它會顯示“簡單完成”,但是直到最終maven:exec告訴我,該過程才會退出:

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

編碼:

    import com.amazonaws.auth.AWSCredentials;
    import com.amazonaws.auth.BasicAWSCredentials;
    import com.amazonaws.services.s3.AmazonS3;
    import com.amazonaws.services.s3.AmazonS3Client;

    public class Simple {

        private static String accessKey = "XXX";
        private static String secretKey = "ZZZ";

        public static void main(String[] args) {

            AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
            AmazonS3 s3client = new AmazonS3Client(credentials);
            System.out.println("Simple is finished");        
        }

    }

它應該像這樣工作嗎? 有辦法殺死它嗎?

編輯:添加版本信息:

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk</artifactId>
    <version>1.9.33</version>
</dependency>

這些似乎工作:

((AmazonS3Client) s3client).shutdown();

要么

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

盡管我不確定它們的正確性。

上面的解決方案有效,這是另外兩個:

  • 手動調用System.gc()(在AmazonS3超出范圍后)
  • 或者,在運行Maven時添加標志exec.cleanupDaemonThreads = false

我遇到了同樣的問題。 如果GC運行,AmazonS3Client實際上將通過垃圾回收進行清理; 但是,根據您的jvm,這可能永遠不會發生。 就我而言,即使使用-Dexec.daemonThreadJoinTimeout = -1運行,這也會使maven無限期地等待線程關閉,但它永遠不會關閉。

aws-java-sdk-s3版本1.10.24

由於我無法發表評論,因此我想為推薦此解決方案的答案提供更多背景信息:

((AmazonS3Client) s3client).shutdown();

根據《 aws開發人員指南》 ,直到客戶端被垃圾回收之前,連接池資源才真正被清除。 調用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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM