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 StringnormalizeEndpointAddress(String endpointAddress) Normalizes an endpoint address by removing a trailing slash, if present.static voidregisterEndpointLimiter(String endpointAddress, int limit) Registers a concurrency limiter for the given endpoint address.static voidreset()Resets provider state to its default configuration.static voidsetDefaultParallelRequestLimitPerServer(int limit) Sets the default limit on the number of concurrent requests per server.
-
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
-
setDefaultParallelRequestLimitPerServer
public static void setDefaultParallelRequestLimitPerServer(int limit) Sets the default limit on the number of concurrent requests per server. This value is used when creating new endpoint limiters via.invalid reference
#getOrCreateEndpointLimiter(String)- Parameters:
limit- maximum number of concurrent requests per server- Throws:
IllegalArgumentException- iflimitis 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 addresslimit- limit on the number of concurrent requests allowed for the endpoint address- Throws:
IllegalArgumentException- iflimitis non-positive.
-
normalizeEndpointAddress
-
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.
-