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]: Unknown


原因:如果你使用的spring的注入方式,那么所获取的OSS是一个单例对象。
当使用ossClient.shutdown()时,下一次请求将无法获取连接。

Spring单例对象注入

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

 


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

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对象,可以定义一个方法单独返回。那么每一次调用这个方法都会产生一个新的对象。

1     /**
2      * 获取ossClient对象(多例)
3      * 由于使用完成需要关闭,所以需要创建多例的ossClient对象
4      */
5     private OSS getOssClient(){
6         return ossConfiguration.ossClient();
7     }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM