博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HttpClient4.3.x的连接管理
阅读量:5979 次
发布时间:2019-06-20

本文共 3427 字,大约阅读时间需要 11 分钟。

持久连接

通常一次连接之间的握手还是很耗费时间的,Http1.1提供了持久连接,可以在一次连接期间,发送多次请求。

HttpClientConnectionManager

Http连接不是线程安全的,每次只能在一个线程里头使用,HttpClient通过HttpConnectionManager来管理。主要作为http connection的工厂,管理它的生命周期,确保每次只被一个线程使用。主要通过ManagedHttpClientConnection来作为代理类,管理连接的状态和I/O操作。如果底层的连接被关闭了,则它会归还到manager。

HttpClientContext context = HttpClientContext.create();HttpClientConnectionManager connMrg = new BasicHttpClientConnectionManager();HttpRoute route = new HttpRoute(new HttpHost("localhost", 80));// Request new connection. This can be a long processConnectionRequest connRequest = connMrg.requestConnection(route, null);// Wait for connection up to 10 secHttpClientConnection conn = connRequest.get(10, TimeUnit.SECONDS);try {    // If not open    if (!conn.isOpen()) {        // establish connection based on its route info        connMrg.connect(conn, route, 1000, context);        // and mark it as route complete        connMrg.routeComplete(conn, route, context);    }    // Do useful things with the connection.} finally {    connMrg.releaseConnection(conn, null, 1, TimeUnit.MINUTES);}

BasicHttpClientConnectionManager

BasicHttpClientConnectionManager是一个简单的连接管理器,每次只维持一个连接,它会试图在同一个route下的一系列请求之间重用这个连接。

PoolingHttpClientConnectionManager

PoolingHttpClientConnectionManager是一个相对复杂的管理器,可以在多线程中使用。

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();// Increase max total connection to 200cm.setMaxTotal(200);// Increase default max connection per route to 20cm.setDefaultMaxPerRoute(20);// Increase max connections for localhost:80 to 50HttpHost localhost = new HttpHost("locahost", 80);cm.setMaxPerRoute(new HttpRoute(localhost), 50);CloseableHttpClient httpClient = HttpClients.custom()        .setConnectionManager(cm)        .build();

如果对于同一个route的所有连接都被租用了,那么新的请求会被阻塞住,直到该route的连接被归还。

注意设置http.conn-manager.timeout,避免一个连接被占用过长时间。

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();CloseableHttpClient httpClient = HttpClients.custom()        .setConnectionManager(cm)        .build();// URIs to perform GETs onString[] urisToGet = {    "http://www.domain1.com/",    "http://www.domain2.com/",    "http://www.domain3.com/",    "http://www.domain4.com/"};// create a thread for each URIGetThread[] threads = new GetThread[urisToGet.length];for (int i = 0; i < threads.length; i++) {    HttpGet httpget = new HttpGet(urisToGet[i]);    threads[i] = new GetThread(httpClient, httpget);}// start the threadsfor (int j = 0; j < threads.length; j++) {    threads[j].start();}// join the threadsfor (int j = 0; j < threads.length; j++) {    threads[j].join();}

建议每个线程维护自己的context:

static class GetThread extends Thread {    private final CloseableHttpClient httpClient;    private final HttpContext context;    private final HttpGet httpget;    public GetThread(CloseableHttpClient httpClient, HttpGet httpget) {        this.httpClient = httpClient;        this.context = HttpClientContext.create();        this.httpget = httpget;    }    @Override    public void run() {        try {            CloseableHttpResponse response = httpClient.execute(                    httpget, context);            try {                HttpEntity entity = response.getEntity();            } finally {                response.close();            }        } catch (ClientProtocolException ex) {            // Handle protocol errors        } catch (IOException ex) {            // Handle I/O errors        }    }}

转载地址:http://lklox.baihongyu.com/

你可能感兴趣的文章
Hadoop WritableComparable接口
查看>>
JSF标签大全详解
查看>>
40多个关于人脸检测/识别的API、库和软件
查看>>
汇流累积量
查看>>
php 换行 PHP_EOL变量
查看>>
ios Url Encode
查看>>
Upgrading WebLogic Application Environments --官方文档
查看>>
Spring自学教程-介绍、特点、框架(一)
查看>>
HBase之八--(1):HBase二级索引的设计(案例讲解)
查看>>
PHP-VC9/VC6 TS/NTS等版本之间的区别
查看>>
tomcat集群
查看>>
bcdedit的研究
查看>>
防asp木马运行
查看>>
谁应该在CCB(变更控制委员会)中?
查看>>
.NET 并行(多核)编程系列之五 Task执行和异常处理
查看>>
Extjs4 Grid内容已经拿到但是不显示数据
查看>>
用AE如何制作如下三个loading动效,
查看>>
大型网站架构演化
查看>>
JS兼容所有浏览器的一段加入收藏代码,设置为首页
查看>>
VSPM虚拟串口使用
查看>>