org.apache.hc.client5.http.impl.nio

## Class PoolingAsyncClientConnectionManager

- java.lang.Object
- - org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager

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

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

`PoolingAsyncClientConnectionManager` maintains a pool of non-blocking `HttpConnection`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 services by leasing a connection from the pool rather than creating a new connection.

`PoolingAsyncClientConnectionManager` maintains a maximum limit of connection on a per route basis and in total. Connection limits 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.

Since:
  5.0

### Field Summary

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

### Constructor Summary

Constructors
`` `PoolingAsyncClientConnectionManager()`
`protected` `PoolingAsyncClientConnectionManager(AsyncClientConnectionOperator connectionOperator, PoolConcurrencyPolicy poolConcurrencyPolicy, PoolReusePolicy poolReusePolicy, TimeValue timeToLive)`
`` `PoolingAsyncClientConnectionManager(Lookup<TlsStrategy> tlsStrategyLookup)`
`` `PoolingAsyncClientConnectionManager(Lookup<TlsStrategy> tlsStrategyLookup, PoolConcurrencyPolicy poolConcurrencyPolicy, PoolReusePolicy poolReusePolicy, TimeValue timeToLive)`
`` `PoolingAsyncClientConnectionManager(Lookup<TlsStrategy> tlsStrategyLookup, PoolConcurrencyPolicy poolConcurrencyPolicy, PoolReusePolicy poolReusePolicy, TimeValue timeToLive, SchemePortResolver schemePortResolver, DnsResolver dnsResolver)`
`` `PoolingAsyncClientConnectionManager(Lookup<TlsStrategy> tlsStrategyLookup, PoolConcurrencyPolicy poolConcurrencyPolicy, TimeValue timeToLive)`
`protected` `PoolingAsyncClientConnectionManager(ManagedConnPool<HttpRoute,ManagedAsyncClientConnection> pool, AsyncClientConnectionOperator connectionOperator)`

### Method Summary

Methods
`void` `close()`
`void` `close(CloseMode closeMode)`
`void` `closeExpired()`
`void` `closeIdle(TimeValue idletime)`
`Future<AsyncConnectionEndpoint>` `connect(AsyncConnectionEndpoint endpoint, ConnectionInitiator connectionInitiator, Timeout timeout, Object attachment, HttpContext context, FutureCallback<AsyncConnectionEndpoint> callback)` 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()`
`int` `getMaxPerRoute(HttpRoute route)`
`int` `getMaxTotal()`
`Set<HttpRoute>` `getRoutes()`
`PoolStats` `getStats(HttpRoute route)`
`PoolStats` `getTotalStats()`
`TimeValue` `getValidateAfterInactivity()`
`Future<AsyncConnectionEndpoint>` `lease(HttpRoute route, Object state, Timeout requestTimeout, FutureCallback<AsyncConnectionEndpoint> callback)` Returns a `Future` object which can be used to obtain an `AsyncConnectionEndpoint` or to cancel the request by calling `Future.cancel(boolean)`.
`void` `release(AsyncConnectionEndpoint 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` `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, Timeout, FutureCallback)` leased} to the consumer.
`void` `upgrade(AsyncConnectionEndpoint endpoint, Object attachment, 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

#### PoolingAsyncClientConnectionManager

    public PoolingAsyncClientConnectionManager()

#### PoolingAsyncClientConnectionManager

    public PoolingAsyncClientConnectionManager(Lookup<TlsStrategy> tlsStrategyLookup)

#### PoolingAsyncClientConnectionManager

    public PoolingAsyncClientConnectionManager(Lookup<TlsStrategy> tlsStrategyLookup,
                                       PoolConcurrencyPolicy poolConcurrencyPolicy,
                                       TimeValue timeToLive)

#### PoolingAsyncClientConnectionManager

    public PoolingAsyncClientConnectionManager(Lookup<TlsStrategy> tlsStrategyLookup,
                                       PoolConcurrencyPolicy poolConcurrencyPolicy,
                                       PoolReusePolicy poolReusePolicy,
                                       TimeValue timeToLive)

#### PoolingAsyncClientConnectionManager

    public PoolingAsyncClientConnectionManager(Lookup<TlsStrategy> tlsStrategyLookup,
                                       PoolConcurrencyPolicy poolConcurrencyPolicy,
                                       PoolReusePolicy poolReusePolicy,
                                       TimeValue timeToLive,
                                       SchemePortResolver schemePortResolver,
                                       DnsResolver dnsResolver)

#### PoolingAsyncClientConnectionManager

    @Internal
    protected PoolingAsyncClientConnectionManager(AsyncClientConnectionOperator connectionOperator,
                                                PoolConcurrencyPolicy poolConcurrencyPolicy,
                                                PoolReusePolicy poolReusePolicy,
                                                TimeValue timeToLive)

#### PoolingAsyncClientConnectionManager

    @Internal
    protected PoolingAsyncClientConnectionManager(ManagedConnPool<HttpRoute,ManagedAsyncClientConnection> pool,
                                                AsyncClientConnectionOperator connectionOperator)

### 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 Future<AsyncConnectionEndpoint> lease(HttpRoute route,
                                        Object state,
                                        Timeout requestTimeout,
                                        FutureCallback<AsyncConnectionEndpoint> callback)

