Merge pull request #40 from kakotor/develop

增加设置setConnectionManagerShared参数
This commit is contained in:
Binary Wang 2016-09-14 17:48:41 +08:00 committed by GitHub
commit b26a01767d

View File

@ -1,8 +1,6 @@
package me.chanjar.weixin.common.util.http; package me.chanjar.weixin.common.util.http;
import java.io.IOException; import me.chanjar.weixin.common.util.StringUtils;
import java.util.concurrent.TimeUnit;
import org.apache.http.annotation.NotThreadSafe; import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials; 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.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpContext;
import me.chanjar.weixin.common.util.StringUtils; import java.io.IOException;
import java.util.concurrent.TimeUnit;
/** /**
* httpclient 连接管理器 * httpclient 连接管理器
@ -104,44 +103,45 @@ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder {
private void prepare() { private void prepare() {
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", this.plainConnectionSocketFactory) .register("http", this.plainConnectionSocketFactory)
.register("https", this.sslConnectionSocketFactory) .register("https", this.sslConnectionSocketFactory)
.build(); .build();
@SuppressWarnings("resource") @SuppressWarnings("resource")
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
connectionManager.setMaxTotal(this.maxTotalConn); connectionManager.setMaxTotal(this.maxTotalConn);
connectionManager.setDefaultMaxPerRoute(this.maxConnPerHost); connectionManager.setDefaultMaxPerRoute(this.maxConnPerHost);
connectionManager.setDefaultSocketConfig( connectionManager.setDefaultSocketConfig(
SocketConfig.copy(SocketConfig.DEFAULT) SocketConfig.copy(SocketConfig.DEFAULT)
.setSoTimeout(this.soTimeout) .setSoTimeout(this.soTimeout)
.build() .build()
); );
this.idleConnectionMonitorThread = new IdleConnectionMonitorThread( this.idleConnectionMonitorThread = new IdleConnectionMonitorThread(
connectionManager, this.idleConnTimeout, this.checkWaitTime); connectionManager, this.idleConnTimeout, this.checkWaitTime);
this.idleConnectionMonitorThread.setDaemon(true); this.idleConnectionMonitorThread.setDaemon(true);
this.idleConnectionMonitorThread.start(); this.idleConnectionMonitorThread.start();
this.httpClientBuilder = HttpClients.custom() this.httpClientBuilder = HttpClients.custom()
.setConnectionManager(connectionManager) .setConnectionManager(connectionManager)
.setDefaultRequestConfig( .setConnectionManagerShared(true)
RequestConfig.custom() .setDefaultRequestConfig(
.setSocketTimeout(this.soTimeout) RequestConfig.custom()
.setConnectTimeout(this.connectionTimeout) .setSocketTimeout(this.soTimeout)
.setConnectionRequestTimeout(this.connectionRequestTimeout) .setConnectTimeout(this.connectionTimeout)
.build() .setConnectionRequestTimeout(this.connectionRequestTimeout)
) .build()
.setRetryHandler(this.httpRequestRetryHandler); )
.setRetryHandler(this.httpRequestRetryHandler);
if (StringUtils.isNotBlank(this.httpProxyHost) if (StringUtils.isNotBlank(this.httpProxyHost)
&& StringUtils.isNotBlank(this.httpProxyUsername)) { && StringUtils.isNotBlank(this.httpProxyUsername)) {
// 使用代理服务器 需要用户认证的代理服务器 // 使用代理服务器 需要用户认证的代理服务器
CredentialsProvider provider = new BasicCredentialsProvider(); CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials( provider.setCredentials(
new AuthScope(this.httpProxyHost, this.httpProxyPort), new AuthScope(this.httpProxyHost, this.httpProxyPort),
new UsernamePasswordCredentials(this.httpProxyUsername, new UsernamePasswordCredentials(this.httpProxyUsername,
this.httpProxyPassword)); this.httpProxyPassword));
this.httpClientBuilder.setDefaultCredentialsProvider(provider); this.httpClientBuilder.setDefaultCredentialsProvider(provider);
} }
@ -182,7 +182,7 @@ public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder {
wait(this.checkWaitTime); wait(this.checkWaitTime);
this.connMgr.closeExpiredConnections(); this.connMgr.closeExpiredConnections();
this.connMgr.closeIdleConnections(this.idleConnTimeout, this.connMgr.closeIdleConnections(this.idleConnTimeout,
TimeUnit.MILLISECONDS); TimeUnit.MILLISECONDS);
} }
} }
} catch (InterruptedException ignore) { } catch (InterruptedException ignore) {