重构DefaultApacheHttpClientBuilder

This commit is contained in:
BinaryWang 2016-08-26 14:29:29 +08:00
parent b5d9ce94ff
commit 7bd75c92a5
3 changed files with 31 additions and 38 deletions

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,13 +21,14 @@ 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 连接管理器
*/ */
@NotThreadSafe @NotThreadSafe
public class DefaultApacheHttpHttpClientBuilder implements ApacheHttpClientBuilder { public class DefaultApacheHttpClientBuilder implements ApacheHttpClientBuilder {
private int connectionRequestTimeout = 3000; private int connectionRequestTimeout = 3000;
private int connectionTimeout = 5000; private int connectionTimeout = 5000;
private int soTimeout = 5000; private int soTimeout = 5000;
@ -52,27 +51,20 @@ public class DefaultApacheHttpHttpClientBuilder implements ApacheHttpClientBuild
private String httpProxyUsername; private String httpProxyUsername;
private String httpProxyPassword; private String httpProxyPassword;
/**
* 连接管理器
*/
private PoolingHttpClientConnectionManager connectionManager;
/** /**
* 闲置连接监控线程 * 闲置连接监控线程
*/ */
private IdleConnectionMonitorThread idleConnectionMonitorThread; private IdleConnectionMonitorThread idleConnectionMonitorThread;
/**
* httpClientBuilder
*/
private HttpClientBuilder httpClientBuilder; private HttpClientBuilder httpClientBuilder;
private boolean prepared = false; private boolean prepared = false;
private DefaultApacheHttpHttpClientBuilder() { private DefaultApacheHttpClientBuilder() {
} }
public static DefaultApacheHttpHttpClientBuilder get() { public static DefaultApacheHttpClientBuilder get() {
return new DefaultApacheHttpHttpClientBuilder(); return new DefaultApacheHttpClientBuilder();
} }
@Override @Override
@ -111,43 +103,44 @@ public class DefaultApacheHttpHttpClientBuilder implements ApacheHttpClientBuild
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();
this.connectionManager = new PoolingHttpClientConnectionManager(registry);
this.connectionManager.setMaxTotal(this.maxTotalConn); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
this.connectionManager.setDefaultMaxPerRoute(this.maxConnPerHost); connectionManager.setMaxTotal(this.maxTotalConn);
this.connectionManager.setDefaultSocketConfig( connectionManager.setDefaultMaxPerRoute(this.maxConnPerHost);
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(
this.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(this.connectionManager) .setConnectionManager(connectionManager)
.setDefaultRequestConfig( .setDefaultRequestConfig(
RequestConfig.custom() RequestConfig.custom()
.setSocketTimeout(this.soTimeout) .setSocketTimeout(this.soTimeout)
.setConnectTimeout(this.connectionTimeout) .setConnectTimeout(this.connectionTimeout)
.setConnectionRequestTimeout(this.connectionRequestTimeout) .setConnectionRequestTimeout(this.connectionRequestTimeout)
.build() .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 credsProvider = new BasicCredentialsProvider(); CredentialsProvider provider = new BasicCredentialsProvider();
credsProvider.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(credsProvider); this.httpClientBuilder.setDefaultCredentialsProvider(provider);
} }
if (StringUtils.isNotBlank(this.userAgent)) { if (StringUtils.isNotBlank(this.userAgent)) {
@ -187,7 +180,7 @@ public class DefaultApacheHttpHttpClientBuilder implements ApacheHttpClientBuild
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) {

View File

@ -591,7 +591,7 @@ public class WxCpServiceImpl implements WxCpService {
this.wxCpConfigStorage = wxConfigProvider; this.wxCpConfigStorage = wxConfigProvider;
ApacheHttpClientBuilder apacheHttpClientBuilder = wxCpConfigStorage.getApacheHttpClientBuilder(); ApacheHttpClientBuilder apacheHttpClientBuilder = wxCpConfigStorage.getApacheHttpClientBuilder();
if (null == apacheHttpClientBuilder) { if (null == apacheHttpClientBuilder) {
apacheHttpClientBuilder = DefaultApacheHttpHttpClientBuilder.get(); apacheHttpClientBuilder = DefaultApacheHttpClientBuilder.get();
} }
apacheHttpClientBuilder.httpProxyHost(wxCpConfigStorage.getHttp_proxy_host()) apacheHttpClientBuilder.httpProxyHost(wxCpConfigStorage.getHttp_proxy_host())
.httpProxyPort(wxCpConfigStorage.getHttp_proxy_port()) .httpProxyPort(wxCpConfigStorage.getHttp_proxy_port())

View File

@ -457,7 +457,7 @@ public class WxMpServiceImpl implements WxMpService {
private void initHttpClient() { private void initHttpClient() {
ApacheHttpClientBuilder apacheHttpClientBuilder = this.wxMpConfigStorage.getApacheHttpClientBuilder(); ApacheHttpClientBuilder apacheHttpClientBuilder = this.wxMpConfigStorage.getApacheHttpClientBuilder();
if (null == apacheHttpClientBuilder) { if (null == apacheHttpClientBuilder) {
apacheHttpClientBuilder = DefaultApacheHttpHttpClientBuilder.get(); apacheHttpClientBuilder = DefaultApacheHttpClientBuilder.get();
} }
apacheHttpClientBuilder.httpProxyHost(this.wxMpConfigStorage.getHttpProxyHost()) apacheHttpClientBuilder.httpProxyHost(this.wxMpConfigStorage.getHttpProxyHost())