优化HttpClientFatory

This commit is contained in:
jinyu 2017-03-17 09:51:23 +08:00
parent 08d6a84d7f
commit 56ed5c8d72
16 changed files with 135 additions and 184 deletions

1
.gitignore vendored
View File

@ -29,4 +29,5 @@ target/*
*.tmp *.tmp
Thumbs.db Thumbs.db
/target/ /target/
weixin4j-future
.DS_Store .DS_Store

View File

@ -71,11 +71,6 @@ public class SimpleHttpClient extends AbstractHttpClient {
// create connection object // create connection object
HttpURLConnection connection = createHttpConnection(request); HttpURLConnection connection = createHttpConnection(request);
String method = request.getMethod().name(); String method = request.getMethod().name();
// set parameters
if (params != null) {
connection.setConnectTimeout(params.getConnectTimeout());
connection.setReadTimeout(params.getReadTimeout());
}
connection.setRequestMethod(method); connection.setRequestMethod(method);
connection.setDoInput(true); connection.setDoInput(true);
connection.setInstanceFollowRedirects("GET".equals(method)); connection.setInstanceFollowRedirects("GET".equals(method));

View File

@ -125,39 +125,17 @@ public abstract class HttpClientFactory {
*/ */
public static HttpClient getInstance(HttpParams params) { public static HttpClient getInstance(HttpParams params) {
HttpClientFactory clientFactory = getDefaultFactory(); HttpClientFactory clientFactory = getDefaultFactory();
if (params != null) { return clientFactory.newInstance(params);
clientFactory.resolveHttpParams(params);
}
return clientFactory.newInstance();
} }
/**
* Resolve the Http Parameter
*
* @param params
* 请求参数
*/
public void resolveHttpParams(HttpParams params){
if (params == null) {
throw new IllegalArgumentException("'params' must not be empty");
}
resolveHttpParams0(params);
}
/**
* Resolve the Http Parameter
*
* @param params
* 请求参数
*/
protected abstract void resolveHttpParams0(HttpParams params);
/** /**
* 获取HttpClient实例 * 获取HttpClient实例
* *
* @param params
* http参数 可为空
* @return * @return
*/ */
public abstract HttpClient newInstance(); public abstract HttpClient newInstance(HttpParams params);
public static SSLContext allowSSLContext() { public static SSLContext allowSSLContext() {
try { try {

View File

@ -15,15 +15,8 @@ import com.foxinmy.weixin4j.http.SimpleHttpClient;
*/ */
public class SimpleHttpClientFactory extends HttpClientFactory { public class SimpleHttpClientFactory extends HttpClientFactory {
private HttpParams params;
@Override @Override
protected void resolveHttpParams0(HttpParams params) { public HttpClient newInstance(HttpParams params) {
this.params = params;
}
@Override
public HttpClient newInstance() {
return new SimpleHttpClient(params); return new SimpleHttpClient(params);
} }
} }

View File

@ -4,7 +4,6 @@ import java.net.InetSocketAddress;
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpConnectionManager; import org.apache.commons.httpclient.HttpConnectionManager;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.protocol.Protocol; import org.apache.commons.httpclient.protocol.Protocol;
import com.foxinmy.weixin4j.http.HttpParams; import com.foxinmy.weixin4j.http.HttpParams;
@ -40,25 +39,6 @@ public class HttpComponent3Factory extends HttpClientFactory {
this.httpClient = httpClient; this.httpClient = httpClient;
} }
/**
* Resolve Parameter
*/
@Override
protected void resolveHttpParams0(HttpParams params) {
if (params.getProxy() != null) {
InetSocketAddress socketAddress = (InetSocketAddress) params
.getProxy().address();
httpClient.getHostConfiguration().setProxy(
socketAddress.getHostName(), socketAddress.getPort());
}
if (params.getSSLContext() != null) {
Protocol.registerProtocol("https", new Protocol("https",
new SSLProtocolSocketFactory(params.getSSLContext()), 443));
}
httpClient.getHttpConnectionManager().getParams()
.setConnectionTimeout(params.getConnectTimeout());
}
public void setHttpConnectionManager( public void setHttpConnectionManager(
HttpConnectionManager httpConnectionManager) { HttpConnectionManager httpConnectionManager) {
if (httpConnectionManager == null) { if (httpConnectionManager == null) {
@ -68,16 +48,27 @@ public class HttpComponent3Factory extends HttpClientFactory {
httpClient.setHttpConnectionManager(httpConnectionManager); httpClient.setHttpConnectionManager(httpConnectionManager);
} }
public void setHttpClientParams(HttpClientParams httpClientParams) { private void resolveHttpParams(HttpParams params) {
if (httpClientParams == null) { if (params != null) {
throw new IllegalArgumentException( if (params.getProxy() != null) {
"'httpClientParams' must not be null"); InetSocketAddress socketAddress = (InetSocketAddress) params
.getProxy().address();
httpClient.getHostConfiguration().setProxy(
socketAddress.getHostName(), socketAddress.getPort());
}
if (params.getSSLContext() != null) {
Protocol.registerProtocol("https", new Protocol("https",
new SSLProtocolSocketFactory(params.getSSLContext()),
443));
}
httpClient.getHttpConnectionManager().getParams()
.setConnectionTimeout(params.getConnectTimeout());
} }
httpClient.setParams(httpClientParams);
} }
@Override @Override
public com.foxinmy.weixin4j.http.HttpClient newInstance() { public com.foxinmy.weixin4j.http.HttpClient newInstance(HttpParams params) {
resolveHttpParams(params);
return new HttpComponent3(httpClient); return new HttpComponent3(httpClient);
} }
} }

View File

@ -36,12 +36,7 @@ public class HttpComponent4Factory extends HttpClientFactory {
} }
@Override @Override
public HttpClient newInstance() { public HttpClient newInstance(HttpParams params) {
return httpComponentFactory.newInstance(); return httpComponentFactory.newInstance(params);
}
@Override
protected void resolveHttpParams0(HttpParams params) {
httpComponentFactory.resolveHttpParams(params);
} }
} }

View File

@ -70,34 +70,6 @@ public class HttpComponent4_1Factory extends HttpClientFactory {
this.httpClient = httpClient; this.httpClient = httpClient;
} }
@Override
protected void resolveHttpParams0(HttpParams params) {
if (params.getProxy() != null) {
InetSocketAddress socketAddress = (InetSocketAddress) params
.getProxy().address();
HttpHost proxy = new HttpHost(socketAddress.getHostName(),
socketAddress.getPort());
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
proxy);
}
httpClient.getParams().setIntParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT,
params.getConnectTimeout());
httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT,
params.getReadTimeout());
if (params.getSSLContext() != null) {
SSLSocketFactory socketFactory = new SSLSocketFactory(
params.getSSLContext());
if (params.getHostnameVerifier() != null) {
socketFactory.setHostnameVerifier(new CustomHostnameVerifier(
params.getHostnameVerifier()));
}
Scheme scheme = new Scheme("https", socketFactory, 443);
httpClient.getConnectionManager().getSchemeRegistry()
.register(scheme);
}
}
/** /**
* 设置Http参数 * 设置Http参数
* *
@ -111,8 +83,39 @@ public class HttpComponent4_1Factory extends HttpClientFactory {
httpClient.getParams().setParameter(name, value); httpClient.getParams().setParameter(name, value);
} }
private void resolveHttpParams(HttpParams params) {
if (params != null) {
if (params.getProxy() != null) {
InetSocketAddress socketAddress = (InetSocketAddress) params
.getProxy().address();
HttpHost proxy = new HttpHost(socketAddress.getHostName(),
socketAddress.getPort());
httpClient.getParams().setParameter(
ConnRoutePNames.DEFAULT_PROXY, proxy);
}
httpClient.getParams().setIntParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT,
params.getConnectTimeout());
httpClient.getParams().setIntParameter(
CoreConnectionPNames.SO_TIMEOUT, params.getReadTimeout());
if (params.getSSLContext() != null) {
SSLSocketFactory socketFactory = new SSLSocketFactory(
params.getSSLContext());
if (params.getHostnameVerifier() != null) {
socketFactory
.setHostnameVerifier(new CustomHostnameVerifier(
params.getHostnameVerifier()));
}
Scheme scheme = new Scheme("https", socketFactory, 443);
httpClient.getConnectionManager().getSchemeRegistry()
.register(scheme);
}
}
}
@Override @Override
public HttpClient newInstance() { public HttpClient newInstance(HttpParams params) {
resolveHttpParams(params);
return new HttpComponent4_1(httpClient); return new HttpComponent4_1(httpClient);
} }
} }

View File

@ -45,32 +45,6 @@ public class HttpComponent4_2Factory extends HttpClientFactory {
this.clientBuilder = clientBuilder; this.clientBuilder = clientBuilder;
} }
@Override
protected void resolveHttpParams0(HttpParams params) {
clientBuilder.setDefaultRequestConfig(RequestConfig.custom()
.setConnectTimeout(params.getConnectTimeout())
.setConnectionRequestTimeout(params.getReadTimeout()).build());
if (params.getProxy() != null) {
InetSocketAddress socketAddress = (InetSocketAddress) params
.getProxy().address();
HttpHost proxy = new HttpHost(socketAddress.getHostName(),
socketAddress.getPort());
clientBuilder.setProxy(proxy);
}
if (params.getSSLContext() != null) {
clientBuilder
.setSSLSocketFactory(new SSLConnectionSocketFactory(
params.getSSLContext(),
params.getHostnameVerifier() != null ? new CustomHostnameVerifier(
params.getHostnameVerifier())
: SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER));
}
if (params.getHostnameVerifier() != null) {
clientBuilder.setHostnameVerifier(new CustomHostnameVerifier(params
.getHostnameVerifier()));
}
}
public HttpComponent4_2Factory setDefaultConnectionConfig( public HttpComponent4_2Factory setDefaultConnectionConfig(
ConnectionConfig connectionConfig) { ConnectionConfig connectionConfig) {
clientBuilder.setDefaultConnectionConfig(connectionConfig); clientBuilder.setDefaultConnectionConfig(connectionConfig);
@ -100,8 +74,37 @@ public class HttpComponent4_2Factory extends HttpClientFactory {
return this; return this;
} }
private void resolveHttpParams(HttpParams params) {
if (params != null) {
clientBuilder.setDefaultRequestConfig(RequestConfig.custom()
.setConnectTimeout(params.getConnectTimeout())
.setConnectionRequestTimeout(params.getReadTimeout())
.build());
if (params.getProxy() != null) {
InetSocketAddress socketAddress = (InetSocketAddress) params
.getProxy().address();
HttpHost proxy = new HttpHost(socketAddress.getHostName(),
socketAddress.getPort());
clientBuilder.setProxy(proxy);
}
if (params.getSSLContext() != null) {
clientBuilder
.setSSLSocketFactory(new SSLConnectionSocketFactory(
params.getSSLContext(),
params.getHostnameVerifier() != null ? new CustomHostnameVerifier(
params.getHostnameVerifier())
: SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER));
}
if (params.getHostnameVerifier() != null) {
clientBuilder.setHostnameVerifier(new CustomHostnameVerifier(
params.getHostnameVerifier()));
}
}
}
@Override @Override
public HttpClient newInstance() { public HttpClient newInstance(HttpParams params) {
resolveHttpParams(params);
return new HttpComponent4_2(clientBuilder.build()); return new HttpComponent4_2(clientBuilder.build());
} }
} }

View File

@ -34,7 +34,6 @@ public class Netty4HttpClientFactory extends HttpClientFactory {
private volatile Bootstrap bootstrap; private volatile Bootstrap bootstrap;
private EventLoopGroup eventLoopGroup; private EventLoopGroup eventLoopGroup;
private Map<ChannelOption<?>, ?> options; private Map<ChannelOption<?>, ?> options;
private HttpParams params;
public Netty4HttpClientFactory() { public Netty4HttpClientFactory() {
this(new NioEventLoopGroup( this(new NioEventLoopGroup(
@ -45,11 +44,6 @@ public class Netty4HttpClientFactory extends HttpClientFactory {
this.eventLoopGroup = eventLoopGroup; this.eventLoopGroup = eventLoopGroup;
} }
@Override
protected void resolveHttpParams0(HttpParams params) {
this.params = params;
}
public Netty4HttpClientFactory setOptions(Map<ChannelOption<?>, ?> options) { public Netty4HttpClientFactory setOptions(Map<ChannelOption<?>, ?> options) {
if (options == null) { if (options == null) {
throw new IllegalArgumentException("'options' must not be empty"); throw new IllegalArgumentException("'options' must not be empty");
@ -58,7 +52,7 @@ public class Netty4HttpClientFactory extends HttpClientFactory {
return this; return this;
} }
private Bootstrap getBootstrap() { private Bootstrap getBootstrap(final HttpParams params) {
if (bootstrap == null) { if (bootstrap == null) {
bootstrap = new Bootstrap(); bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class) bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class)
@ -90,7 +84,7 @@ public class Netty4HttpClientFactory extends HttpClientFactory {
} }
@Override @Override
public HttpClient newInstance() { public HttpClient newInstance(HttpParams params) {
return new Netty4HttpClient(getBootstrap(), params); return new Netty4HttpClient(getBootstrap(params), params);
} }
} }

View File

@ -35,20 +35,22 @@ public class OkHttpClient2Factory extends HttpClientFactory {
this.okClient = okClient; this.okClient = okClient;
} }
@Override private void resolveHttpParams(HttpParams params) {
protected void resolveHttpParams0(HttpParams params) { if (params != null) {
okClient.setConnectTimeout(params.getConnectTimeout(), okClient.setConnectTimeout(params.getConnectTimeout(),
TimeUnit.MILLISECONDS); TimeUnit.MILLISECONDS);
okClient.setReadTimeout(params.getReadTimeout(), TimeUnit.MILLISECONDS); okClient.setReadTimeout(params.getReadTimeout(),
if (params.getProxy() != null) { TimeUnit.MILLISECONDS);
okClient.setProxy(params.getProxy()); if (params.getProxy() != null) {
} okClient.setProxy(params.getProxy());
if (params.getSSLContext() != null) { }
okClient.setSslSocketFactory(params.getSSLContext() if (params.getSSLContext() != null) {
.getSocketFactory()); okClient.setSslSocketFactory(params.getSSLContext()
} .getSocketFactory());
if (params.getHostnameVerifier() != null) { }
okClient.setHostnameVerifier(params.getHostnameVerifier()); if (params.getHostnameVerifier() != null) {
okClient.setHostnameVerifier(params.getHostnameVerifier());
}
} }
} }
@ -83,7 +85,8 @@ public class OkHttpClient2Factory extends HttpClientFactory {
} }
@Override @Override
public HttpClient newInstance() { public HttpClient newInstance(HttpParams params) {
resolveHttpParams(params);
return new OkHttpClient2(okClient); return new OkHttpClient2(okClient);
} }
} }

View File

@ -43,22 +43,23 @@ public class OkHttpClient3Factory extends HttpClientFactory {
* resolve Request.Parameter * resolve Request.Parameter
* *
* */ * */
@Override private void resolveHttpParams(HttpParams params) {
protected void resolveHttpParams0(HttpParams params) { if (params != null) {
clientBuilder.connectTimeout(params.getConnectTimeout(), clientBuilder.connectTimeout(params.getConnectTimeout(),
TimeUnit.MILLISECONDS); TimeUnit.MILLISECONDS);
clientBuilder.readTimeout(params.getReadTimeout(), clientBuilder.readTimeout(params.getReadTimeout(),
TimeUnit.MILLISECONDS); TimeUnit.MILLISECONDS);
if (params.getProxy() != null) { if (params.getProxy() != null) {
clientBuilder.proxy(params.getProxy()); clientBuilder.proxy(params.getProxy());
} }
if (params.getSSLContext() != null) { if (params.getSSLContext() != null) {
clientBuilder.sslSocketFactory(params.getSSLContext() clientBuilder.sslSocketFactory(params.getSSLContext()
.getSocketFactory(), .getSocketFactory(),
HttpClientFactory.AllowX509TrustManager.GLOBAL); HttpClientFactory.AllowX509TrustManager.GLOBAL);
} }
if (params.getHostnameVerifier() != null) { if (params.getHostnameVerifier() != null) {
clientBuilder.hostnameVerifier(params.getHostnameVerifier()); clientBuilder.hostnameVerifier(params.getHostnameVerifier());
}
} }
} }
@ -104,7 +105,8 @@ public class OkHttpClient3Factory extends HttpClientFactory {
} }
@Override @Override
public HttpClient newInstance() { public HttpClient newInstance(HttpParams params) {
resolveHttpParams(params);
if (okClient == null) { if (okClient == null) {
okClient = clientBuilder.build(); okClient = clientBuilder.build();
} }

View File

@ -27,12 +27,7 @@ public class OkHttpClientFactory extends HttpClientFactory {
} }
@Override @Override
protected void resolveHttpParams0(HttpParams params) { public HttpClient newInstance(HttpParams params) {
okHttpClientFactory.resolveHttpParams(params); return okHttpClientFactory.newInstance(params);
}
@Override
public HttpClient newInstance() {
return okHttpClientFactory.newInstance();
} }
} }

