在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

HttpClient4.3.x的连接管理

2016-8-29 13:17| 发布者: zhangjf| 查看: 583| 评论: 0

摘要: 持久连接 通常一次连接之间的握手还是很耗费时间的,Http1.1提供了持久连接,可以在一次连接期间,发送多次请求。 HttpClientConnectionManager Http连接不是线程安全的,每次只能在一个线程里头使用,HttpClient通 ...
持久连接

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

HttpClientConnectionManager

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

  1. HttpClientContext context = HttpClientContext.create();
  2. HttpClientConnectionManager connMrg = new BasicHttpClientConnectionManager();
  3. HttpRoute route = new HttpRoute(new HttpHost("localhost", 80));
  4. // Request new connection. This can be a long process
  5. ConnectionRequest connRequest = connMrg.requestConnection(route, null);
  6. // Wait for connection up to 10 sec
  7. HttpClientConnection conn = connRequest.get(10, TimeUnit.SECONDS);
  8. try {
  9. // If not open
  10. if (!conn.isOpen()) {
  11. // establish connection based on its route info
  12. connMrg.connect(conn, route, 1000, context);
  13. // and mark it as route complete
  14. connMrg.routeComplete(conn, route, context);
  15. }
  16. // Do useful things with the connection.
  17. } finally {
  18. connMrg.releaseConnection(conn, null, 1, TimeUnit.MINUTES);
  19. }
复制代码
BasicHttpClientConnectionManager

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

PoolingHttpClientConnectionManager

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

  1. PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
  2. // Increase max total connection to 200
  3. cm.setMaxTotal(200);
  4. // Increase default max connection per route to 20
  5. cm.setDefaultMaxPerRoute(20);
  6. // Increase max connections for localhost:80 to 50
  7. HttpHost localhost = new HttpHost("locahost", 80);
  8. cm.setMaxPerRoute(new HttpRoute(localhost), 50);
  9. CloseableHttpClient httpClient = HttpClients.custom()
  10. .setConnectionManager(cm)
  11. .build();
复制代码

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

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

  1. PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
  2. CloseableHttpClient httpClient = HttpClients.custom()
  3. .setConnectionManager(cm)
  4. .build();
  5. // URIs to perform GETs on
  6. String[] urisToGet = {
  7. "http://www.domain1.com/",
  8. "http://www.domain2.com/",
  9. "http://www.domain3.com/",
  10. "http://www.domain4.com/"
  11. };
  12. // create a thread for each URI
  13. GetThread[] threads = new GetThread[urisToGet.length];
  14. for (int i = 0; i < threads.length; i++) {
  15. HttpGet httpget = new HttpGet(urisToGet[i]);
  16. threads[i] = new GetThread(httpClient, httpget);
  17. }
  18. // start the threads
  19. for (int j = 0; j < threads.length; j++) {
  20. threads[j].start();
  21. }
  22. // join the threads
  23. for (int j = 0; j < threads.length; j++) {
  24. threads[j].join();
  25. }
复制代码

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

  1. static class GetThread extends Thread {
  2. private final CloseableHttpClient httpClient;
  3. private final HttpContext context;
  4. private final HttpGet httpget;
  5. public GetThread(CloseableHttpClient httpClient, HttpGet httpget) {
  6. this.httpClient = httpClient;
  7. this.context = HttpClientContext.create();
  8. this.httpget = httpget;
  9. }
  10. @Override
  11. public void run() {
  12. try {
  13. CloseableHttpResponse response = httpClient.execute(
  14. httpget, context);
  15. try {
  16. HttpEntity entity = response.getEntity();
  17. } finally {
  18. response.close();
  19. }
  20. } catch (ClientProtocolException ex) {
  21. // Handle protocol errors
  22. } catch (IOException ex) {
  23. // Handle I/O errors
  24. }
  25. }
  26. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-6 22:00

Copyright 2015-2025 djqfx

返回顶部