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, org.apache.hc.core5.io.ModalCloseable, org.apache.hc.core5.pool.ConnPoolControl<HttpRoute>, org.apache.hc.core5.pool.ConnPoolStats<HttpRoute>

      @Contract(threading=SAFE_CONDITIONAL)
      public class PoolingHttpClientConnectionManager
      extends Object
      implements HttpClientConnectionManager, org.apache.hc.core5.pool.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.

  Since:
    4.3

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static int` | `DEFAULT_MAX_CONNECTIONS_PER_ROUTE`
    `static int` | `DEFAULT_MAX_TOTAL_CONNECTIONS`

  - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `` | `PoolingHttpClientConnectionManager()`
    `protected` | `PoolingHttpClientConnectionManager(HttpClientConnectionOperator httpClientConnectionOperator, org.apache.hc.core5.pool.ManagedConnPool<HttpRoute,ManagedHttpClientConnection> pool, org.apache.hc.core5.http.io.HttpConnectionFactory<ManagedHttpClientConnection> connFactory)`
    `` | `PoolingHttpClientConnectionManager(HttpClientConnectionOperator httpClientConnectionOperator, org.apache.hc.core5.pool.PoolConcurrencyPolicy poolConcurrencyPolicy, org.apache.hc.core5.pool.PoolReusePolicy poolReusePolicy, org.apache.hc.core5.util.TimeValue timeToLive, org.apache.hc.core5.http.io.HttpConnectionFactory<ManagedHttpClientConnection> connFactory)`
    `` | `PoolingHttpClientConnectionManager(org.apache.hc.core5.http.config.Registry<ConnectionSocketFactory> socketFactoryRegistry)` Deprecated. Use `PoolingHttpClientConnectionManagerBuilder`
    `` | `PoolingHttpClientConnectionManager(org.apache.hc.core5.http.config.Registry<ConnectionSocketFactory> socketFactoryRegistry, org.apache.hc.core5.http.io.HttpConnectionFactory<ManagedHttpClientConnection> connFactory)` Deprecated. Use `PoolingHttpClientConnectionManagerBuilder`
    `` | `PoolingHttpClientConnectionManager(org.apache.hc.core5.http.config.Registry<ConnectionSocketFactory> socketFactoryRegistry, org.apache.hc.core5.pool.PoolConcurrencyPolicy poolConcurrencyPolicy, org.apache.hc.core5.pool.PoolReusePolicy poolReusePolicy, org.apache.hc.core5.util.TimeValue timeToLive)` Deprecated. Use `PoolingHttpClientConnectionManagerBuilder`
    `` | `PoolingHttpClientConnectionManager(org.apache.hc.core5.http.config.Registry<ConnectionSocketFactory> socketFactoryRegistry, org.apache.hc.core5.pool.PoolConcurrencyPolicy poolConcurrencyPolicy, org.apache.hc.core5.pool.PoolReusePolicy poolReusePolicy, org.apache.hc.core5.util.TimeValue timeToLive, org.apache.hc.core5.http.io.HttpConnectionFactory<ManagedHttpClientConnection> connFactory)` Deprecated. Use `PoolingHttpClientConnectionManagerBuilder`
    `` | `PoolingHttpClientConnectionManager(org.apache.hc.core5.http.config.Registry<ConnectionSocketFactory> socketFactoryRegistry, org.apache.hc.core5.pool.PoolConcurrencyPolicy poolConcurrencyPolicy, org.apache.hc.core5.pool.PoolReusePolicy poolReusePolicy, org.apache.hc.core5.util.TimeValue timeToLive, SchemePortResolver schemePortResolver, DnsResolver dnsResolver, org.apache.hc.core5.http.io.HttpConnectionFactory<ManagedHttpClientConnection> connFactory)` Deprecated. Use `PoolingHttpClientConnectionManagerBuilder`
    `` | `PoolingHttpClientConnectionManager(org.apache.hc.core5.http.config.Registry<ConnectionSocketFactory> socketFactoryRegistry, org.apache.hc.core5.pool.PoolConcurrencyPolicy poolConcurrencyPolicy, org.apache.hc.core5.util.TimeValue timeToLive, org.apache.hc.core5.http.io.HttpConnectionFactory<ManagedHttpClientConnection> connFactory)` Deprecated. Use `PoolingHttpClientConnectionManagerBuilder`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods Deprecated Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `close()`
    `void` | `close(org.apache.hc.core5.io.CloseMode closeMode)`
    `void` | `closeExpired()`
    `void` | `closeIdle(org.apache.hc.core5.util.TimeValue idleTime)`
    `void` | `connect(ConnectionEndpoint endpoint, org.apache.hc.core5.util.TimeValue timeout, org.apache.hc.core5.http.protocol.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()`
    `org.apache.hc.core5.http.io.SocketConfig` | `getDefaultSocketConfig()` Deprecated. Use custom `setConnectionConfigResolver(Resolver)`
    `int` | `getMaxPerRoute(HttpRoute route)`
    `int` | `getMaxTotal()`
    `Set<HttpRoute>` | `getRoutes()`
    `org.apache.hc.core5.pool.PoolStats` | `getStats(HttpRoute route)`
    `org.apache.hc.core5.pool.PoolStats` | `getTotalStats()`
    `org.apache.hc.core5.util.TimeValue` | `getValidateAfterInactivity()` Deprecated. Use `setConnectionConfigResolver(Resolver)`.
    `boolean` | `isClosed()` Method that can be called to determine whether the connection manager has been shut down and is closed or not.
    `LeaseRequest` | `lease(String id, HttpRoute route, Object state)`
    `LeaseRequest` | `lease(String id, HttpRoute route, org.apache.hc.core5.util.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, org.apache.hc.core5.util.TimeValue keepAlive)` Releases the endpoint back to the manager making it potentially re-usable by other consumers.
    `void` | `setConnectionConfigResolver(org.apache.hc.core5.function.Resolver<HttpRoute,ConnectionConfig> connectionConfigResolver)` Sets `Resolver` of `ConnectionConfig` on a per route basis.
    `void` | `setDefaultConnectionConfig(ConnectionConfig config)` Sets the same `ConnectionConfig` for all routes
    `void` | `setDefaultMaxPerRoute(int max)`
    `void` | `setDefaultSocketConfig(org.apache.hc.core5.http.io.SocketConfig config)` Sets the same `SocketConfig` for all routes
    `void` | `setDefaultTlsConfig(TlsConfig config)` Sets the same `ConnectionConfig` for all hosts
    `void` | `setMaxPerRoute(HttpRoute route, int max)`
    `void` | `setMaxTotal(int max)`
    `void` | `setSocketConfigResolver(org.apache.hc.core5.function.Resolver<HttpRoute,org.apache.hc.core5.http.io.SocketConfig> socketConfigResolver)` Sets `Resolver` of `SocketConfig` on a per route basis.
    `void` | `setTlsConfigResolver(org.apache.hc.core5.function.Resolver<org.apache.hc.core5.http.HttpHost,TlsConfig> tlsConfigResolver)` Sets `Resolver` of `TlsConfig` on a per host basis.
    `void` | `setValidateAfterInactivity(org.apache.hc.core5.util.TimeValue validateAfterInactivity)` Deprecated. Use `setConnectionConfigResolver(Resolver)`.
    `void` | `upgrade(ConnectionEndpoint endpoint, org.apache.hc.core5.http.protocol.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

          @Deprecated
          public PoolingHttpClientConnectionManager(org.apache.hc.core5.http.config.Registry<ConnectionSocketFactory> socketFactoryRegistry)

      Deprecated. Use `PoolingHttpClientConnectionManagerBuilder`

    - #### PoolingHttpClientConnectionManager

          @Deprecated
          public PoolingHttpClientConnectionManager(org.apache.hc.core5.http.config.Registry<ConnectionSocketFactory> socketFactoryRegistry,
                                                                org.apache.hc.core5.http.io.HttpConnectionFactory<ManagedHttpClientConnection> connFactory)

      Deprecated. Use `PoolingHttpClientConnectionManagerBuilder`

    - #### PoolingHttpClientConnectionManager

          @Deprecated
          public PoolingHttpClientConnectionManager(org.apache.hc.core5.http.config.Registry<ConnectionSocketFactory> socketFactoryRegistry,
                                                                org.apache.hc.core5.pool.PoolConcurrencyPolicy poolConcurrencyPolicy,
                                                                org.apache.hc.core5.util.TimeValue timeToLive,
                                                                org.apache.hc.core5.http.io.HttpConnectionFactory<ManagedHttpClientConnection> connFactory)

      Deprecated. Use `PoolingHttpClientConnectionManagerBuilder`

    - #### PoolingHttpClientConnectionManager

          @Deprecated
          public PoolingHttpClientConnectionManager(org.apache.hc.core5.http.config.Registry<ConnectionSocketFactory> socketFactoryRegistry,
                                                                org.apache.hc.core5.pool.PoolConcurrencyPolicy poolConcurrencyPolicy,
                                                                org.apache.hc.core5.pool.PoolReusePolicy poolReusePolicy,
                                                                org.apache.hc.core5.util.TimeValue timeToLive)

      Deprecated. Use `PoolingHttpClientConnectionManagerBuilder`

    - #### PoolingHttpClientConnectionManager

          @Deprecated
          public PoolingHttpClientConnectionManager(org.apache.hc.core5.http.config.Registry<ConnectionSocketFactory> socketFactoryRegistry,
                                                                org.apache.hc.core5.pool.PoolConcurrencyPolicy poolConcurrencyPolicy,
                                                                org.apache.hc.core5.pool.PoolReusePolicy poolReusePolicy,
                                                                org.apache.hc.core5.util.TimeValue timeToLive,
                                                                org.apache.hc.core5.http.io.HttpConnectionFactory<ManagedHttpClientConnection> connFactory)

      Deprecated. Use `PoolingHttpClientConnectionManagerBuilder`

    - #### PoolingHttpClientConnectionManager

          @Deprecated
          public PoolingHttpClientConnectionManager(org.apache.hc.core5.http.config.Registry<ConnectionSocketFactory> socketFactoryRegistry,
                                                                org.apache.hc.core5.pool.PoolConcurrencyPolicy poolConcurrencyPolicy,
                                                                org.apache.hc.core5.pool.PoolReusePolicy poolReusePolicy,
                                                                org.apache.hc.core5.util.TimeValue timeToLive,
                                                                SchemePortResolver schemePortResolver,
                                                                DnsResolver dnsResolver,
                                                                org.apache.hc.core5.http.io.HttpConnectionFactory<ManagedHttpClientConnection> connFactory)

      Deprecated. Use `PoolingHttpClientConnectionManagerBuilder`

    - #### PoolingHttpClientConnectionManager

          @Internal
          public PoolingHttpClientConnectionManager(HttpClientConnectionOperator httpClientConnectionOperator,
                                                              org.apache.hc.core5.pool.PoolConcurrencyPolicy poolConcurrencyPolicy,
                                                              org.apache.hc.core5.pool.PoolReusePolicy poolReusePolicy,
                                                              org.apache.hc.core5.util.TimeValue timeToLive,
                                                              org.apache.hc.core5.http.io.HttpConnectionFactory<ManagedHttpClientConnection> connFactory)

    - #### PoolingHttpClientConnectionManager

          @Internal
          protected PoolingHttpClientConnectionManager(HttpClientConnectionOperator httpClientConnectionOperator,
                                                                 org.apache.hc.core5.pool.ManagedConnPool<HttpRoute,ManagedHttpClientConnection> pool,
                                                                 org.apache.hc.core5.http.io.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(org.apache.hc.core5.io.CloseMode closeMode)

      Specified by:
        `close` in interface `org.apache.hc.core5.io.ModalCloseable`

    - #### lease

          public LeaseRequest lease(String id,
                                    HttpRoute route,
                                    Object state)

    - #### lease

          public LeaseRequest lease(String id,
                                    HttpRoute route,
                                    org.apache.hc.core5.util.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:
        `id` - unique operation ID or `null`.
        `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,
                              org.apache.hc.core5.util.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,
                              org.apache.hc.core5.util.TimeValue timeout,
                              org.apache.hc.core5.http.protocol.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.
        `timeout` - connect timeout.
        `context` - the actual HTTP context.
      Throws:
        `IOException`

    - #### upgrade

          public void upgrade(ConnectionEndpoint endpoint,
                              org.apache.hc.core5.http.protocol.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(org.apache.hc.core5.util.TimeValue idleTime)

      Specified by:
        `closeIdle` in interface `org.apache.hc.core5.pool.ConnPoolControl<HttpRoute>`

    - #### closeExpired

          public void closeExpired()

      Specified by:
        `closeExpired` in interface `org.apache.hc.core5.pool.ConnPoolControl<HttpRoute>`

    - #### getRoutes

          public Set<HttpRoute> getRoutes()

      Specified by:
        `getRoutes` in interface `org.apache.hc.core5.pool.ConnPoolControl<HttpRoute>`

    - #### getMaxTotal

          public int getMaxTotal()

      Specified by:
        `getMaxTotal` in interface `org.apache.hc.core5.pool.ConnPoolControl<HttpRoute>`

    - #### setMaxTotal

          public void setMaxTotal(int max)

      Specified by:
        `setMaxTotal` in interface `org.apache.hc.core5.pool.ConnPoolControl<HttpRoute>`

    - #### getDefaultMaxPerRoute

          public int getDefaultMaxPerRoute()

      Specified by:
        `getDefaultMaxPerRoute` in interface `org.apache.hc.core5.pool.ConnPoolControl<HttpRoute>`

    - #### setDefaultMaxPerRoute

          public void setDefaultMaxPerRoute(int max)

      Specified by:
        `setDefaultMaxPerRoute` in interface `org.apache.hc.core5.pool.ConnPoolControl<HttpRoute>`

    - #### getMaxPerRoute

          public int getMaxPerRoute(HttpRoute route)

      Specified by:
        `getMaxPerRoute` in interface `org.apache.hc.core5.pool.ConnPoolControl<HttpRoute>`

    - #### setMaxPerRoute

          public void setMaxPerRoute(HttpRoute route,
                                     int max)

      Specified by:
        `setMaxPerRoute` in interface `org.apache.hc.core5.pool.ConnPoolControl<HttpRoute>`

    - #### getTotalStats

          public org.apache.hc.core5.pool.PoolStats getTotalStats()

      Specified by:
        `getTotalStats` in interface `org.apache.hc.core5.pool.ConnPoolStats<HttpRoute>`

    - #### getStats

          public org.apache.hc.core5.pool.PoolStats getStats(HttpRoute route)

      Specified by:
        `getStats` in interface `org.apache.hc.core5.pool.ConnPoolStats<HttpRoute>`

    - #### setDefaultSocketConfig

          public void setDefaultSocketConfig(org.apache.hc.core5.http.io.SocketConfig config)

      Sets the same `SocketConfig` for all routes

    - #### setSocketConfigResolver

          public void setSocketConfigResolver(org.apache.hc.core5.function.Resolver<HttpRoute,org.apache.hc.core5.http.io.SocketConfig> socketConfigResolver)

      Sets `Resolver` of `SocketConfig` on a per route basis.
      Since:
        5.2

    - #### setDefaultConnectionConfig

          public void setDefaultConnectionConfig(ConnectionConfig config)

      Sets the same `ConnectionConfig` for all routes
      Since:
        5.2

    - #### setConnectionConfigResolver

          public void setConnectionConfigResolver(org.apache.hc.core5.function.Resolver<HttpRoute,ConnectionConfig> connectionConfigResolver)

      Sets `Resolver` of `ConnectionConfig` on a per route basis.
      Since:
        5.2

    - #### setDefaultTlsConfig

          public void setDefaultTlsConfig(TlsConfig config)

      Sets the same `ConnectionConfig` for all hosts
      Since:
        5.2

    - #### setTlsConfigResolver

          public void setTlsConfigResolver(org.apache.hc.core5.function.Resolver<org.apache.hc.core5.http.HttpHost,TlsConfig> tlsConfigResolver)

      Sets `Resolver` of `TlsConfig` on a per host basis.
      Since:
        5.2

    - #### getDefaultSocketConfig

          @Deprecated
          public org.apache.hc.core5.http.io.SocketConfig getDefaultSocketConfig()

      Deprecated. Use custom `setConnectionConfigResolver(Resolver)`

    - #### getValidateAfterInactivity

          @Deprecated
          public org.apache.hc.core5.util.TimeValue getValidateAfterInactivity()

      Deprecated. Use `setConnectionConfigResolver(Resolver)`.
      Since:
        4.4

    - #### setValidateAfterInactivity

          @Deprecated
          public void setValidateAfterInactivity(org.apache.hc.core5.util.TimeValue validateAfterInactivity)

      Deprecated. Use `setConnectionConfigResolver(Resolver)`.
      Defines period of inactivity after which persistent connections must be re-validated prior to being `lease(String, HttpRoute, Object)` leased} to the consumer. Negative values passed to this method disable connection validation. This check helps detect connections that have become stale (half-closed) while kept inactive in the pool.
      Since:
        4.4

    - #### isClosed

          public boolean isClosed()

      Method that can be called to determine whether the connection manager has been shut down and is closed or not.
      Returns:
        `true` if the connection manager has been shut down and is closed, otherwise return `false`.
      Since:
        5.4

Copyright © 1999–2021 The Apache Software Foundation. All rights reserved.