maxConnections and maxConnectionsPerHost

This commit is contained in:
jinyu
2020-07-24 12:55:37 +08:00
parent 4bd6d6ded7
commit 7fd6db2758
9 changed files with 55 additions and 35 deletions
@@ -17,11 +17,19 @@ public final class HttpParams {
/**
* 连接超时时间(单位毫秒)
*/
private int connectTimeout;
private final int connectTimeout;
/**
* 读取超时时间(单位毫秒)
*/
private int readTimeout;
private final int readTimeout;
/**
* 最大连接数
*/
private final int maxConnections;
/**
* 每个host最大连接数
*/
private final int maxConnectionsPerHost;
/**
* 代理对象
*/
@@ -36,15 +44,17 @@ public final class HttpParams {
private HostnameVerifier hostnameVerifier;
/**
* connectTimeout = 15000,readTimeout=20000
* connectTimeout = 15000,readTimeout=20000,maxConnection=100,maxConnectionPerHost=32
*/
public HttpParams() {
this(5000, 15000);
this(5000, 15000,100,32);
}
public HttpParams(int connectTimeout, int readTimeout) {
public HttpParams(int connectTimeout, int readTimeout,int maxConnections,int maxConnectionsPerHost) {
this.connectTimeout = connectTimeout;
this.readTimeout = readTimeout;
this.maxConnections = maxConnections;
this.maxConnectionsPerHost = maxConnectionsPerHost;
}
public int getConnectTimeout() {
@@ -55,6 +65,14 @@ public final class HttpParams {
return readTimeout;
}
public int getMaxConnections() {
return maxConnections;
}
public int getMaxConnectionsPerHost() {
return maxConnectionsPerHost;
}
public Proxy getProxy() {
return proxy;
}
@@ -84,7 +102,7 @@ public final class HttpParams {
public static HttpParams copy(final HttpParams params) {
return new HttpParams(params.getConnectTimeout(),
params.getReadTimeout()).setProxy(params.getProxy())
params.getReadTimeout(),params.getMaxConnections(),params.getMaxConnectionsPerHost()).setProxy(params.getProxy())
.setHostnameVerifier(params.getHostnameVerifier())
.setSSLContext(params.getSSLContext());
}
@@ -5,6 +5,7 @@ import java.net.InetSocketAddress;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpConnectionManager;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.httpclient.protocol.Protocol;
import com.foxinmy.weixin4j.http.HttpParams;
@@ -62,8 +63,10 @@ public class HttpComponent3Factory extends HttpClientFactory {
new SSLProtocolSocketFactory(params.getSSLContext()),
443));
}
httpClient.getHttpConnectionManager().getParams()
.setConnectionTimeout(params.getConnectTimeout());
HttpConnectionManagerParams params_ = httpClient.getHttpConnectionManager().getParams();
params_.setMaxTotalConnections(params.getMaxConnections());
params_.setDefaultMaxConnectionsPerHost(params.getMaxConnectionsPerHost());
params_.setConnectionTimeout(params.getConnectTimeout());
}
}
@@ -40,14 +40,9 @@ public class HttpComponent4_1Factory extends HttpClientFactory {
*
* @see <a
* href="https://issues.apache.org/jira/browse/HTTPCLIENT-1193">HTTPCLIENT-1193</a>
* @param clientConnectionManager
*/
public HttpComponent4_1Factory() {
PoolingClientConnectionManager clientConnectionManager = new PoolingClientConnectionManager();
clientConnectionManager.setMaxTotal(30);
clientConnectionManager.setDefaultMaxPerRoute(clientConnectionManager
.getMaxTotal());
httpClient = new DefaultHttpClient(clientConnectionManager);
httpClient = new DefaultHttpClient(new PoolingClientConnectionManager());
httpClient.getParams().setParameter(
CoreProtocolPNames.HTTP_CONTENT_CHARSET, Consts.UTF_8);
httpClient.getParams().setParameter(
@@ -110,6 +105,11 @@ public class HttpComponent4_1Factory extends HttpClientFactory {
httpClient.getConnectionManager().getSchemeRegistry()
.register(scheme);
}
ClientConnectionManager connectionManager = httpClient.getConnectionManager();
if(connectionManager instanceof PoolingClientConnectionManager){
((PoolingClientConnectionManager) connectionManager).setMaxTotal(params.getMaxConnections());
((PoolingClientConnectionManager) connectionManager).setDefaultMaxPerRoute(params.getMaxConnectionsPerHost());
}
}
}
@@ -99,6 +99,8 @@ public class HttpComponent4_2Factory extends HttpClientFactory {
clientBuilder.setHostnameVerifier(new CustomHostnameVerifier(
params.getHostnameVerifier()));
}
clientBuilder.setMaxConnTotal(params.getMaxConnections());
clientBuilder.setMaxConnPerRoute(params.getMaxConnectionsPerHost());
}
}
@@ -50,6 +50,8 @@ public class OkHttpClient2Factory extends HttpClientFactory {
if (params.getHostnameVerifier() != null) {
okClient.setHostnameVerifier(params.getHostnameVerifier());
}
okClient.getDispatcher().setMaxRequests(params.getMaxConnections());
okClient.getDispatcher().setMaxRequestsPerHost(params.getMaxConnectionsPerHost());
}
}
@@ -60,6 +60,10 @@ public class OkHttpClient3Factory extends HttpClientFactory {
if (params.getHostnameVerifier() != null) {
clientBuilder.hostnameVerifier(params.getHostnameVerifier());
}
Dispatcher dispatcher = new Dispatcher();
dispatcher.setMaxRequests(params.getMaxConnections());
dispatcher.setMaxRequestsPerHost(params.getMaxConnectionsPerHost());
clientBuilder.dispatcher(dispatcher);
}
}