Class HttpClientProvider
HttpClient instances.
Each HttpClient instance manages its own internal connection pool. A
single client handles requests to many different hosts while reusing
connections where possible.
The connectTimeout is a client-level configuration and cannot be
changed after a client has been created. To support different
connection-timeout values, this provider caches one client instance per
timeout. Each such client manages its own independent set of pooled
connections for all routes it is used with.
Returned HttpClient instances are backed by an internal
HttpClientProvider.LimitedHttpClient wrapper to enforce concurrency limits using
Semaphores. Applicable concurrency limits are applied based on
hierarchical lookup by trimming path segments from the request URI.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final Map<Long, HttpClient> One limiter per endpoint address. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic HttpClientclient()Returns a shared client with no configured connect timeout.static HttpClientclient(long connectTimeout) Returns a shared client for a given connect timeout.static voidregisterEndpointLimiter(String endpointAddress, int maxParallelRequests) Registers a concurrency limiter for the given endpoint address.static voidreset()Resets provider state to its default configuration.static voidsetDefaultMaxParallelRequests(int maxParallelRequests) Sets the default maximum number of concurrent requests per endpoint address.
-
Field Details
-
LIMITERS_BY_ENDPOINT_ADDRESS
-
CLIENT_BY_CONNECT_TIMEOUT
-
-
Constructor Details
-
HttpClientProvider
public HttpClientProvider()
-
-
Method Details
-
client
Returns a shared client with no configured connect timeout.- Returns:
- a shared client instance
-
client
Returns a shared client for a given connect timeout. A separate client instance is cached for each effective timeout value, where non-positive values are treated as "no connect timeout".- Parameters:
connectTimeout- connect timeout in milliseconds, or non-positive value for no timeout- Returns:
- shared client instance for the given timeout configuration
-
setDefaultMaxParallelRequests
public static void setDefaultMaxParallelRequests(int maxParallelRequests) Sets the default maximum number of concurrent requests per endpoint address. This value is used when creating new endpoint limiters via.invalid reference
#getOrCreateEndpointLimiter(String)- Parameters:
maxParallelRequests- maximum number of concurrent requests per endpoint address- Throws:
IllegalArgumentException- ifmaxParallelRequestsis non-positive
-
registerEndpointLimiter
Registers a concurrency limiter for the given endpoint address. If a limiter is already registered for the endpoint address, it is replaced.- Parameters:
endpointAddress- endpoint addressmaxParallelRequests- maximum number of concurrent requests allowed for the endpoint address- Throws:
IllegalArgumentException- if maxParallelRequests is non-positive.
-
reset
public static void reset()Resets provider state to its default configuration.This clears all cached clients and registered endpoint address limiters, and restores the default maximum number of parallel requests.
This method is primarily intended for test isolation.
-