【问题标题】:HttpClient connection reusing with 4.3.x使用 4.3.x 重用 HttpClient 连接
【发布时间】:2014-04-14 16:48:52
【问题描述】:

我正在尝试使用 HttpClient,但无法解读 1.1.5. Ensuring release of low level resources 的含义。

这些关闭内容流关闭响应是如何解释的?

关闭内容流:(保持底层连接活动)

CloseableHttpClient httpclient = HttpClients.createDefault();
try {
    HttpGet httpget = new HttpGet("http://localhost/");

    // do multiple times on the same connection
    for (...) {
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            try {
                // do something useful
            } finally {
                EntityUtils.consume(entity);    // <-- ensures reuse
            }
        }
    }
} finally {
    httpclient.close();
}

关闭响应:(立即关闭并丢弃连接)

CloseableHttpClient httpclient = HttpClients.createDefault();
try {
    HttpGet httpget = new HttpGet("http://localhost/");

    // do multiple times on different connections
    for (...) {
        ClosableHttpResponse response = httpclient.execute(httpget);
        try {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                // do something useful
            }
        } finally {
            response.close();    // <-- ensures reconnect
        }
    }
} finally {
    httpclient.close();
}

【问题讨论】:

    标签: java httpclient


    【解决方案1】:

    entityUtils.consume 为你关闭流...

    if (entity.isStreaming()) {
        final InputStream instream = entity.getContent();
        if (instream != null) {
            instream.close();
        }
    }
    

    您只需将您的客户“释放”回池中......

    然后,您应该将您的 HttpClient 包装在一个可运行的...

    public void run() {
        handler.sendMessage(Message.obtain(handler, HttpConnection.DID_START));
        CloseableHttpClient httpClient = HttpClients.custom()
                .setConnectionManager(YourConnectionMgr.getInstance())
                .addInterceptorLast(new HttpRequestInterceptor() {
                    public void process(
                        final HttpRequest request, 
                        final HttpContext context) throws HttpException, IOException { 
                        }
                    })
                    .build();
     }  //end runnable
    

    在 runnable 结束时,客户端刚刚被释放回 ConnectionPool,您不必担心资源或清理。

    使用扩展 PoolingClientConnectionManager 的管理器

    newInstance =  new MyConnectionManager(schemeRegistry);
         instance.setMaxTotal(15);
         instance.setDefaultMaxPerRoute(15);
         HttpHost localhost = new HttpHost("api.parse.com", 443);
         instance.setMaxPerRoute(new HttpRoute(localhost), 10);
    

    最后,我认为您确实需要关闭池。

    YourConnectionMgr.getInstance().shutdown();
    YourConnectionMgr.reset();
    

    更多详情here

    【讨论】:

      【解决方案2】:

      一般来说,一旦您完成了实体,您希望将其丢弃,这样系统资源就不会被不再有意义的对象所占用。在我看来,这里唯一的区别是使用。关于基础的那一章基本上是在描述这一点。无论您如何实现它,请确保仅在需要时使用资源。低级资源是实体中的InputStream,高级资源是连接。例如,如果您正在实现一些不需要读取完整 InputStream 来做出决定的东西,只需终止响应,就会有效地为您处理清理工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-09
        • 1970-01-01
        相关资源
        最近更新 更多