Skip to content

Commit 1f830ec

Browse files
authored
🎨 #2390 【公共问题】ApacheHttpClientBuilder增加支持配置重试策略和超时策略的参数
1 parent 8b4c105 commit 1f830ec

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/apache/DefaultApacheHttpClientBuilder.java

+22-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.apache.http.config.Registry;
1313
import org.apache.http.config.RegistryBuilder;
1414
import org.apache.http.config.SocketConfig;
15+
import org.apache.http.conn.ConnectionKeepAliveStrategy;
1516
import org.apache.http.conn.HttpClientConnectionManager;
1617
import org.apache.http.conn.socket.ConnectionSocketFactory;
1718
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
@@ -92,7 +93,17 @@ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder {
9293
*/
9394
private String userAgent;
9495

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() {
96107
@Override
97108
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
98109
return false;
@@ -187,7 +198,16 @@ private synchronized void prepare() {
187198
.setConnectTimeout(this.connectionTimeout)
188199
.setConnectionRequestTimeout(this.connectionRequestTimeout)
189200
.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+
}
191211

192212
if (StringUtils.isNotBlank(this.httpProxyHost) && StringUtils.isNotBlank(this.httpProxyUsername)) {
193213
// 使用代理服务器 需要用户认证的代理服务器

0 commit comments

Comments
 (0)