I have spent recent days working on this so just want to share some "everyone-known" knowledges with you.
First, as you are dealing with the same server, it is recommended to use a single HTTP client to execute your requests. With the help of PoolingHttpClientConnectionManager, your client can be used to execute multiple requests concurrently. The official example of multithreaded request execution can be found here.
Secondly, HTTP/1.1 (and enhanced versions of HTTP/1.0) allows HTTP clients to keep the connections open after transactions complete so that it can be reused for future requests. This is often refered as Persistent Connection.
Also for the purpose of reusing client for multiple requests, the response header from a server often include an attribute call Keep-Alive that contain the time current connection will be kept alive. Besides that, Apache Http Client also provides you an interface ConnectionKeepAliveStrategyto customize your own policy for reusing connection.
Use a ThreadSafeClientConnManager. Pass a single global instance to the constructor of every HttpClient instance. I don't think there's any point in pooling the HttpClients themselves.
ThreadSafeClientConnManager ... manages a pool of client
connections and is able to service connection requests from multiple execution threads.
Connections are pooled on a per route basis. A request for a route for which the manager already
has a persistent connection available in the pool will be serviced by leasing a connection from
the pool rather than creating a brand new connection.
发布评论
评论(7)
我最近几天一直在研究这个问题,所以只想与您分享一些“众所周知”的知识。
首先,由于您正在处理同一台服务器,因此建议使用单个 HTTP 客户端来执行您的请求。在 PoolingHttpClientConnectionManager 的帮助下,您的客户端可用于同时执行多个请求。官方多线程请求执行的例子可以参见
其次,HTTP/1.1(以及 HTTP/1.0 的增强版本)允许 HTTP 客户端在事务完成后保持连接打开,以便可以在将来的请求中重用。这通常称为持久连接。
另外,为了重复使用客户端进行多个请求,来自服务器的响应标头通常包含一个属性调用
Keep-Alive
,其中包含当前连接保持活动状态的时间。除此之外,Apache Http Client 还为您提供了一个接口ConnectionKeepAliveStrategy
来自定义您自己的重用连接策略。I have spent recent days working on this so just want to share some "everyone-known" knowledges with you.
First, as you are dealing with the same server, it is recommended to use a single HTTP client to execute your requests. With the help of
PoolingHttpClientConnectionManager
, your client can be used to execute multiple requests concurrently. The official example of multithreaded request execution can be found here.Secondly, HTTP/1.1 (and enhanced versions of HTTP/1.0) allows HTTP clients to keep the connections open after transactions complete so that it can be reused for future requests. This is often refered as Persistent Connection.
Also for the purpose of reusing client for multiple requests, the response header from a server often include an attribute call
Keep-Alive
that contain the time current connection will be kept alive. Besides that, Apache Http Client also provides you an interfaceConnectionKeepAliveStrategy
to customize your own policy for reusing connection.PoolingClientConnectionManager
现已弃用。从(4.3 版本)开始使用PoolingHttpClientConnectionManager。PoolingClientConnectionManager
is Deprecated now . from (4.3 version) usePoolingHttpClientConnectionManager
.[假设 Java 和 Apache 的 HttpClient]
使用 ThreadSafeClientConnManager。将单个全局实例传递给每个 HttpClient 实例的构造函数。我认为汇集 HttpClient 本身没有任何意义。
[assuming Java, and Apache's HttpClient]
Use a ThreadSafeClientConnManager. Pass a single global instance to the constructor of every HttpClient instance. I don't think there's any point in pooling the HttpClients themselves.
ThreadSafeClientConnManager 现已弃用,请使用 PoolingClientConnectionManager 相反。
ThreadSafeClientConnManager is deprecated now, use PoolingClientConnectionManager instead.
对于 HttpClient 4x:
http://hc.apache.org/httpcomponents-client-ga /tutorial/html/connmgmt.html
For HttpClient 4x:
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html
这是不需要身份验证的 Apache HttpClient 4.3 连接池的示例:
This is an example of an Apache HttpClient 4.3 pool of connections which do not require authentication:
HttpClient已经有一个连接池。所以你不需要创建它。只需使用它即可。
HttpClient has already have a connection pool.So you do not need to create it. Just use it.