HttpClient線程池&重試機制


HttpClientUtils

package com.example.http_thread.util;

import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.ServiceUnavailableRetryStrategy;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.protocol.HttpContext;

import javax.net.ssl.SSLException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.UnknownHostException;


public class HttpClientUtil {
    private static PoolingHttpClientConnectionManager cm = null;

    static {
        cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(200);//多線程調用注意配置,根據線程數設定
        cm.setDefaultMaxPerRoute(300);
    }

    public static CloseableHttpClient getHttpClient() {

        HttpRequestRetryHandler httpRequestRetryHandler=new HttpRequestRetryHandler() {
            @Override
            public boolean retryRequest(IOException e, int i, HttpContext httpContext) {
                //Date date=new Date();
                System.out.println("try httpRequestRetryHandler o: " +i);

                if (i >= 10000) {
                    // Do not retry if over max retry count
                    return false;
                }
                if (e instanceof InterruptedIOException) {
                    // Timeout
                    return true;
                }
                if (e instanceof UnknownHostException) {
                    // Unknown host
                    return false;
                }
                if (e instanceof ConnectTimeoutException) {
                    // Connection refused
                    return true;
                }
                if (e instanceof SSLException) {
                    // SSL handshake exception
                    return false;
                }
                HttpClientContext clientContext = HttpClientContext.adapt(httpContext);
                HttpRequest request = clientContext.getRequest();
                boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
                if (idempotent) {
                    // Retry if the request is considered idempotent
                    return true;
                }
                return false;

            }
        };
        CloseableHttpClient httpClient = HttpClients.custom().setRetryHandler(httpRequestRetryHandler)
                .setConnectionManager(cm)
                .build();
        return httpClient;
    }
}

如何配置使用

@Test
    void testThreads() {
        String url = "*********************************";
        CloseableHttpClient client = HttpClientUtil.getHttpClient();
        HttpGet request = new HttpGet(url);
        CloseableHttpResponse response = null;
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(20000)//數據傳輸過程中數據包之間間隔的最大時間
                .setConnectTimeout(200000)//連接建立時間,三次握手完成時間
                .setExpectContinueEnabled(true)//重點參數
                .setConnectionRequestTimeout(60000)
                .setStaleConnectionCheckEnabled(true)//重點參數,在請求之前校驗鏈接是否有效
                .build();
        request.setConfig(requestConfig);
        System.out.println("111111111111111111111111111111111111");
        try {
            response = client.execute(request);
            System.out.println("22222222222222222222222222222222");
            if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                System.out.println("請求失敗");
            }
            HttpEntity resEntity = response.getEntity();
            if (resEntity == null) {
                System.out.println("No Response");
            }
            String result = EntityUtils.toString(resEntity, "UTF-8");
        } catch (Exception e) {
            System.out.println("0123123312"+e.getMessage()+e.getClass());
        }
          finally {
            if (response != null) {
                try {
                    //此處調優重點,多線程模式下可提高性能。
                    EntityUtils.consume(response.getEntity());//此處高能,通過源碼分析,由EntityUtils是否回收HttpEntity
                    response.close();
                } catch (IOException e) {
                    System.out.println("關閉response失敗:" + e);
                }
            }
        }
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM