Skip to content

Commit 7bd75c9

Browse files
committed
重构DefaultApacheHttpClientBuilder
1 parent b5d9ce9 commit 7bd75c9

File tree

3 files changed

+31
-38
lines changed

3 files changed

+31
-38
lines changed
Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package me.chanjar.weixin.common.util.http;
22

3-
import java.io.IOException;
4-
import java.util.concurrent.TimeUnit;
5-
3+
import me.chanjar.weixin.common.util.StringUtils;
64
import org.apache.http.annotation.NotThreadSafe;
75
import org.apache.http.auth.AuthScope;
86
import org.apache.http.auth.UsernamePasswordCredentials;
@@ -23,13 +21,14 @@
2321
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
2422
import org.apache.http.protocol.HttpContext;
2523

26-
import me.chanjar.weixin.common.util.StringUtils;
24+
import java.io.IOException;
25+
import java.util.concurrent.TimeUnit;
2726

2827
/**
2928
* httpclient 连接管理器
3029
*/
3130
@NotThreadSafe
32-
public class DefaultApacheHttpHttpClientBuilder implements ApacheHttpClientBuilder {
31+
public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder {
3332
private int connectionRequestTimeout = 3000;
3433
private int connectionTimeout = 5000;
3534
private int soTimeout = 5000;
@@ -52,27 +51,20 @@ public boolean retryRequest(IOException exception, int executionCount, HttpConte
5251
private String httpProxyUsername;
5352
private String httpProxyPassword;
5453

55-
/**
56-
* 连接管理器
57-
*/
58-
private PoolingHttpClientConnectionManager connectionManager;
5954
/**
6055
* 闲置连接监控线程
6156
*/
6257
private IdleConnectionMonitorThread idleConnectionMonitorThread;
6358

64-
/**
65-
* httpClientBuilder
66-
*/
6759
private HttpClientBuilder httpClientBuilder;
6860

6961
private boolean prepared = false;
7062

71-
private DefaultApacheHttpHttpClientBuilder() {
63+
private DefaultApacheHttpClientBuilder() {
7264
}
7365

74-
public static DefaultApacheHttpHttpClientBuilder get() {
75-
return new DefaultApacheHttpHttpClientBuilder();
66+
public static DefaultApacheHttpClientBuilder get() {
67+
return new DefaultApacheHttpClientBuilder();
7668
}
7769

7870
@Override
@@ -111,43 +103,44 @@ public IdleConnectionMonitorThread getIdleConnectionMonitorThread() {
111103

112104
private void prepare() {
113105
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
114-
.register("http", this.plainConnectionSocketFactory)
115-
.register("https", this.sslConnectionSocketFactory)
106+
.register("http", this.plainConnectionSocketFactory)
107+
.register("https", this.sslConnectionSocketFactory)
116108
.build();
117-
this.connectionManager = new PoolingHttpClientConnectionManager(registry);
118-
this.connectionManager.setMaxTotal(this.maxTotalConn);
119-
this.connectionManager.setDefaultMaxPerRoute(this.maxConnPerHost);
120-
this.connectionManager.setDefaultSocketConfig(
109+
110+
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
111+
connectionManager.setMaxTotal(this.maxTotalConn);
112+
connectionManager.setDefaultMaxPerRoute(this.maxConnPerHost);
113+
connectionManager.setDefaultSocketConfig(
121114
SocketConfig.copy(SocketConfig.DEFAULT)
122-
.setSoTimeout(this.soTimeout)
115+
.setSoTimeout(this.soTimeout)
123116
.build()
124117
);
125118

126119
this.idleConnectionMonitorThread = new IdleConnectionMonitorThread(
127-
this.connectionManager, this.idleConnTimeout, this.checkWaitTime);
120+
connectionManager, this.idleConnTimeout, this.checkWaitTime);
128121
this.idleConnectionMonitorThread.setDaemon(true);
129122
this.idleConnectionMonitorThread.start();
130123

131124
this.httpClientBuilder = HttpClients.custom()
132-
.setConnectionManager(this.connectionManager)
125+
.setConnectionManager(connectionManager)
133126
.setDefaultRequestConfig(
134127
RequestConfig.custom()
135-
.setSocketTimeout(this.soTimeout)
136-
.setConnectTimeout(this.connectionTimeout)
137-
.setConnectionRequestTimeout(this.connectionRequestTimeout)
128+
.setSocketTimeout(this.soTimeout)
129+
.setConnectTimeout(this.connectionTimeout)
130+
.setConnectionRequestTimeout(this.connectionRequestTimeout)
138131
.build()
139132
)
140-
.setRetryHandler(this.httpRequestRetryHandler);
133+
.setRetryHandler(this.httpRequestRetryHandler);
141134

142135
if (StringUtils.isNotBlank(this.httpProxyHost)
143-
&& StringUtils.isNotBlank(this.httpProxyUsername)) {
136+
&& StringUtils.isNotBlank(this.httpProxyUsername)) {
144137
// 使用代理服务器 需要用户认证的代理服务器
145-
CredentialsProvider credsProvider = new BasicCredentialsProvider();
146-
credsProvider.setCredentials(
147-
new AuthScope(this.httpProxyHost, this.httpProxyPort),
148-
new UsernamePasswordCredentials(this.httpProxyUsername,
149-
this.httpProxyPassword));
150-
this.httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
138+
CredentialsProvider provider = new BasicCredentialsProvider();
139+
provider.setCredentials(
140+
new AuthScope(this.httpProxyHost, this.httpProxyPort),
141+
new UsernamePasswordCredentials(this.httpProxyUsername,
142+
this.httpProxyPassword));
143+
this.httpClientBuilder.setDefaultCredentialsProvider(provider);
151144
}
152145

153146
if (StringUtils.isNotBlank(this.userAgent)) {
@@ -187,7 +180,7 @@ public void run() {
187180
wait(this.checkWaitTime);
188181
this.connMgr.closeExpiredConnections();
189182
this.connMgr.closeIdleConnections(this.idleConnTimeout,
190-
TimeUnit.MILLISECONDS);
183+
TimeUnit.MILLISECONDS);
191184
}
192185
}
193186
} catch (InterruptedException ignore) {

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ public void setWxCpConfigStorage(WxCpConfigStorage wxConfigProvider) {
591591
this.wxCpConfigStorage = wxConfigProvider;
592592
ApacheHttpClientBuilder apacheHttpClientBuilder = wxCpConfigStorage.getApacheHttpClientBuilder();
593593
if (null == apacheHttpClientBuilder) {
594-
apacheHttpClientBuilder = DefaultApacheHttpHttpClientBuilder.get();
594+
apacheHttpClientBuilder = DefaultApacheHttpClientBuilder.get();
595595
}
596596
apacheHttpClientBuilder.httpProxyHost(wxCpConfigStorage.getHttp_proxy_host())
597597
.httpProxyPort(wxCpConfigStorage.getHttp_proxy_port())

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ public void setWxMpConfigStorage(WxMpConfigStorage wxConfigProvider) {
457457
private void initHttpClient() {
458458
ApacheHttpClientBuilder apacheHttpClientBuilder = this.wxMpConfigStorage.getApacheHttpClientBuilder();
459459
if (null == apacheHttpClientBuilder) {
460-
apacheHttpClientBuilder = DefaultApacheHttpHttpClientBuilder.get();
460+
apacheHttpClientBuilder = DefaultApacheHttpClientBuilder.get();
461461
}
462462

463463
apacheHttpClientBuilder.httpProxyHost(this.wxMpConfigStorage.getHttpProxyHost())

0 commit comments

Comments
 (0)