摘要:连接不是线程安全的,每次只能在一个线程里头使用,通过来管理。主要通过来作为代理类,管理连接的状态和操作。如果底层的连接被关闭了,则它会归还到。建议每个线程维护自己的
持久连接
通常一次连接之间的握手还是很耗费时间的,Http1.1提供了持久连接,可以在一次连接期间,发送多次请求。
HttpClientConnectionManagerHttp连接不是线程安全的,每次只能在一个线程里头使用,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 process ConnectionRequest connRequest = connMrg.requestConnection(route, null); // Wait for connection up to 10 sec HttpClientConnection 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下的一系列请求之间重用这个连接。
PoolingHttpClientConnectionManagerPoolingHttpClientConnectionManager是一个相对复杂的管理器,可以在多线程中使用。
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); // Increase max total connection to 200 cm.setMaxTotal(200); // Increase default max connection per route to 20 cm.setDefaultMaxPerRoute(20); // Increase max connections for localhost:80 to 50 HttpHost 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 on String[] urisToGet = { "http://www.domain1.com/", "http://www.domain2.com/", "http://www.domain3.com/", "http://www.domain4.com/" }; // create a thread for each URI GetThread[] 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 threads for (int j = 0; j < threads.length; j++) { threads[j].start(); } // join the threads for (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 } } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/65898.html
摘要:自定义是请求响应式的,本是无状态的,不过应用通常需要在几个连续的请求之间保持联系,因此可以使用这个来传递变量,注意这个不是线程安全的,建议每个线程使用一个。这个方法是线程安全的,而且可以从任意线程中调用。协议拦截器必须实现为线程安全的。 1、关闭流和response CloseableHttpClient httpclient = HttpClients.createDefault()...
摘要:内容提示阿里云服务器入门教程步骤远程连接实例根据您本地的操作系统,您可以从等操作系统连接实例。根据提示,分别输入您的云服务器实例的用户名和密码。内容提示:阿里云ECS服务器入门教程:步骤 3 远程连接 Linux 实例 根据您本地的操作系统,您可以从 Windows、Linux、Mac OS X 等操作系统连接 Linux 实例。本文介绍常用的连接服务器方式。更全面详细的连接实例方式介绍,请...
阅读 3034·2023-04-26 03:01
阅读 3537·2023-04-25 19:54
阅读 1591·2021-11-24 09:39
阅读 1373·2021-11-19 09:40
阅读 4250·2021-10-14 09:43
阅读 2062·2019-08-30 15:56
阅读 1490·2019-08-30 13:52
阅读 1659·2019-08-29 13:05