Apache HttpClient4.5.2 超时等待,超时重复请求设置

对于接口服务重启,网络动荡,接口返回异常等情况,客户端需要做重新请求的机制!

public static CloseableHttpClient getHttpClient() {
        HttpRequestRetryHandler handler = new HttpRequestRetryHandler() {
            @Override
            public boolean retryRequest(IOException arg0, int retryTimes, HttpContext arg2) {
                if (retryTimes > 2) {
                    return false;
                }
                System.out.println(System.currentTimeMillis());
                if (arg0 instanceof UnknownHostException || arg0 instanceof ConnectTimeoutException
                        || !(arg0 instanceof SSLException) || arg0 instanceof NoHttpResponseException) {
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(System.currentTimeMillis());
                    return true;
                }

                HttpClientContext clientContext = HttpClientContext.adapt(arg2);
                HttpRequest request = clientContext.getRequest();
                boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
                if (idempotent) {
                    // 如果请求被认为是幂等的,那么就重试。即重复执行不影响程序其他效果的
                    return true;
                }
                return false;
            }
        };
        ServiceUnavailableRetryStrategy serviceUnavailableRetryStrategy = new ServiceUnavailableRetryStrategy() {
            /**
             * retry逻辑
             */
            @Override
            public boolean retryRequest(HttpResponse response, int executionCount, HttpContext context) {
                if (executionCount < 3){
                    if(response.getStatusLine().getStatusCode()!=200){
                        return true;
                    }
                    JSONObject obj = null;
                    try {
                        obj =  JSONObject.fromObject(EntityUtils.toString(response.getEntity()));
                        if(("9999").equals(obj.getString("errorcode"))){
                            return true;
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return false;
                }else {
                    return false;
                }

            }

            /**
             * retry间隔时间
             */
            @Override
            public long getRetryInterval() {
                return 12000;
            }
        };
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(1000);
        cm.setDefaultMaxPerRoute(200);
      return HttpClients.custom().setConnectionManager(cm).setServiceUnavailableRetryStrategy(serviceUnavailableRetryStrategy).setRetryHandler(handler).setRedirectStrategy(new LaxRedirectStrategy()).build();
    
    }

HttpRequestRetryHandler :针对 ConnectTimeoutException、NoHttpResponseException、UnknownHostException 等连接超时异常,做三次重复请求,间隔10s;
ServiceUnavailableRetryStrategy :针对响应非200,或者返回结果为9999的,做三次重复请求。
两层处理机制,能够有效的避免请求丢失的情况。

你可能感兴趣的:(HttpClient,JAVA)