
Apache HttpClient - 多執行緒
多執行緒程式包含兩個或多個可以併發執行的部分,每個部分可以同時處理不同的任務,從而最大程度地利用可用資源。
您可以透過編寫多執行緒 HttpClient 程式來從多個執行緒執行請求。
如果您想從執行緒連續執行多個客戶端請求,則需要建立一個**ClientConnectionPoolManager**。它維護一個**HttpClientConnections**池,併為來自執行緒的多個請求提供服務。
連線管理器根據路由對連線進行池化。如果管理器具有特定路由的連線,則它透過從池中租用現有連線來為這些路由中的新請求提供服務,而不是建立新的連線。
請按照以下步驟從多個執行緒執行請求:
步驟 1 - 建立客戶端連線池管理器
透過例項化**PoolingHttpClientConnectionManager**類來建立客戶端連線池管理器。
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
步驟 2 - 設定最大連線數
使用**setMaxTotal()**方法設定池中的最大連線數。
//Set the maximum number of connections in the pool connManager.setMaxTotal(100);
步驟 3 - 建立 ClientBuilder 物件
透過使用**setConnectionManager()**方法設定連線管理器來建立一個**ClientBuilder**物件,如下所示:
HttpClientBuilder clientbuilder = HttpClients.custom().setConnectionManager(connManager);
步驟 4 - 建立 HttpGet 請求物件
透過將所需的 URI 作為引數傳遞給其建構函式來例項化 HttpGet 類。
HttpGet httpget1 = new HttpGet("URI1"); HttpGet httpget2 = new HttpGet("URI2"); . . . . . . . . . . . .
步驟 5 - 實現 run 方法
確保您已建立一個類,並將其設為執行緒(透過擴充套件執行緒類或實現 Runnable 介面),並實現了 run 方法。
public class ClientMultiThreaded extends Thread { public void run() { //Run method implementation . . . . . . . . . . } }
步驟 6 - 建立執行緒物件
透過例項化上面建立的 Thread 類(ClientMultiThreaded)來建立執行緒物件。
將 HttpClient 物件、相應的 HttpGet 物件和表示 ID 的整數傳遞給這些執行緒。
ClientMultiThreaded thread1 = new ClientMultiThreaded(httpclient,httpget1, 1); ClientMultiThreaded thread2 = new ClientMultiThreaded(httpclient,httpget2, 2); . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
步驟 7 - 啟動並連線執行緒
使用**start()**方法啟動所有執行緒,並使用 join **method()**方法連線它們。
thread1.start(); thread2.start(); . . . . . . . . thread1.join(); thread2.join(); . . . . . . . . . . . .
步驟 8 - Run 方法實現
在 run 方法中,執行請求,檢索響應並列印結果。
示例
以下示例演示了從多個執行緒同時執行 HTTP 請求。在此示例中,我們嘗試從各個執行緒執行各種請求,並嘗試列印每個客戶端的狀態和讀取的位元組數。
import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.util.EntityUtils; public class ClientMultiThreaded extends Thread { CloseableHttpClient httpClient; HttpGet httpget; int id; public ClientMultiThreaded(CloseableHttpClient httpClient, HttpGet httpget, int id) { this.httpClient = httpClient; this.httpget = httpget; this.id = id; } @Override public void run() { try{ //Executing the request CloseableHttpResponse httpresponse = httpClient.execute(httpget); //Displaying the status of the request. System.out.println("status of thread "+id+":"+httpresponse.getStatusLine()); //Retrieving the HttpEntity and displaying the no.of bytes read HttpEntity entity = httpresponse.getEntity(); if (entity != null) { System.out.println("Bytes read by thread thread "+id+": "+EntityUtils.toByteArray(entity).length); } }catch(Exception e) { System.out.println(e.getMessage()); } } public static void main(String[] args) throws Exception { //Creating the Client Connection Pool Manager by instantiating the PoolingHttpClientConnectionManager class. PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); //Set the maximum number of connections in the pool connManager.setMaxTotal(100); //Create a ClientBuilder Object by setting the connection manager HttpClientBuilder clientbuilder = HttpClients.custom().setConnectionManager(connManager); //Build the CloseableHttpClient object using the build() method. CloseableHttpClient httpclient = clientbuilder.build(); //Creating the HttpGet requests HttpGet httpget1 = new HttpGet("https://tutorialspoint.tw/"); HttpGet httpget2 = new HttpGet("http://www.google.com/"); HttpGet httpget3 = new HttpGet("https://www.qries.com/"); HttpGet httpget4 = new HttpGet("https://in.yahoo.com/"); //Creating the Thread objects ClientMultiThreaded thread1 = new ClientMultiThreaded(httpclient,httpget1, 1); ClientMultiThreaded thread2 = new ClientMultiThreaded(httpclient,httpget2, 2); ClientMultiThreaded thread3 = new ClientMultiThreaded(httpclient,httpget3, 3); ClientMultiThreaded thread4 = new ClientMultiThreaded(httpclient,httpget4, 4); //Starting all the threads thread1.start(); thread2.start(); thread3.start(); thread4.start(); //Joining all the threads thread1.join(); thread2.join(); thread3.join(); thread4.join(); } }
輸出
執行上述程式後,將生成以下輸出:
status of thread 1: HTTP/1.1 200 OK Bytes read by thread thread 1: 36907 status of thread 2: HTTP/1.1 200 OK Bytes read by thread thread 2: 13725 status of thread 3: HTTP/1.1 200 OK Bytes read by thread thread 3: 17319 status of thread 4: HTTP/1.1 200 OK Bytes read by thread thread 4: 127018