摘要:連接不是線程安全的,每次只能在一個線程里頭使用,通過來管理。主要通過來作為代理類,管理連接的狀態和操作。如果底層的連接被關閉了,則它會歸還到。建議每個線程維護自己的
持久連接
通常一次連接之間的握手還是很耗費時間的,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 } } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/65898.html
摘要:自定義是請求響應式的,本是無狀態的,不過應用通常需要在幾個連續的請求之間保持聯系,因此可以使用這個來傳遞變量,注意這個不是線程安全的,建議每個線程使用一個。這個方法是線程安全的,而且可以從任意線程中調用。協議攔截器必須實現為線程安全的。 1、關閉流和response CloseableHttpClient httpclient = HttpClients.createDefault()...
摘要:內容提示阿里云服務器入門教程步驟遠程連接實例根據您本地的操作系統,您可以從等操作系統連接實例。根據提示,分別輸入您的云服務器實例的用戶名和密碼。內容提示:阿里云ECS服務器入門教程:步驟 3 遠程連接 Linux 實例 根據您本地的操作系統,您可以從 Windows、Linux、Mac OS X 等操作系統連接 Linux 實例。本文介紹常用的連接服務器方式。更全面詳細的連接實例方式介紹,請...
閱讀 3035·2023-04-26 03:01
閱讀 3538·2023-04-25 19:54
閱讀 1592·2021-11-24 09:39
閱讀 1374·2021-11-19 09:40
閱讀 4250·2021-10-14 09:43
閱讀 2062·2019-08-30 15:56
閱讀 1490·2019-08-30 13:52
閱讀 1661·2019-08-29 13:05