我一直在尝试使用 Apache HTTPClient (v4.1) 为我的应用程序实现连接池。问题是客户端在运行时总是只建立两个连接,尽管有足够多的线程并行运行。我一直在尝试修改代码一段时间,但没有任何帮助。
我正在使用ThreadSafeClientConnManager
连接池并将 and 设置为MaxTotal
我DefaulMaxPerRoute
想要的值。
你有什么首先想到的我可能想要检查的吗?
这是我用来创建客户端的代码段。
DefaultHttpClient createClient() {
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, new Integer(60000));
params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, new Integer(60000));
params.setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("https", sf, 6443));
registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, registry);
cm.setMaxTotal(2 * maxConnections);
cm.setDefaultMaxPerRoute(maxConnections);
HttpHost localhost = new HttpHost("localhost");
cm.setMaxForRoute(new HttpRoute(localhost), maxConnections);
HttpHost sdpTargetHost = new HttpHost("webserviceIP", webservicePort, "https");
cm.setMaxForRoute(new HttpRoute(sdpTargetHost, null, true), maxConnections);
return new DefaultHttpClient(cm, params);
}
此函数返回的客户Runnables
端由ThreadPoolExecutor
. Runnables 使用客户端,并具有以下几行:
HttpResponse response = httpClient.execute(httpPost, context);
HttpEntity entity = response.getEntity();
....
EntityUtils.consume(entity);
据我所知,EntityUtils.consume(entity)
将通知连接管理器该连接不再使用,因此将释放该连接以供其他线程使用。所以我猜连接管理没问题。
我想我已经提供了足够的信息,请告诉我是否要添加更多信息。
谢谢