在多线程环境中使用HttpClient的最佳实践

Che*_*eng 82 java apache-commons-httpclient

有一段时间,我一直在多线程环境中使用HttpClient.对于每个线程,当它启动连接时,它将创建一个全新的HttpClient实例.

最近,我发现,通过使用这种方法,它可能导致用户打开太多端口,并且大多数连接处于TIME_WAIT状态.

http://www.opensubscriber.com/message/commons-httpclient-dev@jakarta.apache.org/86045.html

因此,而不是每个线程做:

HttpClient c = new HttpClient();
try {
    c.executeMethod(method);
}
catch(...) {
}
finally {
    method.releaseConnection();
}
Run Code Online (Sandbox Code Playgroud)

我们计划:

[方法A]

// global_c is initialized once through
// HttpClient global_c = new HttpClient(new MultiThreadedHttpConnectionManager());

try {
    global_c.executeMethod(method);
}
catch(...) {
}
finally {
    method.releaseConnection();
}
Run Code Online (Sandbox Code Playgroud)

在正常情况下,global_c将同时由50个++线程访问.我想知道,这会产生任何性能问题吗?MultiThreadedHttpConnectionManager是否使用无锁机制来实现其线程安全策略?

如果10个线程正在使用global_c,那么其他40个线程是否会被锁定?

或者,如果在每个线程中我创建一个HttpClient实例,但是显式释放连接管理器会更好吗?

[方法B]

MultiThreadedHttpConnectionManager connman = new MultiThreadedHttpConnectionManager();
HttpClient c = new HttpClient(connman);
try {
      c.executeMethod(method);
}
catch(...) {
}
finally {
    method.releaseConnection();
    connman.shutdown();
}
Run Code Online (Sandbox Code Playgroud)

connman.shutdown()会遇到性能问题吗?

对于使用50 ++线程的应用程序,我可以知道哪种方法(A或B)更好吗?

小智 43

绝对是方法A因为它的汇集和线程安全.

如果您使用的是httpclient 4.x,则连接管理器称为ThreadSafeClientConnManager.有关详细信息,请参阅此链接(向下滚动到"池化连接管理器").例如:

    HttpParams params = new BasicHttpParams();
    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    ClientConnectionManager cm = new ThreadSafeClientConnManager(params, registry);
    HttpClient client = new DefaultHttpClient(cm, params);
Run Code Online (Sandbox Code Playgroud)

  • [ThreadSafeClientConnManager](http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/conn/tsccm/ThreadSafeClientConnManager.html)已被弃用,转而使用[PoolingClientConnManager]( 4.2中的http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/conn/PoolingClientConnectionManager.html) (47认同)
  • 4.3.1此处:不推荐使用PoolingClientConnManager,而使用PoolingHttpClientConnectionManager. (16认同)

Che*_*eng 17

方法A由httpclient开发人员社区推荐.

有关详细信息,请参阅http://www.mail-archive.com/httpclient-users@hc.apache.org/msg02455.html.


djn*_*jna 13

我对文档的阅读是HttpConnection本身不被视为线程安全,因此MultiThreadedHttpConnectionManager提供了一个可重用的HttpConnections池,你有一个由所有线程共享的单个MultiThreadedHttpConnectionManager并初始化一次.因此,您需要对选项A进行一些小改进.

MultiThreadedHttpConnectionManager connman = new MultiThreadedHttpConnectionManag
Run Code Online (Sandbox Code Playgroud)

然后每个线程应该为每个请求使用序列,从池中获取一个连接并在完成其工作时将其放回 - 使用finally块可能是好的.您还应该编写池没有可用连接并处理超时异常的可能性.

HttpConnection connection = null
try {
    connection = connman.getConnectionWithTimeout(
                        HostConfiguration hostConfiguration, long timeout) 
    // work
} catch (/*etc*/) {/*etc*/} finally{
    if ( connection != null )
        connman.releaseConnection(connection);
}
Run Code Online (Sandbox Code Playgroud)

当您使用连接池时,您实际上不会关闭连接,因此不应该遇到TIME_WAIT问题.这种方法确实可以确保每个线程不会长时间挂起连接.请注意,conman本身是开放的.

  • 答案是两者都不正确.我会说出来的. (5认同)

Tho*_*hle 5

我想你会想要使用ThreadSafeClientConnManager.

你可以在这里看到它是如何工作的:http://foo.jasonhudgins.com/2009/08/http-connection-reuse-in-android.html

或者在AndroidHttpClient内部使用它.

  • 当然,就好像有人来​​这里谷歌搜索答案:) (9认同)

Dim*_* II 5

使用 HttpClient 4.5,您可以执行以下操作:

CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(new PoolingHttpClientConnectionManager()).build();
Run Code Online (Sandbox Code Playgroud)

请注意,这个实现了 Closeable(用于关闭连接管理器)。