Class PoolingAsyncClientConnectionManager
- java.lang.Object
-
- org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,AsyncClientConnectionManager,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 PoolingAsyncClientConnectionManager extends java.lang.Object implements AsyncClientConnectionManager, org.apache.hc.core5.pool.ConnPoolControl<HttpRoute>
PoolingAsyncClientConnectionManagermaintains a pool of non-blockingHttpConnections 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.PoolingAsyncClientConnectionManagermaintains a maximum limit of connection on a per route basis and in total. Connection limits can be adjusted usingConnPoolControlmethods.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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classPoolingAsyncClientConnectionManager.InternalConnectionEndpoint
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.atomic.AtomicBooleanclosedprivate org.apache.hc.core5.function.Resolver<HttpRoute,ConnectionConfig>connectionConfigResolverprivate AsyncClientConnectionOperatorconnectionOperatorstatic intDEFAULT_MAX_CONNECTIONS_PER_ROUTEstatic intDEFAULT_MAX_TOTAL_CONNECTIONSprivate static PrefixedIncrementingIdINCREMENTING_IDprivate static org.slf4j.LoggerLOGprivate org.apache.hc.core5.pool.ManagedConnPool<HttpRoute,ManagedAsyncClientConnection>poolprivate org.apache.hc.core5.function.Resolver<org.apache.hc.core5.http.HttpHost,TlsConfig>tlsConfigResolver
-
Constructor Summary
Constructors Modifier Constructor Description PoolingAsyncClientConnectionManager()protectedPoolingAsyncClientConnectionManager(AsyncClientConnectionOperator connectionOperator, org.apache.hc.core5.pool.PoolConcurrencyPolicy poolConcurrencyPolicy, org.apache.hc.core5.pool.PoolReusePolicy poolReusePolicy, org.apache.hc.core5.util.TimeValue timeToLive)PoolingAsyncClientConnectionManager(org.apache.hc.core5.http.config.Lookup<org.apache.hc.core5.http.nio.ssl.TlsStrategy> tlsStrategyLookup)PoolingAsyncClientConnectionManager(org.apache.hc.core5.http.config.Lookup<org.apache.hc.core5.http.nio.ssl.TlsStrategy> tlsStrategyLookup, org.apache.hc.core5.pool.PoolConcurrencyPolicy poolConcurrencyPolicy, org.apache.hc.core5.pool.PoolReusePolicy poolReusePolicy, org.apache.hc.core5.util.TimeValue timeToLive)PoolingAsyncClientConnectionManager(org.apache.hc.core5.http.config.Lookup<org.apache.hc.core5.http.nio.ssl.TlsStrategy> tlsStrategyLookup, 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)PoolingAsyncClientConnectionManager(org.apache.hc.core5.http.config.Lookup<org.apache.hc.core5.http.nio.ssl.TlsStrategy> tlsStrategyLookup, org.apache.hc.core5.pool.PoolConcurrencyPolicy poolConcurrencyPolicy, org.apache.hc.core5.util.TimeValue timeToLive)protectedPoolingAsyncClientConnectionManager(org.apache.hc.core5.pool.ManagedConnPool<HttpRoute,ManagedAsyncClientConnection> pool, AsyncClientConnectionOperator connectionOperator)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private PoolingAsyncClientConnectionManager.InternalConnectionEndpointcast(AsyncConnectionEndpoint endpoint)voidclose()voidclose(org.apache.hc.core5.io.CloseMode closeMode)voidcloseExpired()voidcloseIdle(org.apache.hc.core5.util.TimeValue idletime)(package private) voidcloseIfExpired(org.apache.hc.core5.pool.PoolEntry<HttpRoute,ManagedAsyncClientConnection> entry)java.util.concurrent.Future<AsyncConnectionEndpoint>connect(AsyncConnectionEndpoint endpoint, org.apache.hc.core5.reactor.ConnectionInitiator connectionInitiator, org.apache.hc.core5.util.Timeout timeout, java.lang.Object attachment, org.apache.hc.core5.http.protocol.HttpContext context, org.apache.hc.core5.concurrent.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).intgetDefaultMaxPerRoute()intgetMaxPerRoute(HttpRoute route)intgetMaxTotal()java.util.Set<HttpRoute>getRoutes()org.apache.hc.core5.pool.PoolStatsgetStats(HttpRoute route)org.apache.hc.core5.pool.PoolStatsgetTotalStats()org.apache.hc.core5.util.TimeValuegetValidateAfterInactivity()Deprecated.Use customsetConnectionConfigResolver(Resolver)(package private) booleanisClosed()Method that can be called to determine whether the connection manager has been shut down and is closed or not.java.util.concurrent.Future<AsyncConnectionEndpoint>lease(java.lang.String id, HttpRoute route, java.lang.Object state, org.apache.hc.core5.util.Timeout requestTimeout, org.apache.hc.core5.concurrent.FutureCallback<AsyncConnectionEndpoint> callback)Returns aFutureobject which can be used to obtain anAsyncConnectionEndpointor to cancel the request by callingFuture.cancel(boolean).voidrelease(AsyncConnectionEndpoint endpoint, java.lang.Object state, org.apache.hc.core5.util.TimeValue keepAlive)Releases the endpoint back to the manager making it potentially re-usable by other consumers.private ConnectionConfigresolveConnectionConfig(HttpRoute route)private TlsConfigresolveTlsConfig(org.apache.hc.core5.http.HttpHost host, java.lang.Object attachment)voidsetConnectionConfigResolver(org.apache.hc.core5.function.Resolver<HttpRoute,ConnectionConfig> connectionConfigResolver)SetsResolverofConnectionConfigon a per route basis.voidsetDefaultConnectionConfig(ConnectionConfig config)Sets the sameConnectionConfigfor all routesvoidsetDefaultMaxPerRoute(int max)voidsetDefaultTlsConfig(TlsConfig config)Sets the sameConnectionConfigfor all hostsvoidsetMaxPerRoute(HttpRoute route, int max)voidsetMaxTotal(int max)voidsetTlsConfigResolver(org.apache.hc.core5.function.Resolver<org.apache.hc.core5.http.HttpHost,TlsConfig> tlsConfigResolver)SetsResolverofTlsConfigon a per host basis.voidsetValidateAfterInactivity(org.apache.hc.core5.util.TimeValue validateAfterInactivity)Deprecated.voidupgrade(AsyncConnectionEndpoint endpoint, java.lang.Object attachment, org.apache.hc.core5.http.protocol.HttpContext context)Upgrades transport security of the given endpoint by using the TLS security protocol.voidupgrade(AsyncConnectionEndpoint endpoint, java.lang.Object attachment, org.apache.hc.core5.http.protocol.HttpContext context, org.apache.hc.core5.concurrent.FutureCallback<AsyncConnectionEndpoint> callback)Upgrades transport security of the given endpoint by using the TLS security protocol.
-
-
-
Field Detail
-
LOG
private static final org.slf4j.Logger LOG
-
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
-
pool
private final org.apache.hc.core5.pool.ManagedConnPool<HttpRoute,ManagedAsyncClientConnection> pool
-
connectionOperator
private final AsyncClientConnectionOperator connectionOperator
-
closed
private final java.util.concurrent.atomic.AtomicBoolean closed
-
connectionConfigResolver
private volatile org.apache.hc.core5.function.Resolver<HttpRoute,ConnectionConfig> connectionConfigResolver
-
tlsConfigResolver
private volatile org.apache.hc.core5.function.Resolver<org.apache.hc.core5.http.HttpHost,TlsConfig> tlsConfigResolver
-
INCREMENTING_ID
private static final PrefixedIncrementingId INCREMENTING_ID
-
-
Constructor Detail
-
PoolingAsyncClientConnectionManager
public PoolingAsyncClientConnectionManager()
-
PoolingAsyncClientConnectionManager
public PoolingAsyncClientConnectionManager(org.apache.hc.core5.http.config.Lookup<org.apache.hc.core5.http.nio.ssl.TlsStrategy> tlsStrategyLookup)
-
PoolingAsyncClientConnectionManager
public PoolingAsyncClientConnectionManager(org.apache.hc.core5.http.config.Lookup<org.apache.hc.core5.http.nio.ssl.TlsStrategy> tlsStrategyLookup, org.apache.hc.core5.pool.PoolConcurrencyPolicy poolConcurrencyPolicy, org.apache.hc.core5.util.TimeValue timeToLive)
-
PoolingAsyncClientConnectionManager
public PoolingAsyncClientConnectionManager(org.apache.hc.core5.http.config.Lookup<org.apache.hc.core5.http.nio.ssl.TlsStrategy> tlsStrategyLookup, org.apache.hc.core5.pool.PoolConcurrencyPolicy poolConcurrencyPolicy, org.apache.hc.core5.pool.PoolReusePolicy poolReusePolicy, org.apache.hc.core5.util.TimeValue timeToLive)
-
PoolingAsyncClientConnectionManager
public PoolingAsyncClientConnectionManager(org.apache.hc.core5.http.config.Lookup<org.apache.hc.core5.http.nio.ssl.TlsStrategy> tlsStrategyLookup, 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)
-
PoolingAsyncClientConnectionManager
@Internal protected PoolingAsyncClientConnectionManager(AsyncClientConnectionOperator connectionOperator, org.apache.hc.core5.pool.PoolConcurrencyPolicy poolConcurrencyPolicy, org.apache.hc.core5.pool.PoolReusePolicy poolReusePolicy, org.apache.hc.core5.util.TimeValue timeToLive)
-
PoolingAsyncClientConnectionManager
@Internal protected PoolingAsyncClientConnectionManager(org.apache.hc.core5.pool.ManagedConnPool<HttpRoute,ManagedAsyncClientConnection> pool, AsyncClientConnectionOperator connectionOperator)
-
-
Method Detail
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-
close
public void close(org.apache.hc.core5.io.CloseMode closeMode)
- Specified by:
closein interfaceorg.apache.hc.core5.io.ModalCloseable
-
cast
private PoolingAsyncClientConnectionManager.InternalConnectionEndpoint cast(AsyncConnectionEndpoint endpoint)
-
resolveConnectionConfig
private ConnectionConfig resolveConnectionConfig(HttpRoute route)
-
resolveTlsConfig
private TlsConfig resolveTlsConfig(org.apache.hc.core5.http.HttpHost host, java.lang.Object attachment)
-
lease
public java.util.concurrent.Future<AsyncConnectionEndpoint> lease(java.lang.String id, HttpRoute route, java.lang.Object state, org.apache.hc.core5.util.Timeout requestTimeout, org.apache.hc.core5.concurrent.FutureCallback<AsyncConnectionEndpoint> callback)
Description copied from interface:AsyncClientConnectionManagerReturns aFutureobject which can be used to obtain anAsyncConnectionEndpointor to cancel the request by callingFuture.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 callingAsyncClientConnectionManager.connect(AsyncConnectionEndpoint, ConnectionInitiator, Timeout, Object, HttpContext, FutureCallback)in order to connect directly to the target or to the first proxy hop, and optionally callingAsyncClientConnectionManager.upgrade(AsyncConnectionEndpoint, Object, HttpContext)method to upgrade the underlying transport to Transport Layer Security after having executed aCONNECTmethod to all intermediate proxy hops.- Specified by:
leasein interfaceAsyncClientConnectionManager- Parameters:
id- unique operation ID ornull.route- HTTP route of the requested connection.state- expected state of the connection ornullif the connection is not expected to carry any state.requestTimeout- lease request timeout.callback- result callback.
-
release
public void release(AsyncConnectionEndpoint endpoint, java.lang.Object state, org.apache.hc.core5.util.TimeValue keepAlive)
Description copied from interface:AsyncClientConnectionManagerReleases 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 usingvalidDurationandtimeUnitparameters.- Specified by:
releasein interfaceAsyncClientConnectionManager- Parameters:
endpoint- the managed endpoint.state- the new connection state ofnullif state-less.keepAlive- the duration of time this connection is valid for reuse.
-
connect
public java.util.concurrent.Future<AsyncConnectionEndpoint> connect(AsyncConnectionEndpoint endpoint, org.apache.hc.core5.reactor.ConnectionInitiator connectionInitiator, org.apache.hc.core5.util.Timeout timeout, java.lang.Object attachment, org.apache.hc.core5.http.protocol.HttpContext context, org.apache.hc.core5.concurrent.FutureCallback<AsyncConnectionEndpoint> callback)
Description copied from interface:AsyncClientConnectionManagerConnects 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:
connectin interfaceAsyncClientConnectionManager- 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, java.lang.Object attachment, org.apache.hc.core5.http.protocol.HttpContext context, org.apache.hc.core5.concurrent.FutureCallback<AsyncConnectionEndpoint> callback)
Description copied from interface:AsyncClientConnectionManagerUpgrades transport security of the given endpoint by using the TLS security protocol.- Specified by:
upgradein interfaceAsyncClientConnectionManager- Parameters:
endpoint- the managed endpoint.attachment- the attachment the upgrade attachment object.context- the actual HTTP context.callback- result callback.
-
upgrade
public void upgrade(AsyncConnectionEndpoint endpoint, java.lang.Object attachment, org.apache.hc.core5.http.protocol.HttpContext context)
Description copied from interface:AsyncClientConnectionManagerUpgrades transport security of the given endpoint by using the TLS security protocol.- Specified by:
upgradein interfaceAsyncClientConnectionManager- Parameters:
endpoint- the managed endpoint.attachment- the attachment the upgrade attachment object.context- the actual HTTP context.
-
getRoutes
public java.util.Set<HttpRoute> getRoutes()
- Specified by:
getRoutesin interfaceorg.apache.hc.core5.pool.ConnPoolControl<HttpRoute>
-
setMaxTotal
public void setMaxTotal(int max)
- Specified by:
setMaxTotalin interfaceorg.apache.hc.core5.pool.ConnPoolControl<HttpRoute>
-
getMaxTotal
public int getMaxTotal()
- Specified by:
getMaxTotalin interfaceorg.apache.hc.core5.pool.ConnPoolControl<HttpRoute>
-
setDefaultMaxPerRoute
public void setDefaultMaxPerRoute(int max)
- Specified by:
setDefaultMaxPerRoutein interfaceorg.apache.hc.core5.pool.ConnPoolControl<HttpRoute>
-
getDefaultMaxPerRoute
public int getDefaultMaxPerRoute()
- Specified by:
getDefaultMaxPerRoutein interfaceorg.apache.hc.core5.pool.ConnPoolControl<HttpRoute>
-
setMaxPerRoute
public void setMaxPerRoute(HttpRoute route, int max)
- Specified by:
setMaxPerRoutein interfaceorg.apache.hc.core5.pool.ConnPoolControl<HttpRoute>
-
getMaxPerRoute
public int getMaxPerRoute(HttpRoute route)
- Specified by:
getMaxPerRoutein interfaceorg.apache.hc.core5.pool.ConnPoolControl<HttpRoute>
-
closeIdle
public void closeIdle(org.apache.hc.core5.util.TimeValue idletime)
- Specified by:
closeIdlein interfaceorg.apache.hc.core5.pool.ConnPoolControl<HttpRoute>
-
closeExpired
public void closeExpired()
- Specified by:
closeExpiredin interfaceorg.apache.hc.core5.pool.ConnPoolControl<HttpRoute>
-
getTotalStats
public org.apache.hc.core5.pool.PoolStats getTotalStats()
- Specified by:
getTotalStatsin interfaceorg.apache.hc.core5.pool.ConnPoolStats<HttpRoute>
-
getStats
public org.apache.hc.core5.pool.PoolStats getStats(HttpRoute route)
- Specified by:
getStatsin interfaceorg.apache.hc.core5.pool.ConnPoolStats<HttpRoute>
-
setDefaultConnectionConfig
public void setDefaultConnectionConfig(ConnectionConfig config)
Sets the sameConnectionConfigfor all routes- Since:
- 5.2
-
setConnectionConfigResolver
public void setConnectionConfigResolver(org.apache.hc.core5.function.Resolver<HttpRoute,ConnectionConfig> connectionConfigResolver)
SetsResolverofConnectionConfigon a per route basis.- Since:
- 5.2
-
setDefaultTlsConfig
public void setDefaultTlsConfig(TlsConfig config)
Sets the sameConnectionConfigfor all hosts- Since:
- 5.2
-
setTlsConfigResolver
public void setTlsConfigResolver(org.apache.hc.core5.function.Resolver<org.apache.hc.core5.http.HttpHost,TlsConfig> tlsConfigResolver)
SetsResolverofTlsConfigon a per host basis.- Since:
- 5.2
-
closeIfExpired
void closeIfExpired(org.apache.hc.core5.pool.PoolEntry<HttpRoute,ManagedAsyncClientConnection> entry)
-
getValidateAfterInactivity
@Deprecated public org.apache.hc.core5.util.TimeValue getValidateAfterInactivity()
Deprecated.Use customsetConnectionConfigResolver(Resolver)
-
setValidateAfterInactivity
@Deprecated public void setValidateAfterInactivity(org.apache.hc.core5.util.TimeValue validateAfterInactivity)
Deprecated.Defines period of inactivity after which persistent connections must be re-validated prior to beinglease(String, HttpRoute, Object, Timeout, FutureCallback)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.
-
isClosed
boolean isClosed()
Method that can be called to determine whether the connection manager has been shut down and is closed or not.- Returns:
trueif the connection manager has been shut down and is closed, otherwise returnfalse.
-
-