From 15f17b2a27f627662d705ac9159898fc0466e38c Mon Sep 17 00:00:00 2001 From: ben Date: Wed, 14 Sep 2016 16:39:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AE=BE=E7=BD=AEsetConnecti?= =?UTF-8?q?onManagerShared=E5=8F=82=E6=95=B0=E9=81=BF=E5=85=8DPoolingHttpC?= =?UTF-8?q?lientConnectionManager=E8=A2=ABCloseableHttpClient=E8=BF=9E?= =?UTF-8?q?=E5=B8=A6=E5=85=B3=E9=97=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../http/DefaultApacheHttpClientBuilder.java | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/DefaultApacheHttpClientBuilder.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/DefaultApacheHttpClientBuilder.java index f01d0d9e..f69dcc6c 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/DefaultApacheHttpClientBuilder.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/DefaultApacheHttpClientBuilder.java @@ -1,8 +1,6 @@ package me.chanjar.weixin.common.util.http; -import java.io.IOException; -import java.util.concurrent.TimeUnit; - +import me.chanjar.weixin.common.util.StringUtils; import org.apache.http.annotation.NotThreadSafe; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; @@ -23,7 +21,8 @@ import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.protocol.HttpContext; -import me.chanjar.weixin.common.util.StringUtils; +import java.io.IOException; +import java.util.concurrent.TimeUnit; /** * httpclient 连接管理器 @@ -104,44 +103,45 @@ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder { private void prepare() { Registry registry = RegistryBuilder.create() - .register("http", this.plainConnectionSocketFactory) - .register("https", this.sslConnectionSocketFactory) - .build(); + .register("http", this.plainConnectionSocketFactory) + .register("https", this.sslConnectionSocketFactory) + .build(); @SuppressWarnings("resource") PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry); connectionManager.setMaxTotal(this.maxTotalConn); connectionManager.setDefaultMaxPerRoute(this.maxConnPerHost); connectionManager.setDefaultSocketConfig( - SocketConfig.copy(SocketConfig.DEFAULT) - .setSoTimeout(this.soTimeout) - .build() + SocketConfig.copy(SocketConfig.DEFAULT) + .setSoTimeout(this.soTimeout) + .build() ); this.idleConnectionMonitorThread = new IdleConnectionMonitorThread( - connectionManager, this.idleConnTimeout, this.checkWaitTime); + connectionManager, this.idleConnTimeout, this.checkWaitTime); this.idleConnectionMonitorThread.setDaemon(true); this.idleConnectionMonitorThread.start(); this.httpClientBuilder = HttpClients.custom() - .setConnectionManager(connectionManager) - .setDefaultRequestConfig( - RequestConfig.custom() - .setSocketTimeout(this.soTimeout) - .setConnectTimeout(this.connectionTimeout) - .setConnectionRequestTimeout(this.connectionRequestTimeout) - .build() - ) - .setRetryHandler(this.httpRequestRetryHandler); + .setConnectionManager(connectionManager) + .setConnectionManagerShared(true) + .setDefaultRequestConfig( + RequestConfig.custom() + .setSocketTimeout(this.soTimeout) + .setConnectTimeout(this.connectionTimeout) + .setConnectionRequestTimeout(this.connectionRequestTimeout) + .build() + ) + .setRetryHandler(this.httpRequestRetryHandler); if (StringUtils.isNotBlank(this.httpProxyHost) - && StringUtils.isNotBlank(this.httpProxyUsername)) { + && StringUtils.isNotBlank(this.httpProxyUsername)) { // 使用代理服务器 需要用户认证的代理服务器 CredentialsProvider provider = new BasicCredentialsProvider(); provider.setCredentials( - new AuthScope(this.httpProxyHost, this.httpProxyPort), - new UsernamePasswordCredentials(this.httpProxyUsername, - this.httpProxyPassword)); + new AuthScope(this.httpProxyHost, this.httpProxyPort), + new UsernamePasswordCredentials(this.httpProxyUsername, + this.httpProxyPassword)); this.httpClientBuilder.setDefaultCredentialsProvider(provider); } @@ -182,7 +182,7 @@ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder { wait(this.checkWaitTime); this.connMgr.closeExpiredConnections(); this.connMgr.closeIdleConnections(this.idleConnTimeout, - TimeUnit.MILLISECONDS); + TimeUnit.MILLISECONDS); } } } catch (InterruptedException ignore) {