Skip to content

Commit b26a017

Browse files
authored
Merge pull request binarywang#40 from kakotor/develop
增加设置setConnectionManagerShared参数
2 parents 9a38010 + 15f17b2 commit b26a017

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

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

Lines changed: 25 additions & 25 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,7 +21,8 @@
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 连接管理器
@@ -104,44 +103,45 @@ public IdleConnectionMonitorThread getIdleConnectionMonitorThread() {
104103

105104
private void prepare() {
106105
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
107-
.register("http", this.plainConnectionSocketFactory)
108-
.register("https", this.sslConnectionSocketFactory)
109-
.build();
106+
.register("http", this.plainConnectionSocketFactory)
107+
.register("https", this.sslConnectionSocketFactory)
108+
.build();
110109

111110
@SuppressWarnings("resource")
112111
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
113112
connectionManager.setMaxTotal(this.maxTotalConn);
114113
connectionManager.setDefaultMaxPerRoute(this.maxConnPerHost);
115114
connectionManager.setDefaultSocketConfig(
116-
SocketConfig.copy(SocketConfig.DEFAULT)
117-
.setSoTimeout(this.soTimeout)
118-
.build()
115+
SocketConfig.copy(SocketConfig.DEFAULT)
116+
.setSoTimeout(this.soTimeout)
117+
.build()
119118
);
120119

121120
this.idleConnectionMonitorThread = new IdleConnectionMonitorThread(
122-
connectionManager, this.idleConnTimeout, this.checkWaitTime);
121+
connectionManager, this.idleConnTimeout, this.checkWaitTime);
123122
this.idleConnectionMonitorThread.setDaemon(true);
124123
this.idleConnectionMonitorThread.start();
125124

126125
this.httpClientBuilder = HttpClients.custom()
127-
.setConnectionManager(connectionManager)
128-
.setDefaultRequestConfig(
129-
RequestConfig.custom()
130-
.setSocketTimeout(this.soTimeout)
131-
.setConnectTimeout(this.connectionTimeout)
132-
.setConnectionRequestTimeout(this.connectionRequestTimeout)
133-
.build()
134-
)
135-
.setRetryHandler(this.httpRequestRetryHandler);
126+
.setConnectionManager(connectionManager)
127+
.setConnectionManagerShared(true)
128+
.setDefaultRequestConfig(
129+
RequestConfig.custom()
130+
.setSocketTimeout(this.soTimeout)
131+
.setConnectTimeout(this.connectionTimeout)
132+
.setConnectionRequestTimeout(this.connectionRequestTimeout)
133+
.build()
134+
)
135+
.setRetryHandler(this.httpRequestRetryHandler);
136136

137137
if (StringUtils.isNotBlank(this.httpProxyHost)
138-
&& StringUtils.isNotBlank(this.httpProxyUsername)) {
138+
&& StringUtils.isNotBlank(this.httpProxyUsername)) {
139139
// 使用代理服务器 需要用户认证的代理服务器
140140
CredentialsProvider provider = new BasicCredentialsProvider();
141141
provider.setCredentials(
142-
new AuthScope(this.httpProxyHost, this.httpProxyPort),
143-
new UsernamePasswordCredentials(this.httpProxyUsername,
144-
this.httpProxyPassword));
142+
new AuthScope(this.httpProxyHost, this.httpProxyPort),
143+
new UsernamePasswordCredentials(this.httpProxyUsername,
144+
this.httpProxyPassword));
145145
this.httpClientBuilder.setDefaultCredentialsProvider(provider);
146146
}
147147

@@ -182,7 +182,7 @@ public void run() {
182182
wait(this.checkWaitTime);
183183
this.connMgr.closeExpiredConnections();
184184
this.connMgr.closeIdleConnections(this.idleConnTimeout,
185-
TimeUnit.MILLISECONDS);
185+
TimeUnit.MILLISECONDS);
186186
}
187187
}
188188
} catch (InterruptedException ignore) {

0 commit comments

Comments
 (0)