View File

@ -85,9 +85,7 @@ public class JSSDKConfigurator {
*/ */
public JSSDKConfigurator apis(JSSDKAPI[]... apis) { public JSSDKConfigurator apis(JSSDKAPI[]... apis) {
for (JSSDKAPI[] api : apis) { for (JSSDKAPI[] api : apis) {
for (JSSDKAPI apii : api) { apis(api);
this.apis.add(apii);
}
} }
return this; return this;
} }

View File

@ -34,7 +34,7 @@ public abstract class HttpClientTest {
protected abstract HttpClientFactory createHttpFactory(); protected abstract HttpClientFactory createHttpFactory();
protected HttpClient createHttpClient() { protected HttpClient createHttpClient() {
return createHttpFactory().newInstance(); return createHttpFactory().newInstance(null);
} }
protected HttpClient createProxyHttpClient() { protected HttpClient createProxyHttpClient() {
@ -60,8 +60,8 @@ public abstract class HttpClientTest {
protected HttpClient createHttpClient(HttpParams params) { protected HttpClient createHttpClient(HttpParams params) {
HttpClientFactory httpClientFactory = createHttpFactory(); HttpClientFactory httpClientFactory = createHttpFactory();
httpClientFactory.setDefaultParams(params); HttpClientFactory.setDefaultParams(params);
return httpClientFactory.newInstance(); return httpClientFactory.newInstance(null);
} }
@Test @Test

View File

@ -3,6 +3,7 @@ package com.foxinmy.weixin4j.example.server;
import com.foxinmy.weixin4j.exception.WeixinException; import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.handler.DebugMessageHandler; import com.foxinmy.weixin4j.handler.DebugMessageHandler;
import com.foxinmy.weixin4j.startup.WeixinServerBootstrap; import com.foxinmy.weixin4j.startup.WeixinServerBootstrap;
import com.foxinmy.weixin4j.util.AesToken;
/** /**
* 微信消息服务:单独作为一个服务jar包启动推荐这种方式去处理微信消息可在本项目的根目录运行 `mvn package`得到一个可执行的zip包 * 微信消息服务:单独作为一个服务jar包启动推荐这种方式去处理微信消息可在本项目的根目录运行 `mvn package`得到一个可执行的zip包
@ -38,7 +39,7 @@ public final class Weixin4jServerStartup {
* @throws WeixinException * @throws WeixinException
*/ */
public static void main(String[] args) throws WeixinException { public static void main(String[] args) throws WeixinException {
new WeixinServerBootstrap(aesToken) // 指定开发者token信息 new WeixinServerBootstrap(new AesToken("wxa652fc930afe9b22", "weixin4j", "iFv2hlZm56rkwv5oC45UoIfvPJVHp2ngocBMbt5FP9C")) // 指定开发者token信息
.handlerPackagesToScan(handlerPackage) // 扫描处理消息的包 .handlerPackagesToScan(handlerPackage) // 扫描处理消息的包
.addHandler(DebugMessageHandler.global) // 当没有匹配到消息处理时输出调试信息开发环境打开 .addHandler(DebugMessageHandler.global) // 当没有匹配到消息处理时输出调试信息开发环境打开
.openAlwaysResponse() // 当没有匹配到消息处理时输出空白回复(公众号不会出现该公众号无法提供服务的提示)正式环境打开 .openAlwaysResponse() // 当没有匹配到消息处理时输出空白回复(公众号不会出现该公众号无法提供服务的提示)正式环境打开

View File

@ -19,7 +19,6 @@ import com.foxinmy.weixin4j.response.WeixinResponse;
*/ */
@Component @Component
public class TextMessageHandler extends MessageHandlerAdapter<TextMessage> { public class TextMessageHandler extends MessageHandlerAdapter<TextMessage> {
@Override @Override
public WeixinResponse doHandle0(WeixinRequest request, TextMessage message) public WeixinResponse doHandle0(WeixinRequest request, TextMessage message)
throws WeixinException { throws WeixinException {