org.apache.hc.client5.http.impl.io

## Class PoolingHttpClientConnectionManager

- java.lang.Object
- - org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager

All Implemented Interfaces:
  Closeable, AutoCloseable, HttpClientConnectionManager, ModalCloseable, ConnPoolControl<HttpRoute>, ConnPoolStats<HttpRoute>

    @Contract(threading=SAFE_CONDITIONAL)
    public class PoolingHttpClientConnectionManager
    extends Object
    implements HttpClientConnectionManager, ConnPoolControl<HttpRoute>

`ClientConnectionPoolManager` maintains a pool of `ManagedHttpClientConnection`s and is able to service connection requests from multiple execution threads. Connections are pooled on a per route basis. A request for a route which already the manager has persistent connections for available in the pool will be serviced by leasing a connection from the pool rather than creating a new connection.

`ClientConnectionPoolManager` maintains a maximum limit of connection on a per route basis and in total. Connection limits, however, can be adjusted using `ConnPoolControl` methods.

Total time to live (TTL) set at construction time defines maximum life span of persistent connections regardless of their expiration setting. No persistent connection will be re-used past its TTL value.

The handling of stale connections was changed in version 4.4. Previously, the code would check every connection by default before re-using it. The code now only checks the connection if the elapsed time since the last use of the connection exceeds the timeout that has been set. The default timeout is set to 5000ms.

Since:
  4.3

### Field Summary

Fields
`static int` `DEFAULT_MAX_CONNECTIONS_PER_ROUTE`
`static int` `DEFAULT_MAX_TOTAL_CONNECTIONS`

### Constructor Summary

Constructors
`` `PoolingHttpClientConnectionManager()`
`protected` `PoolingHttpClientConnectionManager(HttpClientConnectionOperator httpClientConnectionOperator, ManagedConnPool<HttpRoute,ManagedHttpClientConnection> pool, HttpConnectionFactory<ManagedHttpClientConnection> connFactory)`
`protected` `PoolingHttpClientConnectionManager(HttpClientConnectionOperator httpClientConnectionOperator, PoolConcurrencyPolicy poolConcurrencyPolicy, PoolReusePolicy poolReusePolicy, TimeValue timeToLive, HttpConnectionFactory<ManagedHttpClientConnection> connFactory)`
`` `PoolingHttpClientConnectionManager(Registry<ConnectionSocketFactory> socketFactoryRegistry)`
`` `PoolingHttpClientConnectionManager(Registry<ConnectionSocketFactory> socketFactoryRegistry, HttpConnectionFactory<ManagedHttpClientConnection> connFactory)`
`` `PoolingHttpClientConnectionManager(Registry<ConnectionSocketFactory> socketFactoryRegistry, PoolConcurrencyPolicy poolConcurrencyPolicy, PoolReusePolicy poolReusePolicy, TimeValue timeToLive)`
`` `PoolingHttpClientConnectionManager(Registry<ConnectionSocketFactory> socketFactoryRegistry, PoolConcurrencyPolicy poolConcurrencyPolicy, PoolReusePolicy poolReusePolicy, TimeValue timeToLive, HttpConnectionFactory<ManagedHttpClientConnection> connFactory)`
`` `PoolingHttpClientConnectionManager(Registry<ConnectionSocketFactory> socketFactoryRegistry, PoolConcurrencyPolicy poolConcurrencyPolicy, PoolReusePolicy poolReusePolicy, TimeValue timeToLive, SchemePortResolver schemePortResolver, DnsResolver dnsResolver, HttpConnectionFactory<ManagedHttpClientConnection> connFactory)`
`` `PoolingHttpClientConnectionManager(Registry<ConnectionSocketFactory> socketFactoryRegistry, PoolConcurrencyPolicy poolConcurrencyPolicy, TimeValue timeToLive, HttpConnectionFactory<ManagedHttpClientConnection> connFactory)`

### Method Summary

Methods
`void` `close()`
`void` `close(CloseMode closeMode)`
`void` `closeExpired()`
`void` `closeIdle(TimeValue idleTime)`
`void` `connect(ConnectionEndpoint endpoint, TimeValue connectTimeout, HttpContext context)` Connects the endpoint to the initial hop (connection target in case of a direct route or to the first proxy hop in case of a route via a proxy or multiple proxies).
`int` `getDefaultMaxPerRoute()`
`SocketConfig` `getDefaultSocketConfig()`
`int` `getMaxPerRoute(HttpRoute route)`
`int` `getMaxTotal()`
`Set<HttpRoute>` `getRoutes()`
`PoolStats` `getStats(HttpRoute route)`
`PoolStats` `getTotalStats()`
`TimeValue` `getValidateAfterInactivity()`
`LeaseRequest` `lease(HttpRoute route, Object state)`
`LeaseRequest` `lease(HttpRoute route, Timeout requestTimeout, Object state)` Returns a `LeaseRequest` object which can be used to obtain a `ConnectionEndpoint` to cancel the request by calling `Cancellable.cancel()`.
`void` `release(ConnectionEndpoint endpoint, Object state, TimeValue keepAlive)` Releases the endpoint back to the manager making it potentially re-usable by other consumers.
`void` `setDefaultMaxPerRoute(int max)`
`void` `setDefaultSocketConfig(SocketConfig defaultSocketConfig)`
`void` `setMaxPerRoute(HttpRoute route, int max)`
`void` `setMaxTotal(int max)`
`void` `setValidateAfterInactivity(TimeValue validateAfterInactivity)` Defines period of inactivity in milliseconds after which persistent connections must be re-validated prior to being `lease(HttpRoute, Object)` leased} to the consumer.
`void` `upgrade(ConnectionEndpoint endpoint, HttpContext context)` Upgrades transport security of the given endpoint by using the TLS security protocol.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### DEFAULT_MAX_TOTAL_CONNECTIONS

    public static final int DEFAULT_MAX_TOTAL_CONNECTIONS

