|
|
@@ -1,6 +1,11 @@
|
|
|
package com.uas.ps.inquiry.http;
|
|
|
|
|
|
+import org.apache.http.HttpEntityEnclosingRequest;
|
|
|
+import org.apache.http.HttpRequest;
|
|
|
+import org.apache.http.NoHttpResponseException;
|
|
|
+import org.apache.http.client.HttpRequestRetryHandler;
|
|
|
import org.apache.http.client.config.RequestConfig;
|
|
|
+import org.apache.http.client.protocol.HttpClientContext;
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
|
|
@@ -11,12 +16,17 @@ import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
|
|
|
import org.apache.http.impl.nio.reactor.IOReactorConfig;
|
|
|
import org.apache.http.nio.reactor.ConnectingIOReactor;
|
|
|
import org.apache.http.nio.reactor.IOReactorException;
|
|
|
+import org.apache.http.protocol.HttpContext;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
+import javax.net.ssl.SSLException;
|
|
|
+import javax.net.ssl.SSLHandshakeException;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InterruptedIOException;
|
|
|
|
|
|
/**
|
|
|
* http 同步连接池、http 异步连接池,与spring集成
|
|
|
@@ -69,6 +79,8 @@ public class HttpClientSpringFactory {
|
|
|
|
|
|
private PoolingHttpClientConnectionManager syncConnManager;
|
|
|
|
|
|
+ private HttpRequestRetryHandler httpRequestRetryHandler;
|
|
|
+
|
|
|
@PostConstruct
|
|
|
public void init() {
|
|
|
asyncRequestConfig = RequestConfig.custom()
|
|
|
@@ -105,6 +117,40 @@ public class HttpClientSpringFactory {
|
|
|
syncConnManager = new PoolingHttpClientConnectionManager();
|
|
|
syncConnManager.setDefaultMaxPerRoute(syncConnectPerRoute);
|
|
|
syncConnManager.setMaxTotal(syncConnectNum);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // 请求重试处理
|
|
|
+ httpRequestRetryHandler = new HttpRequestRetryHandler() {
|
|
|
+ public boolean retryRequest(IOException exception,
|
|
|
+ int executionCount, HttpContext context) {
|
|
|
+ if (executionCount >= 5) {// 如果已经重试了5次,就放弃
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (exception instanceof NoHttpResponseException) {// 如果服务器丢掉了连接,那么就重试
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ if (exception instanceof SSLHandshakeException) {// 不要重试SSL握手异常
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (exception instanceof InterruptedIOException) {// 超时
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (exception instanceof SSLException) {// SSL握手异常
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ HttpClientContext clientContext = HttpClientContext
|
|
|
+ .adapt(context);
|
|
|
+ HttpRequest request = clientContext.getRequest();
|
|
|
+ // 如果请求是幂等的,就再次尝试
|
|
|
+ if (!(request instanceof HttpEntityEnclosingRequest)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -128,6 +174,7 @@ public class HttpClientSpringFactory {
|
|
|
CloseableHttpClient httpClient = HttpClients.custom()
|
|
|
.setConnectionManager(syncConnManager)
|
|
|
.setDefaultRequestConfig(syncRequestConfig)
|
|
|
+ .setRetryHandler(httpRequestRetryHandler)
|
|
|
.build();
|
|
|
return httpClient;
|
|
|
}
|