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

View File

@ -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());
}

View File

@ -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());
}
}

View File

@ -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());
}
}
}

View File

@ -99,6 +99,8 @@ public class HttpComponent4_2Factory extends HttpClientFactory {
clientBuilder.setHostnameVerifier(new CustomHostnameVerifier(
params.getHostnameVerifier()));
}
clientBuilder.setMaxConnTotal(params.getMaxConnections());
clientBuilder.setMaxConnPerRoute(params.getMaxConnectionsPerHost());
}
}

View File

@ -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());
}
}

View File

@ -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);
}
}

View File

@ -1,5 +1,6 @@
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
@ -15,7 +16,7 @@
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<artifactId>netty-codec-http</artifactId>
<version>4.1.42.Final</version>
</dependency>
<dependency>
@ -33,26 +34,10 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
<exclusions>
<exclusion>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
</exclusion>
</exclusions>
<version>4.2</version>
<scope>test</scope>
</dependency>
<dependency>
@ -60,6 +45,12 @@
<artifactId>spring-context</artifactId>
<version>4.2.0.RELEASE</version>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>

View File

@ -61,7 +61,7 @@ public final class ClassUtil {
}
}
if (clazz == null || clazz.isEmpty()) {
clazz = new ArrayList<>();
clazz = new ArrayList<Class<?>>();
try {
for (URL url : ((URLClassLoader) ClassLoader.getSystemClassLoader()).getURLs()) {
File file = new File(url.getFile());

View File

@ -27,7 +27,7 @@ import com.foxinmy.weixin4j.util.ServerToolkits;
*/
public class MessagePush {
private final String server = "https://localhost:30000";
private final String server = "http://localhost:30000";
private final HttpClient httpClient;
private final HttpPost httpPost;
private final HttpGet httpGet;