本文介绍了com.amazonaws.AmazonClientException:无法完成传输:连接池关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经简单地配置了 Spring AWS Cloud:

I have simply configured 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/>

我正在尝试将文件上传到存储桶

I'm trying to upload file onto bucket

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!
}

如果我使用其他方法也会出现同样的异常:

Same exception appears if I use another method:

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

它导致异常堆栈跟踪:

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 到达这里,我也在我的项目中使用了它

It looks like PoolingClientConnectionManager has arrived here from apache httpclient 4.5.2 which I also use in my project

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

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

But it is just an assumption. The problem may be some where else. Any ideas?

推荐答案

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

I've found workaround, but it requires completely change method of Amazon S3 configuration:

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

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

In such configuration everything works like a charm.

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

So, it looks like Spring's aws-context works wrongly, or I use it wrongly. But in previous configuration autowired AmazonS3 instance wasn't null, so I don't know the reason why it doesn't work properly.

这篇关于com.amazonaws.AmazonClientException:无法完成传输:连接池关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 19:29