优化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
Thumbs.db
/target/
weixin4j-future
.DS_Store

View File

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

View File

@ -125,39 +125,17 @@ public abstract class HttpClientFactory {
*/
public static HttpClient getInstance(HttpParams params) {
HttpClientFactory clientFactory = getDefaultFactory();
if (params != null) {
clientFactory.resolveHttpParams(params);
return clientFactory.newInstance(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实例
*
* @param params
* http参数 可为空
* @return
*/
public abstract HttpClient newInstance();
public abstract HttpClient newInstance(HttpParams params);
public static SSLContext allowSSLContext() {
try {

View File

@ -15,15 +15,8 @@ import com.foxinmy.weixin4j.http.SimpleHttpClient;
*/
public class SimpleHttpClientFactory extends HttpClientFactory {
private HttpParams params;
@Override
protected void resolveHttpParams0(HttpParams params) {
this.params = params;
}
@Override
public HttpClient newInstance() {
public HttpClient newInstance(HttpParams 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.HttpConnectionManager;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.protocol.Protocol;
import com.foxinmy.weixin4j.http.HttpParams;
@ -40,25 +39,6 @@ public class HttpComponent3Factory extends HttpClientFactory {
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(
HttpConnectionManager httpConnectionManager) {
if (httpConnectionManager == null) {
@ -68,16 +48,27 @@ public class HttpComponent3Factory extends HttpClientFactory {
httpClient.setHttpConnectionManager(httpConnectionManager);
}
public void setHttpClientParams(HttpClientParams httpClientParams) {
if (httpClientParams == null) {
throw new IllegalArgumentException(
"'httpClientParams' must not be null");
private void resolveHttpParams(HttpParams params) {
if (params != null) {
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());
}
httpClient.setParams(httpClientParams);
}
@Override
public com.foxinmy.weixin4j.http.HttpClient newInstance() {
public com.foxinmy.weixin4j.http.HttpClient newInstance(HttpParams params) {
resolveHttpParams(params);
return new HttpComponent3(httpClient);
}
}

View File

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

View File

@ -70,34 +70,6 @@ public class HttpComponent4_1Factory extends HttpClientFactory {
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参数
*
@ -111,8 +83,39 @@ public class HttpComponent4_1Factory extends HttpClientFactory {
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
public HttpClient newInstance() {
public HttpClient newInstance(HttpParams params) {
resolveHttpParams(params);
return new HttpComponent4_1(httpClient);
}
}

View File

@ -45,32 +45,6 @@ public class HttpComponent4_2Factory extends HttpClientFactory {
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(
ConnectionConfig connectionConfig) {
clientBuilder.setDefaultConnectionConfig(connectionConfig);
@ -100,8 +74,37 @@ public class HttpComponent4_2Factory extends HttpClientFactory {
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
public HttpClient newInstance() {
public HttpClient newInstance(HttpParams params) {
resolveHttpParams(params);
return new HttpComponent4_2(clientBuilder.build());
}
}

View File

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

View File

@ -35,11 +35,12 @@ public class OkHttpClient2Factory extends HttpClientFactory {
this.okClient = okClient;
}
@Override
protected void resolveHttpParams0(HttpParams params) {
private void resolveHttpParams(HttpParams params) {
if (params != null) {
okClient.setConnectTimeout(params.getConnectTimeout(),
TimeUnit.MILLISECONDS);
okClient.setReadTimeout(params.getReadTimeout(), TimeUnit.MILLISECONDS);
okClient.setReadTimeout(params.getReadTimeout(),
TimeUnit.MILLISECONDS);
if (params.getProxy() != null) {
okClient.setProxy(params.getProxy());
}
@ -51,6 +52,7 @@ public class OkHttpClient2Factory extends HttpClientFactory {
okClient.setHostnameVerifier(params.getHostnameVerifier());
}
}
}
public OkHttpClient2Factory setWriteTimeout(int writeTimeout) {
okClient.setWriteTimeout(writeTimeout, TimeUnit.MILLISECONDS);
@ -83,7 +85,8 @@ public class OkHttpClient2Factory extends HttpClientFactory {
}
@Override
public HttpClient newInstance() {
public HttpClient newInstance(HttpParams params) {
resolveHttpParams(params);
return new OkHttpClient2(okClient);
}
}

View File

@ -43,8 +43,8 @@ public class OkHttpClient3Factory extends HttpClientFactory {
* resolve Request.Parameter
*
* */
@Override
protected void resolveHttpParams0(HttpParams params) {
private void resolveHttpParams(HttpParams params) {
if (params != null) {
clientBuilder.connectTimeout(params.getConnectTimeout(),
TimeUnit.MILLISECONDS);
clientBuilder.readTimeout(params.getReadTimeout(),
@ -61,6 +61,7 @@ public class OkHttpClient3Factory extends HttpClientFactory {
clientBuilder.hostnameVerifier(params.getHostnameVerifier());
}
}
}
public OkHttpClient3Factory setWriteTimeout(int writeTimeout) {
clientBuilder.writeTimeout(writeTimeout, TimeUnit.MILLISECONDS);
@ -104,7 +105,8 @@ public class OkHttpClient3Factory extends HttpClientFactory {
}
@Override
public HttpClient newInstance() {
public HttpClient newInstance(HttpParams params) {
resolveHttpParams(params);
if (okClient == null) {
okClient = clientBuilder.build();
}

View File

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

View File

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

View File

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

View File

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

View File

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