See Also:
  Constant Field Values

#### DEFAULT_MAX_CONNECTIONS_PER_ROUTE

    public static final int DEFAULT_MAX_CONNECTIONS_PER_ROUTE

See Also:
  Constant Field Values

### Constructor Detail

#### PoolingHttpClientConnectionManager

    public PoolingHttpClientConnectionManager()

#### PoolingHttpClientConnectionManager

    public PoolingHttpClientConnectionManager(Registry<ConnectionSocketFactory> socketFactoryRegistry)

#### PoolingHttpClientConnectionManager

    public PoolingHttpClientConnectionManager(Registry<ConnectionSocketFactory> socketFactoryRegistry,
                                      HttpConnectionFactory<ManagedHttpClientConnection> connFactory)

#### PoolingHttpClientConnectionManager

    public PoolingHttpClientConnectionManager(Registry<ConnectionSocketFactory> socketFactoryRegistry,
                                      PoolConcurrencyPolicy poolConcurrencyPolicy,
                                      TimeValue timeToLive,
                                      HttpConnectionFactory<ManagedHttpClientConnection> connFactory)

#### PoolingHttpClientConnectionManager

    public PoolingHttpClientConnectionManager(Registry<ConnectionSocketFactory> socketFactoryRegistry,
                                      PoolConcurrencyPolicy poolConcurrencyPolicy,
                                      PoolReusePolicy poolReusePolicy,
                                      TimeValue timeToLive)

#### PoolingHttpClientConnectionManager

    public PoolingHttpClientConnectionManager(Registry<ConnectionSocketFactory> socketFactoryRegistry,
                                      PoolConcurrencyPolicy poolConcurrencyPolicy,
                                      PoolReusePolicy poolReusePolicy,
                                      TimeValue timeToLive,
                                      HttpConnectionFactory<ManagedHttpClientConnection> connFactory)

#### PoolingHttpClientConnectionManager

    public PoolingHttpClientConnectionManager(Registry<ConnectionSocketFactory> socketFactoryRegistry,
                                      PoolConcurrencyPolicy poolConcurrencyPolicy,
                                      PoolReusePolicy poolReusePolicy,
                                      TimeValue timeToLive,
                                      SchemePortResolver schemePortResolver,
                                      DnsResolver dnsResolver,
                                      HttpConnectionFactory<ManagedHttpClientConnection> connFactory)

#### PoolingHttpClientConnectionManager

    @Internal
    protected PoolingHttpClientConnectionManager(HttpClientConnectionOperator httpClientConnectionOperator,
                                               PoolConcurrencyPolicy poolConcurrencyPolicy,
                                               PoolReusePolicy poolReusePolicy,
                                               TimeValue timeToLive,
                                               HttpConnectionFactory<ManagedHttpClientConnection> connFactory)

#### PoolingHttpClientConnectionManager

    @Internal
    protected PoolingHttpClientConnectionManager(HttpClientConnectionOperator httpClientConnectionOperator,
                                               ManagedConnPool<HttpRoute,ManagedHttpClientConnection> pool,
                                               HttpConnectionFactory<ManagedHttpClientConnection> connFactory)

### Method Detail

#### close

    public void close()

Specified by:
  `close` in interface `Closeable`
Specified by:
  `close` in interface `AutoCloseable`

#### close

    public void close(CloseMode closeMode)

Specified by:
  `close` in interface `ModalCloseable`

#### lease

    public LeaseRequest lease(HttpRoute route,
                     Object state)

#### lease

    public LeaseRequest lease(HttpRoute route,
                     Timeout requestTimeout,
                     Object state)

Description copied from interface: `HttpClientConnectionManager`
Returns a `LeaseRequest` object which can be used to obtain a `ConnectionEndpoint` to cancel the request by calling `Cancellable.cancel()`.

