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     }
相關文章
相關標籤/搜索