HTTP connection pooling using HttpClient

后端 未结 7 985
南笙
南笙 2020-12-02 18:44
  • 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?
相关标签:
7条回答
  • 2020-12-02 19:45

    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.

    0 讨论(0)
提交回复
热议问题