摘要:連接不是線程安全的,每次只能在一個(gè)線程里頭使用,通過來管理。主要通過來作為代理類,管理連接的狀態(tài)和操作。如果底層的連接被關(guān)閉了,則它會(huì)歸還到。建議每個(gè)線程維護(hù)自己的
持久連接
通常一次連接之間的握手還是很耗費(fèi)時(shí)間的,Http1.1提供了持久連接,可以在一次連接期間,發(fā)送多次請(qǐng)求。
HttpClientConnectionManagerHttp連接不是線程安全的,每次只能在一個(gè)線程里頭使用,HttpClient通過HttpConnectionManager來管理。主要作為http connection的工廠,管理它的生命周期,確保每次只被一個(gè)線程使用。主要通過ManagedHttpClientConnection來作為代理類,管理連接的狀態(tài)和I/O操作。如果底層的連接被關(guān)閉了,則它會(huì)歸還到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是一個(gè)簡(jiǎn)單的連接管理器,每次只維持一個(gè)連接,它會(huì)試圖在同一個(gè)route下的一系列請(qǐng)求之間重用這個(gè)連接。
PoolingHttpClientConnectionManagerPoolingHttpClientConnectionManager是一個(gè)相對(duì)復(fù)雜的管理器,可以在多線程中使用。
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();
如果對(duì)于同一個(gè)route的所有連接都被租用了,那么新的請(qǐng)求會(huì)被阻塞住,直到該route的連接被歸還。
注意設(shè)置http.conn-manager.timeout,避免一個(gè)連接被占用過長(zhǎng)時(shí)間。
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(); }
建議每個(gè)線程維護(hù)自己的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 } } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/65898.html
摘要:自定義是請(qǐng)求響應(yīng)式的,本是無狀態(tài)的,不過應(yīng)用通常需要在幾個(gè)連續(xù)的請(qǐng)求之間保持聯(lián)系,因此可以使用這個(gè)來傳遞變量,注意這個(gè)不是線程安全的,建議每個(gè)線程使用一個(gè)。這個(gè)方法是線程安全的,而且可以從任意線程中調(diào)用。協(xié)議攔截器必須實(shí)現(xiàn)為線程安全的。 1、關(guān)閉流和response CloseableHttpClient httpclient = HttpClients.createDefault()...
摘要:內(nèi)容提示阿里云服務(wù)器入門教程步驟遠(yuǎn)程連接實(shí)例根據(jù)您本地的操作系統(tǒng),您可以從等操作系統(tǒng)連接實(shí)例。根據(jù)提示,分別輸入您的云服務(wù)器實(shí)例的用戶名和密碼。內(nèi)容提示:阿里云ECS服務(wù)器入門教程:步驟 3 遠(yuǎn)程連接 Linux 實(shí)例 根據(jù)您本地的操作系統(tǒng),您可以從 Windows、Linux、Mac OS X 等操作系統(tǒng)連接 Linux 實(shí)例。本文介紹常用的連接服務(wù)器方式。更全面詳細(xì)的連接實(shí)例方式介紹,請(qǐng)...
摘要:數(shù)據(jù)庫(kù)連接池的基本原理是在內(nèi)部對(duì)象池中維護(hù)一定數(shù)量的數(shù)據(jù)庫(kù)連接,并對(duì)外暴露數(shù)據(jù)庫(kù)連接獲取和返回方法。統(tǒng)一的連接管理,避免數(shù)據(jù)庫(kù)連接泄漏在較為完備的數(shù)據(jù)庫(kù)連接池實(shí)現(xiàn)中,可根據(jù)預(yù)先的連接占用超時(shí)設(shè)定,強(qiáng)制收回被占用連接。 一、數(shù)據(jù)庫(kù)連接池的原理 基本原理 對(duì)于一個(gè)簡(jiǎn)單的數(shù)據(jù)庫(kù)應(yīng)用,由于對(duì)于數(shù)據(jù)庫(kù)的訪問不是很頻繁。這時(shí)可以簡(jiǎn)單地在需要訪問數(shù)據(jù)庫(kù)時(shí),就新創(chuàng)建一個(gè)連接,用完后就關(guān)閉它,這樣做也...
摘要:數(shù)據(jù)庫(kù)連接池的基本原理是在內(nèi)部對(duì)象池中維護(hù)一定數(shù)量的數(shù)據(jù)庫(kù)連接,并對(duì)外暴露數(shù)據(jù)庫(kù)連接獲取和返回方法。統(tǒng)一的連接管理,避免數(shù)據(jù)庫(kù)連接泄漏在較為完備的數(shù)據(jù)庫(kù)連接池實(shí)現(xiàn)中,可根據(jù)預(yù)先的連接占用超時(shí)設(shè)定,強(qiáng)制收回被占用連接。 一、數(shù)據(jù)庫(kù)連接池的原理 基本原理 對(duì)于一個(gè)簡(jiǎn)單的數(shù)據(jù)庫(kù)應(yīng)用,由于對(duì)于數(shù)據(jù)庫(kù)的訪問不是很頻繁。這時(shí)可以簡(jiǎn)單地在需要訪問數(shù)據(jù)庫(kù)時(shí),就新創(chuàng)建一個(gè)連接,用完后就關(guān)閉它,這樣做也...
閱讀 3206·2023-04-26 03:01
閱讀 3592·2023-04-25 19:54
閱讀 1694·2021-11-24 09:39
閱讀 1448·2021-11-19 09:40
閱讀 4368·2021-10-14 09:43
閱讀 2881·2019-08-30 15:56
閱讀 1546·2019-08-30 13:52
閱讀 1717·2019-08-29 13:05