com.aliyun.oss.ClientException: Connection error due to: Connection pool shut down

com.aliyun.oss.ClientException: Connection error due to: Connection pool shut down
[ErrorCode]: Unknown
[RequestId]: Unknownspring


缘由:若是你使用的spring的注入方式,那么所获取的OSS是一个单例对象。
当使用ossClient.shutdown()时,下一次请求将没法获取链接。ui

Spring单例对象注入spa

1     @Bean
2     public OSS ossClient() {
3         return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
4     }

 


解决方案:使用多例注入@Scope("prototype"),或者直接 new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret)prototype

1     @Bean
2     @Scope("prototype")
3     public OSS ossClient() {
4 //        return new OSSClient(endpoint, accessKeyId, accessKeySecret);
5         return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
6     }

 


获取OSS对象,能够定义一个方法单独返回。那么每一次调用这个方法都会产生一个新的对象。code

1     /**
2      * 获取ossClient对象(多例)
3      * 因为使用完成须要关闭,因此须要建立多例的ossClient对象
4      */
5     private OSS getOssClient(){
6         return ossConfiguration.ossClient();
7     }
相关文章
相关标签/搜索