开发者社区> 问答> 正文

如何回收或者关闭OSSClient实例的连接

以下是我上传文件到OSS的部分代码:

try {
            // 创建OSS客户端
            client = new OSSClient(endpoint, keyId, key, conf);
            
            // 设置过期时间
            Date expires = new Date (new Date().getTime() + 1000 * second);
            
            // 创建请求
            GeneratePresignedUrlRequest generatePresignedUrlRequest =
                    new GeneratePresignedUrlRequest(bucketName, DestPathName);
            generatePresignedUrlRequest.setBucketName(bucketName);
            // HttpMethod为PUT
            generatePresignedUrlRequest.setMethod(HttpMethod.PUT);
            generatePresignedUrlRequest.setExpiration(expires);
            // ContentType必须设置, 否则签名失败
            generatePresignedUrlRequest.setContentType(contentType);
            // 生成签名的URL
            URL url = client.generatePresignedUrl(generatePresignedUrlRequest);
            
            
            HashMap<String, String> requestHeaders = new HashMap<String, String>();
            requestHeaders.put("Content-Length", inputLength+"");
            requestHeaders.put("Content-Type", contentType);
            // 使用URL签名方式上传指定输入流
            PutObjectResult result = client.putObject(url, inputStream, inputLength, requestHeaders);
//                System.out.println(result.getETag());
            return 0;
        } catch(Exception e) {
            logger.error("上传异常: " + e.getMessage());
        } finally {
//            if(null != client) {
//                client.shutdown();
//            }
        }


如果完成操作之后使用shutdown方法, 会抛出线程异常, 如果对OSSClient不做任何处理, 每隔60秒就有关闭空闲连接的日志输出(new了2个OSSClient实例, 完成操作之后断点停住):

2015-09-09 15:56:50,517 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 15:56:50,517 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 15:57:50,517 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 15:57:50,517 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 15:58:50,519 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 15:58:50,519 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 15:59:50,520 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 15:59:50,520 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 16:00:50,521 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 16:00:50,521 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 16:01:50,521 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 16:01:50,522 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 16:02:50,522 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 16:02:50,522 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>


这样上传文件是否合适? 该如何回收连接?

展开
收起
reformer 2015-09-09 16:07:13 22319 0
4 条回答
写回答
取消 提交回答
  • Re如何回收或者关闭OSSClient实例的连接
    如问题所示,关闭的时候会出现线程异常,请问如何关闭才不会?
    2016-08-09 15:11:37
    赞同 展开评论 打赏
  • 回2楼harvey91的帖子
    可以关闭,一旦关闭无法继续使用,需要重新创建。另外,建议使用单例模式,ossclient支持并发请求。
    2015-10-27 15:32:23
    赞同 展开评论 打赏
  • 回1楼oss-dev的帖子
    上传文件或者获取文件大小什么的,最后不需要关闭client 吗?不需要调用 client.shutdown(); 吗?这样会不会造成占用内存过多?
    2015-10-27 10:18:39
    赞同 展开评论 打赏
  • Re如何回收或者关闭OSSClient实例的连接
    不需要关闭oss的连接呀,连接池由OSSClient自己去管理的。
    2015-09-15 10:57:14
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载