Description copied from interface: `AsyncClientConnectionManager`
Returns a `Future` object which can be used to obtain an `AsyncConnectionEndpoint` or to cancel the request by calling `Future.cancel(boolean)`.

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 `AsyncClientConnectionManager.connect(AsyncConnectionEndpoint, ConnectionInitiator, Timeout, Object, HttpContext, FutureCallback)` in order to connect directly to the target or to the first proxy hop, and optionally calling `AsyncClientConnectionManager.upgrade(AsyncConnectionEndpoint, Object, 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 `AsyncClientConnectionManager`
Parameters:
  `route` - HTTP route of the requested connection.
  `state` - expected state of the connection or `null` if the connection is not expected to carry any state.
  `requestTimeout` - lease request timeout.
  `callback` - result callback.

#### release

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

Description copied from interface: `AsyncClientConnectionManager`
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 `AsyncClientConnectionManager`
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 Future<AsyncConnectionEndpoint> connect(AsyncConnectionEndpoint endpoint,
                                          ConnectionInitiator connectionInitiator,
                                          Timeout timeout,
                                          Object attachment,
                                          HttpContext context,
                                          FutureCallback<AsyncConnectionEndpoint> callback)

Description copied from interface: `AsyncClientConnectionManager`
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 `AsyncClientConnectionManager`
Parameters:
  `endpoint` - the managed endpoint.
  `timeout` - connect timeout.
  `attachment` - connect request attachment.
  `context` - the actual HTTP context.
  `callback` - result callback.

#### upgrade

    public void upgrade(AsyncConnectionEndpoint endpoint,
               Object attachment,
               HttpContext context)

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

Specified by:
  `upgrade` in interface `AsyncClientConnectionManager`
Parameters:
  `endpoint` - the managed endpoint.
  `attachment` - the attachment the upgrade attachment object.
  `context` - the actual HTTP context.

#### getRoutes

    public Set<HttpRoute> getRoutes()

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

#### setMaxTotal

    public void setMaxTotal(int max)

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

#### getMaxTotal

    public int getMaxTotal()

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

#### setDefaultMaxPerRoute

    public void setDefaultMaxPerRoute(int max)

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

#### getDefaultMaxPerRoute

    public int getDefaultMaxPerRoute()

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

#### setMaxPerRoute

    public void setMaxPerRoute(HttpRoute route,
                      int max)

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

#### getMaxPerRoute

    public int getMaxPerRoute(HttpRoute route)

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

#### closeIdle

    public void closeIdle(TimeValue idletime)

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

#### closeExpired

    public void closeExpired()

Specified by:
  `closeExpired` 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>`

#### getValidateAfterInactivity

    public TimeValue getValidateAfterInactivity()

#### 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, Timeout, FutureCallback)` 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.