Reputation: 9450
Lets consider one of the latest (4.2.x) releases of HttpComponents.
Please explain how generally an instance of HttpClient
is related to instance of HttpConnection
(such as ManagedClientConnectionImpl
) and also ClientConnectionManager
.
Who knows about who ?
I will refer to them below ommiting [Http] prefix. So, HttpClient
-> Client
I understand that Client
keeps reference to ClientConnectionManager
.
Does that mean that Client
also has reference to underlying Connection
object ?
Does ConnectionManager
knows about all Clients
linked to it ?
How HttpGet
, which is used by HttpClient to perform execute
, knows about stream, exposed thru response's Entity
? (which it does, because we can close the stream by calling .abort()
on HttpGet
object.) Is HttpGet
instance also linked to Connection
object ?
I am confused and would appreciate detailed overview answering the questions above. To give examples, you could use any specific implementations, such as DefaultHttpClient
, ManagedClientConnectionImpl
, BasicClientConnectionManager
, if it would make understanding easier.
Upvotes: 1
Views: 1424
Reputation: 27558
HttpClient
is a facade to the request execution pipeline. The exact composition of the execution pipeline depends on the configuration of the HttpClient
instance. Think of it as a browser with multiple tabs.
One of more HttpClient
instances can depend on one ClientConnectionManager
sharing the underlying pool of persistent connections. ClientConnectionManager
instances have no knowledge of HttpClient
instances that depend upon them. They just lease connections to whatever entity requests them.
Each request is executed within a particular HttpContext
. HttpContext
instance is a collection of all stateful objects involved in execution of the actual HTTP exchange. HttpContext
instances are not thread-safe and may not be shared by multiple worker threads.
When initiating an HTTP exchange HttpClient
leases a ManagedClientConnection
instance from the ClientConnectionManager
it is associated with. It serializes and transmits the request message to the opposite endpoint, receives a response head, and binds the underlying connection to the response HttpEntity
instance (when available) without reading the response content. This enables the consumer of HttpResponse instance stream the response content without intermediate in-memory buffering. This also makes it necessary for the consumer to ensure proper release of resources (in particular ManagedClientConnection
) associated with the response.
Upvotes: 4