com.amazonaws.AmazonClientException:无法完成传输:连接池关闭

时间:2016-05-18 15:25:29

标签: java spring amazon-web-services amazon-s3 apache-httpclient-4.x

我只是配置了Spring AWS Cloud:

<aws-context:context-credentials>
    <aws-context:simple-credentials access-key="${s3.key}" secret-key="${s3.secret}"/>
</aws-context:context-credentials>

<aws-context:context-resource-loader/>

我试图将文件上传到存储桶

ObjectMetadata meta = new ObjectMetadata();
meta.setContentType(contentType);
meta.setContentLength(bytes.length);

try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes)) {
    TransferManager transferManager = new TransferManager(this.amazonS3);
    Upload upload = transferManager.upload(BUCKET, fileName, bis, meta);
    UploadResult result = upload.waitForUploadResult(); // here is an exception!
}

如果我使用其他方法,则会出现相同的异常:

PutObjectRequest request = new PutObjectRequest(BUCKET, fileName, bis, meta);
request.setCannedAcl(CannedAccessControlList.PublicRead);
amazonS3.putObject(request);

它导致异常Stacktrace:

com.amazonaws.AmazonClientException: Unable to complete transfer: Connection pool shut down
    at com.amazonaws.services.s3.transfer.internal.AbstractTransfer.unwrapExecutionException(AbstractTransfer.java:277)
    at com.amazonaws.services.s3.transfer.internal.AbstractTransfer.rethrowExecutionException(AbstractTransfer.java:261)
    at com.amazonaws.services.s3.transfer.internal.UploadImpl.waitForUploadResult(UploadImpl.java:66)
...

Caused by: java.lang.IllegalStateException: Connection pool shut down
    at org.apache.http.util.Asserts.check(Asserts.java:34)
    at org.apache.http.pool.AbstractConnPool.lease(AbstractConnPool.java:184)
    at org.apache.http.pool.AbstractConnPool.lease(AbstractConnPool.java:217)
    at org.apache.http.impl.conn.PoolingClientConnectionManager.requestConnection(PoolingClientConnectionManager.java:186)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.amazonaws.http.conn.ClientConnectionManagerFactory$Handler.invoke(ClientConnectionManagerFactory.java:72)
    at com.amazonaws.http.conn.$Proxy11.requestConnection(Unknown Source)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:416)
    at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:884)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
    at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:749)
    at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:505)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:317)
    at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:3595)
    at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1382)
    at com.amazonaws.services.s3.transfer.internal.UploadCallable.uploadInOneChunk(UploadCallable.java:131)
    at com.amazonaws.services.s3.transfer.internal.UploadCallable.call(UploadCallable.java:123)
    at com.amazonaws.services.s3.transfer.internal.UploadMonitor.call(UploadMonitor.java:139)
    at com.amazonaws.services.s3.transfer.internal.UploadMonitor.call(UploadMonitor.java:47)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    ... 1 more

看起来PoolingClientConnectionManager已从apache httpclient 4.5.2到达此处,我也在我的项目中使用

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.2</version>
    </dependency>

但这只是一个假设。问题可能出在其他地方。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我找到了解决方法,但它需要完全更改Amazon S3配置的方法:

<bean id="basicAwsCredentials" class="com.amazonaws.auth.BasicAWSCredentials">
    <constructor-arg name="accessKey" value="${s3.key}"/>
    <constructor-arg name="secretKey" value="${s3.secret}"/>
</bean>

<bean id="amazonS3Client" class="com.amazonaws.services.s3.AmazonS3Client">
    <constructor-arg name="awsCredentials" ref="basicAwsCredentials"/>
</bean>

在这样的配置中,一切都像魅力一样。

所以,看起来Spring的aws-context工作错误,或者我错误地使用它。但在以前的配置中,自动装配的AmazonS3实例并非null,因此我不知道它无法正常工作的原因。