🎨 #2390 【公共问题】ApacheHttpClientBuilder增加支持配置重试策略和超时策略的参数

This commit is contained in:
haoyanbing 2021-11-17 18:45:43 +08:00 committed by GitHub
parent 8b4c105029
commit 1f830ecacf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,6 +12,7 @@ import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
@ -92,7 +93,17 @@ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder {
*/
private String userAgent;
private final HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() {
/**
* 自定义重试策略
*/
private HttpRequestRetryHandler httpRequestRetryHandler;
/**
* 自定义KeepAlive策略
*/
private ConnectionKeepAliveStrategy connectionKeepAliveStrategy;
private final HttpRequestRetryHandler defaultHttpRequestRetryHandler = new HttpRequestRetryHandler() {
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
return false;
@ -187,7 +198,16 @@ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder {
.setConnectTimeout(this.connectionTimeout)
.setConnectionRequestTimeout(this.connectionRequestTimeout)
.build()
).setRetryHandler(this.httpRequestRetryHandler);
);
// 设置重试策略没有则使用默认
httpRequestRetryHandler = httpRequestRetryHandler == null ? defaultHttpRequestRetryHandler : httpRequestRetryHandler;
httpClientBuilder.setRetryHandler(httpRequestRetryHandler);
// 设置KeepAliveStrategy没有使用默认
if (connectionKeepAliveStrategy != null) {
httpClientBuilder.setKeepAliveStrategy(connectionKeepAliveStrategy);
}
if (StringUtils.isNotBlank(this.httpProxyHost) && StringUtils.isNotBlank(this.httpProxyUsername)) {
// 使用代理服务器 需要用户认证的代理服务器