|
12 | 12 | import org.apache.http.config.Registry;
|
13 | 13 | import org.apache.http.config.RegistryBuilder;
|
14 | 14 | import org.apache.http.config.SocketConfig;
|
| 15 | +import org.apache.http.conn.ConnectionKeepAliveStrategy; |
15 | 16 | import org.apache.http.conn.HttpClientConnectionManager;
|
16 | 17 | import org.apache.http.conn.socket.ConnectionSocketFactory;
|
17 | 18 | import org.apache.http.conn.socket.PlainConnectionSocketFactory;
|
@@ -92,7 +93,17 @@ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder {
|
92 | 93 | */
|
93 | 94 | private String userAgent;
|
94 | 95 |
|
95 |
| - private final HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() { |
| 96 | + /** |
| 97 | + * 自定义重试策略 |
| 98 | + */ |
| 99 | + private HttpRequestRetryHandler httpRequestRetryHandler; |
| 100 | + |
| 101 | + /** |
| 102 | + * 自定义KeepAlive策略 |
| 103 | + */ |
| 104 | + private ConnectionKeepAliveStrategy connectionKeepAliveStrategy; |
| 105 | + |
| 106 | + private final HttpRequestRetryHandler defaultHttpRequestRetryHandler = new HttpRequestRetryHandler() { |
96 | 107 | @Override
|
97 | 108 | public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
|
98 | 109 | return false;
|
@@ -187,7 +198,16 @@ private synchronized void prepare() {
|
187 | 198 | .setConnectTimeout(this.connectionTimeout)
|
188 | 199 | .setConnectionRequestTimeout(this.connectionRequestTimeout)
|
189 | 200 | .build()
|
190 |
| - ).setRetryHandler(this.httpRequestRetryHandler); |
| 201 | + ); |
| 202 | + |
| 203 | + // 设置重试策略,没有则使用默认 |
| 204 | + httpRequestRetryHandler = httpRequestRetryHandler == null ? defaultHttpRequestRetryHandler : httpRequestRetryHandler; |
| 205 | + httpClientBuilder.setRetryHandler(httpRequestRetryHandler); |
| 206 | + |
| 207 | + // 设置KeepAliveStrategy,没有使用默认 |
| 208 | + if (connectionKeepAliveStrategy != null) { |
| 209 | + httpClientBuilder.setKeepAliveStrategy(connectionKeepAliveStrategy); |
| 210 | + } |
191 | 211 |
|
192 | 212 | if (StringUtils.isNotBlank(this.httpProxyHost) && StringUtils.isNotBlank(this.httpProxyUsername)) {
|
193 | 213 | // 使用代理服务器 需要用户认证的代理服务器
|
|
0 commit comments