Please note that newly allocated endpoints can be leased `disconnected`. The consumer of the endpoint is responsible for fully establishing the route to the endpoint target by calling `HttpClientConnectionManager.connect(ConnectionEndpoint, TimeValue, HttpContext)` in order to connect directly to the target or to the first proxy hop, and optionally calling `HttpClientConnectionManager.upgrade(ConnectionEndpoint, HttpContext)` method to upgrade the underlying transport to Transport Layer Security after having executed a `CONNECT` method to all intermediate proxy hops.

Specified by:
  `lease` in interface `HttpClientConnectionManager`
Parameters:
  `route` - HTTP route of the requested connection.
  `requestTimeout` - lease request timeout.
  `state` - expected state of the connection or `null` if the connection is not expected to carry any state.

#### release

    public void release(ConnectionEndpoint endpoint,
               Object state,
               TimeValue keepAlive)

Description copied from interface: `HttpClientConnectionManager`
Releases the endpoint back to the manager making it potentially re-usable by other consumers. Optionally, the maximum period of how long the manager should keep the connection alive can be defined using `validDuration` and `timeUnit` parameters.

Specified by:
  `release` in interface `HttpClientConnectionManager`
Parameters:
  `endpoint` - the managed endpoint.
  `state` - the new connection state of `null` if state-less.
  `keepAlive` - the duration of time this connection is valid for reuse.

#### connect

    public void connect(ConnectionEndpoint endpoint,
               TimeValue connectTimeout,
               HttpContext context)
                 throws IOException

Description copied from interface: `HttpClientConnectionManager`
Connects the endpoint to the initial hop (connection target in case of a direct route or to the first proxy hop in case of a route via a proxy or multiple proxies).

Specified by:
  `connect` in interface `HttpClientConnectionManager`
Parameters:
  `endpoint` - the managed endpoint.
  `connectTimeout` - connect timeout.
  `context` - the actual HTTP context.
Throws:
  `IOException`

#### upgrade

    public void upgrade(ConnectionEndpoint endpoint,
               HttpContext context)
                 throws IOException

Description copied from interface: `HttpClientConnectionManager`
Upgrades transport security of the given endpoint by using the TLS security protocol.

Specified by:
  `upgrade` in interface `HttpClientConnectionManager`
Parameters:
  `endpoint` - the managed endpoint.
  `context` - the actual HTTP context.
Throws:
  `IOException`

#### closeIdle

    public void closeIdle(TimeValue idleTime)

Specified by:
  `closeIdle` in interface `ConnPoolControl<HttpRoute>`

#### closeExpired

    public void closeExpired()

Specified by:
  `closeExpired` in interface `ConnPoolControl<HttpRoute>`

#### getRoutes

    public Set<HttpRoute> getRoutes()

Specified by:
  `getRoutes` in interface `ConnPoolControl<HttpRoute>`

#### getMaxTotal

    public int getMaxTotal()

Specified by:
  `getMaxTotal` in interface `ConnPoolControl<HttpRoute>`

#### setMaxTotal

    public void setMaxTotal(int max)

Specified by:
  `setMaxTotal` in interface `ConnPoolControl<HttpRoute>`

#### getDefaultMaxPerRoute

    public int getDefaultMaxPerRoute()

Specified by:
  `getDefaultMaxPerRoute` in interface `ConnPoolControl<HttpRoute>`

#### setDefaultMaxPerRoute

    public void setDefaultMaxPerRoute(int max)

Specified by:
  `setDefaultMaxPerRoute` in interface `ConnPoolControl<HttpRoute>`

#### getMaxPerRoute

    public int getMaxPerRoute(HttpRoute route)

Specified by:
  `getMaxPerRoute` in interface `ConnPoolControl<HttpRoute>`

#### setMaxPerRoute

    public void setMaxPerRoute(HttpRoute route,
                      int max)

Specified by:
  `setMaxPerRoute` in interface `ConnPoolControl<HttpRoute>`

#### getTotalStats

    public PoolStats getTotalStats()

Specified by:
  `getTotalStats` in interface `ConnPoolStats<HttpRoute>`

#### getStats

    public PoolStats getStats(HttpRoute route)

Specified by:
  `getStats` in interface `ConnPoolStats<HttpRoute>`

#### getDefaultSocketConfig

    public SocketConfig getDefaultSocketConfig()

#### setDefaultSocketConfig

    public void setDefaultSocketConfig(SocketConfig defaultSocketConfig)

#### getValidateAfterInactivity

    public TimeValue getValidateAfterInactivity()

Since:
  4.4
See Also:
  `setValidateAfterInactivity(TimeValue)`

#### setValidateAfterInactivity

    public void setValidateAfterInactivity(TimeValue validateAfterInactivity)

Defines period of inactivity in milliseconds after which persistent connections must be re-validated prior to being `lease(HttpRoute, Object)` leased} to the consumer. Non-positive value passed to this method disables connection validation. This check helps detect connections that have become stale (half-closed) while kept inactive in the pool.

Since:
  4.4
See Also:
  `lease(HttpRoute, Object)`