@Contract(threading=SAFE_CONDITIONAL) public class PoolingHttpClientConnectionManager extends Object implements HttpClientConnectionManager, org.apache.http.pool.ConnPoolControl<HttpRoute>, Closeable
ClientConnectionPoolManager maintains a pool of
HttpClientConnections 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 brand new connection.
ClientConnectionPoolManager maintains a maximum limit of connection
on a per route basis and in total. Per default this implementation will
create no more than than 2 concurrent connections per given route
and no more 20 connections in total. For many real-world applications
these limits may prove too constraining, especially if they use HTTP
as a transport protocol for their services. 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 2000ms
| Modifier and Type | Method and Description |
|---|---|
void |
close() |
void |
closeExpiredConnections()
Closes all expired connections in the pool.
|
void |
closeIdleConnections(long idleTimeout,
TimeUnit timeUnit)
Closes idle connections in the pool.
|
void |
connect(org.apache.http.HttpClientConnection managedConn,
HttpRoute route,
int connectTimeout,
org.apache.http.protocol.HttpContext context)
Connects the underlying connection socket to the 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).
|
protected void |
enumAvailable(org.apache.http.pool.PoolEntryCallback<HttpRoute,ManagedHttpClientConnection> callback) |
protected void |
enumLeased(org.apache.http.pool.PoolEntryCallback<HttpRoute,ManagedHttpClientConnection> callback) |
protected void |
finalize() |
org.apache.http.config.ConnectionConfig |
getConnectionConfig(org.apache.http.HttpHost host) |
org.apache.http.config.ConnectionConfig |
getDefaultConnectionConfig() |
int |
getDefaultMaxPerRoute() |
org.apache.http.config.SocketConfig |
getDefaultSocketConfig() |
int |
getMaxPerRoute(HttpRoute route) |
int |
getMaxTotal() |
Set<HttpRoute> |
getRoutes() |
org.apache.http.config.SocketConfig |
getSocketConfig(org.apache.http.HttpHost host) |
org.apache.http.pool.PoolStats |
getStats(HttpRoute route) |
org.apache.http.pool.PoolStats |
getTotalStats() |
int |
getValidateAfterInactivity() |
protected org.apache.http.HttpClientConnection |
leaseConnection(Future<org.apache.http.impl.conn.CPoolEntry> future,
long timeout,
TimeUnit timeUnit) |
void |
releaseConnection(org.apache.http.HttpClientConnection managedConn,
Object state,
long keepalive,
TimeUnit timeUnit)
Releases the connection back to the manager making it potentially
re-usable by other consumers.
|
ConnectionRequest |
requestConnection(HttpRoute route,
Object state)
Returns a new
ConnectionRequest, from which a
HttpClientConnection can be obtained or the request can be
aborted. |
void |
routeComplete(org.apache.http.HttpClientConnection managedConn,
HttpRoute route,
org.apache.http.protocol.HttpContext context)
Marks the connection as fully established with all its intermediate
hops completed.
|
void |
setConnectionConfig(org.apache.http.HttpHost host,
org.apache.http.config.ConnectionConfig connectionConfig) |
void |
setDefaultConnectionConfig(org.apache.http.config.ConnectionConfig defaultConnectionConfig) |
void |
setDefaultMaxPerRoute(int max) |
void |
setDefaultSocketConfig(org.apache.http.config.SocketConfig defaultSocketConfig) |
void |
setMaxPerRoute(HttpRoute route,
int max) |
void |
setMaxTotal(int max) |
void |
setSocketConfig(org.apache.http.HttpHost host,
org.apache.http.config.SocketConfig socketConfig) |
void |
setValidateAfterInactivity(int ms)
Defines period of inactivity in milliseconds after which persistent connections must
be re-validated prior to being
leased to the consumer. |
void |
shutdown()
Shuts down this connection manager and releases allocated resources.
|
void |
upgrade(org.apache.http.HttpClientConnection managedConn,
HttpRoute route,
org.apache.http.protocol.HttpContext context)
Upgrades the underlying connection socket to TLS/SSL (or another layering
protocol) after having executed
CONNECT method to all
intermediate proxy hops |
public PoolingHttpClientConnectionManager()
public PoolingHttpClientConnectionManager(long timeToLive,
TimeUnit timeUnit)
public PoolingHttpClientConnectionManager(org.apache.http.config.Registry<ConnectionSocketFactory> socketFactoryRegistry)
public PoolingHttpClientConnectionManager(org.apache.http.config.Registry<ConnectionSocketFactory> socketFactoryRegistry, DnsResolver dnsResolver)
public PoolingHttpClientConnectionManager(org.apache.http.config.Registry<ConnectionSocketFactory> socketFactoryRegistry, HttpConnectionFactory<HttpRoute,ManagedHttpClientConnection> connFactory)
public PoolingHttpClientConnectionManager(HttpConnectionFactory<HttpRoute,ManagedHttpClientConnection> connFactory)
public PoolingHttpClientConnectionManager(org.apache.http.config.Registry<ConnectionSocketFactory> socketFactoryRegistry, HttpConnectionFactory<HttpRoute,ManagedHttpClientConnection> connFactory, DnsResolver dnsResolver)
public PoolingHttpClientConnectionManager(org.apache.http.config.Registry<ConnectionSocketFactory> socketFactoryRegistry, HttpConnectionFactory<HttpRoute,ManagedHttpClientConnection> connFactory, SchemePortResolver schemePortResolver, DnsResolver dnsResolver, long timeToLive, TimeUnit timeUnit)
public PoolingHttpClientConnectionManager(HttpClientConnectionOperator httpClientConnectionOperator, HttpConnectionFactory<HttpRoute,ManagedHttpClientConnection> connFactory, long timeToLive, TimeUnit timeUnit)
protected void finalize()
throws Throwable
public void close()
close in interface Closeableclose in interface AutoCloseablepublic ConnectionRequest requestConnection(HttpRoute route, Object state)
HttpClientConnectionManagerConnectionRequest, from which a
HttpClientConnection can be obtained or the request can be
aborted.
Please note that newly allocated connections can be returned
in the closed state. The consumer of that connection is responsible
for fully establishing the route the to the connection target
by calling connect in order to connect
directly to the target or to the first proxy hop, optionally calling
upgrade method to upgrade
the connection after having executed CONNECT method to
all intermediate proxy hops and and finally calling routeComplete to mark the route
as fully completed.
requestConnection in interface HttpClientConnectionManagerroute - HTTP route of the requested connection.state - expected state of the connection or null
if the connection is not expected to carry any state.protected org.apache.http.HttpClientConnection leaseConnection(Future<org.apache.http.impl.conn.CPoolEntry> future, long timeout, TimeUnit timeUnit) throws InterruptedException, ExecutionException, ConnectionPoolTimeoutException
public void releaseConnection(org.apache.http.HttpClientConnection managedConn,
Object state,
long keepalive,
TimeUnit timeUnit)
HttpClientConnectionManagervalidDuration and timeUnit
parameters.releaseConnection in interface HttpClientConnectionManagermanagedConn - the managed connection to release.keepalive - the duration of time this connection is valid for reuse.timeUnit - the time unit.HttpClientConnectionManager.closeExpiredConnections()public void connect(org.apache.http.HttpClientConnection managedConn,
HttpRoute route,
int connectTimeout,
org.apache.http.protocol.HttpContext context)
throws IOException
HttpClientConnectionManagerconnect in interface HttpClientConnectionManagermanagedConn - the managed connection.route - the route of the connection.connectTimeout - connect timeout in milliseconds.context - the actual HTTP context.IOExceptionpublic void upgrade(org.apache.http.HttpClientConnection managedConn,
HttpRoute route,
org.apache.http.protocol.HttpContext context)
throws IOException
HttpClientConnectionManagerCONNECT method to all
intermediate proxy hopsupgrade in interface HttpClientConnectionManagermanagedConn - the managed connection.route - the route of the connection.context - the actual HTTP context.IOExceptionpublic void routeComplete(org.apache.http.HttpClientConnection managedConn,
HttpRoute route,
org.apache.http.protocol.HttpContext context)
throws IOException
HttpClientConnectionManagerrouteComplete in interface HttpClientConnectionManagermanagedConn - the managed connection.route - the route of the connection.context - the actual HTTP context.IOExceptionpublic void shutdown()
HttpClientConnectionManagershutdown in interface HttpClientConnectionManagerpublic void closeIdleConnections(long idleTimeout,
TimeUnit timeUnit)
HttpClientConnectionManagerOpen connections in the pool that have not been used for the timespan given by the argument will be closed. Currently allocated connections are not subject to this method. Times will be checked with milliseconds precision
All expired connections will also be closed.
closeIdleConnections in interface HttpClientConnectionManageridleTimeout - the idle time of connections to be closedtimeUnit - the unit for the idletimeHttpClientConnectionManager.closeExpiredConnections()public void closeExpiredConnections()
HttpClientConnectionManagerOpen connections in the pool that have not been used for the timespan defined when the connection was released will be closed. Currently allocated connections are not subject to this method. Times will be checked with milliseconds precision.
closeExpiredConnections in interface HttpClientConnectionManagerprotected void enumAvailable(org.apache.http.pool.PoolEntryCallback<HttpRoute,ManagedHttpClientConnection> callback)
protected void enumLeased(org.apache.http.pool.PoolEntryCallback<HttpRoute,ManagedHttpClientConnection> callback)
public int getMaxTotal()
getMaxTotal in interface org.apache.http.pool.ConnPoolControl<HttpRoute>public void setMaxTotal(int max)
setMaxTotal in interface org.apache.http.pool.ConnPoolControl<HttpRoute>public int getDefaultMaxPerRoute()
getDefaultMaxPerRoute in interface org.apache.http.pool.ConnPoolControl<HttpRoute>public void setDefaultMaxPerRoute(int max)
setDefaultMaxPerRoute in interface org.apache.http.pool.ConnPoolControl<HttpRoute>public int getMaxPerRoute(HttpRoute route)
getMaxPerRoute in interface org.apache.http.pool.ConnPoolControl<HttpRoute>public void setMaxPerRoute(HttpRoute route, int max)
setMaxPerRoute in interface org.apache.http.pool.ConnPoolControl<HttpRoute>public org.apache.http.pool.PoolStats getTotalStats()
getTotalStats in interface org.apache.http.pool.ConnPoolControl<HttpRoute>public org.apache.http.pool.PoolStats getStats(HttpRoute route)
getStats in interface org.apache.http.pool.ConnPoolControl<HttpRoute>public org.apache.http.config.SocketConfig getDefaultSocketConfig()
public void setDefaultSocketConfig(org.apache.http.config.SocketConfig defaultSocketConfig)
public org.apache.http.config.ConnectionConfig getDefaultConnectionConfig()
public void setDefaultConnectionConfig(org.apache.http.config.ConnectionConfig defaultConnectionConfig)
public org.apache.http.config.SocketConfig getSocketConfig(org.apache.http.HttpHost host)
public void setSocketConfig(org.apache.http.HttpHost host,
org.apache.http.config.SocketConfig socketConfig)
public org.apache.http.config.ConnectionConfig getConnectionConfig(org.apache.http.HttpHost host)
public void setConnectionConfig(org.apache.http.HttpHost host,
org.apache.http.config.ConnectionConfig connectionConfig)
public int getValidateAfterInactivity()
setValidateAfterInactivity(int)public void setValidateAfterInactivity(int ms)
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.Copyright © 1999–2022 The Apache Software Foundation. All rights reserved.