Skip to content

Commit 669b4ea

Browse files
committed
🎨 优化ApacheHttpClientBuilder代码
1 parent cffd173 commit 669b4ea

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package me.chanjar.weixin.common.util.http.apache;
22

3+
import org.apache.http.client.HttpRequestRetryHandler;
4+
import org.apache.http.conn.ConnectionKeepAliveStrategy;
35
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
46
import org.apache.http.impl.client.CloseableHttpClient;
57

@@ -37,6 +39,16 @@ public interface ApacheHttpClientBuilder {
3739
*/
3840
ApacheHttpClientBuilder httpProxyPassword(String httpProxyPassword);
3941

42+
/**
43+
* 重试策略.
44+
*/
45+
ApacheHttpClientBuilder httpRequestRetryHandler(HttpRequestRetryHandler httpRequestRetryHandler );
46+
47+
/**
48+
* 超时时间.
49+
*/
50+
ApacheHttpClientBuilder keepAliveStrategy(ConnectionKeepAliveStrategy keepAliveStrategy);
51+
4052
/**
4153
* ssl连接socket工厂.
4254
*/

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.apache.http.config.Registry;
1111
import org.apache.http.config.RegistryBuilder;
1212
import org.apache.http.config.SocketConfig;
13+
import org.apache.http.conn.ConnectionKeepAliveStrategy;
1314
import org.apache.http.conn.DnsResolver;
1415
import org.apache.http.conn.HttpClientConnectionManager;
1516
import org.apache.http.conn.socket.ConnectionSocketFactory;
@@ -48,12 +49,13 @@ public class ApacheHttpDnsClientBuilder implements ApacheHttpClientBuilder {
4849
private int maxTotalConn = 50;
4950
private String userAgent;
5051

51-
private DnsResolver dnsResover;
52+
private DnsResolver dnsResolver;
5253

5354
private HttpRequestRetryHandler httpRequestRetryHandler = (IOException exception, int executionCount, HttpContext context) -> false;
5455
private SSLConnectionSocketFactory sslConnectionSocketFactory = SSLConnectionSocketFactory.getSocketFactory();
5556
private PlainConnectionSocketFactory plainConnectionSocketFactory = PlainConnectionSocketFactory.getSocketFactory();
5657
private String httpProxyHost;
58+
private ConnectionKeepAliveStrategy keepAliveStrategy;
5759

5860
private int httpProxyPort;
5961
private String httpProxyUsername;
@@ -97,6 +99,18 @@ public ApacheHttpClientBuilder httpProxyPassword(String httpProxyPassword) {
9799
return this;
98100
}
99101

102+
@Override
103+
public ApacheHttpClientBuilder httpRequestRetryHandler(HttpRequestRetryHandler httpRequestRetryHandler) {
104+
this.httpRequestRetryHandler = httpRequestRetryHandler;
105+
return this;
106+
}
107+
108+
@Override
109+
public ApacheHttpClientBuilder keepAliveStrategy(ConnectionKeepAliveStrategy keepAliveStrategy) {
110+
this.keepAliveStrategy = keepAliveStrategy;
111+
return this;
112+
}
113+
100114
@Override
101115
public ApacheHttpClientBuilder sslConnectionSocketFactory(SSLConnectionSocketFactory sslConnectionSocketFactory) {
102116
this.sslConnectionSocketFactory = sslConnectionSocketFactory;
@@ -202,11 +216,11 @@ private synchronized void prepare() {
202216

203217
@SuppressWarnings("resource")
204218
PoolingHttpClientConnectionManager connectionManager;
205-
if (dnsResover != null) {
219+
if (dnsResolver != null) {
206220
if (log.isDebugEnabled()) {
207221
log.debug("specified dns resolver.");
208222
}
209-
connectionManager = new PoolingHttpClientConnectionManager(registry, dnsResover);
223+
connectionManager = new PoolingHttpClientConnectionManager(registry, dnsResolver);
210224
} else {
211225
if (log.isDebugEnabled()) {
212226
log.debug("Not specified dns resolver.");
@@ -254,12 +268,12 @@ public CloseableHttpClient build() {
254268
return this.httpClientBuilder.build();
255269
}
256270

257-
public DnsResolver getDnsResover() {
258-
return dnsResover;
271+
public DnsResolver getDnsResolver() {
272+
return dnsResolver;
259273
}
260274

261-
public void setDnsResover(DnsResolver dnsResover) {
262-
this.dnsResover = dnsResover;
275+
public void setDnsResolver(DnsResolver dnsResolver) {
276+
this.dnsResolver = dnsResolver;
263277
}
264278

265279
public static class IdleConnectionMonitorThread extends Thread {

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,8 @@ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder {
103103
*/
104104
private ConnectionKeepAliveStrategy connectionKeepAliveStrategy;
105105

106-
private final HttpRequestRetryHandler defaultHttpRequestRetryHandler = new HttpRequestRetryHandler() {
107-
@Override
108-
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
109-
return false;
110-
}
111-
};
106+
private final HttpRequestRetryHandler defaultHttpRequestRetryHandler = (exception, executionCount, context) -> false;
107+
112108
private SSLConnectionSocketFactory sslConnectionSocketFactory = SSLConnectionSocketFactory.getSocketFactory();
113109
private final PlainConnectionSocketFactory plainConnectionSocketFactory = PlainConnectionSocketFactory.getSocketFactory();
114110
private String httpProxyHost;
@@ -155,6 +151,18 @@ public ApacheHttpClientBuilder httpProxyPassword(String httpProxyPassword) {
155151
return this;
156152
}
157153

154+
@Override
155+
public ApacheHttpClientBuilder httpRequestRetryHandler(HttpRequestRetryHandler httpRequestRetryHandler) {
156+
this.httpRequestRetryHandler = httpRequestRetryHandler;
157+
return this;
158+
}
159+
160+
@Override
161+
public ApacheHttpClientBuilder keepAliveStrategy(ConnectionKeepAliveStrategy keepAliveStrategy) {
162+
this.connectionKeepAliveStrategy = keepAliveStrategy;
163+
return this;
164+
}
165+
158166
@Override
159167
public ApacheHttpClientBuilder sslConnectionSocketFactory(SSLConnectionSocketFactory sslConnectionSocketFactory) {
160168
this.sslConnectionSocketFactory = sslConnectionSocketFactory;

0 commit comments

Comments
 (0)