HTTP connection pooling using HttpClient

2019-01-11 01:10发布

  • How can I create a pool of connections using HttpClient?
  • I have to make frequent connections to the same server. Is it worth creating such a pool?
  • Is it possible to keep live connections and use it for various requests, and if yes how can I do so?

I am developing in Java, using Apache HTTP Client.

7条回答
别忘想泡老子
2楼-- · 2019-01-11 01:43

For HttpClient 4x:

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.

http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html

查看更多
Ridiculous、
3楼-- · 2019-01-11 01:52

HttpClient has already have a connection pool.So you do not need to create it. Just use it.

查看更多
叼着烟拽天下
4楼-- · 2019-01-11 01:54

PoolingClientConnectionManager is Deprecated now . from (4.3 version) use PoolingHttpClientConnectionManager.

查看更多
▲ chillily
5楼-- · 2019-01-11 01:59

ThreadSafeClientConnManager is deprecated now, use PoolingClientConnectionManager instead.

查看更多
手持菜刀,她持情操
6楼-- · 2019-01-11 02:01

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.

查看更多
欢心
7楼-- · 2019-01-11 02:07

[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.

查看更多
登录 后发表回答