Class PoolingHttpClientConnectionManager
- java.lang.Object
-
- org.apache.http.impl.conn.PoolingHttpClientConnectionManager
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,HttpClientConnectionManager,org.apache.http.pool.ConnPoolControl<HttpRoute>
@Contract(threading=SAFE_CONDITIONAL) public class PoolingHttpClientConnectionManager extends java.lang.Object implements HttpClientConnectionManager, org.apache.http.pool.ConnPoolControl<HttpRoute>, java.io.Closeable
ClientConnectionPoolManagermaintains a pool ofHttpClientConnections 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.ClientConnectionPoolManagermaintains 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 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.
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
- Since:
- 4.3
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classPoolingHttpClientConnectionManager.ConfigData(package private) static classPoolingHttpClientConnectionManager.InternalConnectionFactory
-
Field Summary
Fields Modifier and Type Field Description private PoolingHttpClientConnectionManager.ConfigDataconfigDataprivate HttpClientConnectionOperatorconnectionOperatorprivate java.util.concurrent.atomic.AtomicBooleanisShutDownprivate org.apache.commons.logging.Loglogprivate CPoolpool
-
Constructor Summary
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()voidcloseExpiredConnections()Closes all expired connections in the pool.voidcloseIdleConnections(long idleTimeout, java.util.concurrent.TimeUnit timeUnit)Closes idle connections in the pool.voidconnect(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 voidenumAvailable(org.apache.http.pool.PoolEntryCallback<HttpRoute,ManagedHttpClientConnection> callback)protected voidenumLeased(org.apache.http.pool.PoolEntryCallback<HttpRoute,ManagedHttpClientConnection> callback)protected voidfinalize()private java.lang.Stringformat(HttpRoute route, java.lang.Object state)private java.lang.Stringformat(CPoolEntry entry)private java.lang.StringformatStats(HttpRoute route)org.apache.http.config.ConnectionConfiggetConnectionConfig(org.apache.http.HttpHost host)org.apache.http.config.ConnectionConfiggetDefaultConnectionConfig()intgetDefaultMaxPerRoute()private static org.apache.http.config.Registry<ConnectionSocketFactory>getDefaultRegistry()org.apache.http.config.SocketConfiggetDefaultSocketConfig()intgetMaxPerRoute(HttpRoute route)intgetMaxTotal()java.util.Set<HttpRoute>getRoutes()org.apache.http.config.SocketConfiggetSocketConfig(org.apache.http.HttpHost host)org.apache.http.pool.PoolStatsgetStats(HttpRoute route)org.apache.http.pool.PoolStatsgetTotalStats()intgetValidateAfterInactivity()protected org.apache.http.HttpClientConnectionleaseConnection(java.util.concurrent.Future<CPoolEntry> future, long timeout, java.util.concurrent.TimeUnit timeUnit)voidreleaseConnection(org.apache.http.HttpClientConnection managedConn, java.lang.Object state, long keepalive, java.util.concurrent.TimeUnit timeUnit)Releases the connection back to the manager making it potentially re-usable by other consumers.ConnectionRequestrequestConnection(HttpRoute route, java.lang.Object state)Returns a newConnectionRequest, from which aHttpClientConnectioncan be obtained or the request can be aborted.private org.apache.http.config.SocketConfigresolveSocketConfig(org.apache.http.HttpHost host)voidrouteComplete(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.voidsetConnectionConfig(org.apache.http.HttpHost host, org.apache.http.config.ConnectionConfig connectionConfig)voidsetDefaultConnectionConfig(org.apache.http.config.ConnectionConfig defaultConnectionConfig)voidsetDefaultMaxPerRoute(int max)voidsetDefaultSocketConfig(org.apache.http.config.SocketConfig defaultSocketConfig)voidsetMaxPerRoute(HttpRoute route, int max)voidsetMaxTotal(int max)voidsetSocketConfig(org.apache.http.HttpHost host, org.apache.http.config.SocketConfig socketConfig)voidsetValidateAfterInactivity(int ms)Defines period of inactivity in milliseconds after which persistent connections must be re-validated prior to beingleasedto the consumer.voidshutdown()Shuts down this connection manager and releases allocated resources.voidupgrade(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 executedCONNECTmethod to all intermediate proxy hops
-
-
-
Field Detail
-
log
private final org.apache.commons.logging.Log log
-
configData
private final PoolingHttpClientConnectionManager.ConfigData configData
-
pool
private final CPool pool
-
connectionOperator
private final HttpClientConnectionOperator connectionOperator
-
isShutDown
private final java.util.concurrent.atomic.AtomicBoolean isShutDown
-
-
Constructor Detail
-
PoolingHttpClientConnectionManager
public PoolingHttpClientConnectionManager()
-
PoolingHttpClientConnectionManager
public PoolingHttpClientConnectionManager(long timeToLive, java.util.concurrent.TimeUnit timeUnit)
-
PoolingHttpClientConnectionManager
public PoolingHttpClientConnectionManager(org.apache.http.config.Registry<ConnectionSocketFactory> socketFactoryRegistry)
-
PoolingHttpClientConnectionManager
public PoolingHttpClientConnectionManager(org.apache.http.config.Registry<ConnectionSocketFactory> socketFactoryRegistry, DnsResolver dnsResolver)
-
PoolingHttpClientConnectionManager
public PoolingHttpClientConnectionManager(org.apache.http.config.Registry<ConnectionSocketFactory> socketFactoryRegistry, HttpConnectionFactory<HttpRoute,ManagedHttpClientConnection> connFactory)
-
PoolingHttpClientConnectionManager
public PoolingHttpClientConnectionManager(HttpConnectionFactory<HttpRoute,ManagedHttpClientConnection> connFactory)
-
PoolingHttpClientConnectionManager
public PoolingHttpClientConnectionManager(org.apache.http.config.Registry<ConnectionSocketFactory> socketFactoryRegistry, HttpConnectionFactory<HttpRoute,ManagedHttpClientConnection> connFactory, DnsResolver dnsResolver)
-
PoolingHttpClientConnectionManager
public PoolingHttpClientConnectionManager(org.apache.http.config.Registry<ConnectionSocketFactory> socketFactoryRegistry, HttpConnectionFactory<HttpRoute,ManagedHttpClientConnection> connFactory, SchemePortResolver schemePortResolver, DnsResolver dnsResolver, long timeToLive, java.util.concurrent.TimeUnit timeUnit)
-
PoolingHttpClientConnectionManager
public PoolingHttpClientConnectionManager(HttpClientConnectionOperator httpClientConnectionOperator, HttpConnectionFactory<HttpRoute,ManagedHttpClientConnection> connFactory, long timeToLive, java.util.concurrent.TimeUnit timeUnit)
- Since:
- 4.4
-
PoolingHttpClientConnectionManager
PoolingHttpClientConnectionManager(CPool pool, org.apache.http.config.Lookup<ConnectionSocketFactory> socketFactoryRegistry, SchemePortResolver schemePortResolver, DnsResolver dnsResolver)
Visible for test.
-
-
Method Detail
-
getDefaultRegistry
private static org.apache.http.config.Registry<ConnectionSocketFactory> getDefaultRegistry()
-
finalize
protected void finalize() throws java.lang.Throwable- Overrides:
finalizein classjava.lang.Object- Throws:
java.lang.Throwable
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-
format
private java.lang.String format(HttpRoute route, java.lang.Object state)
-
formatStats
private java.lang.String formatStats(HttpRoute route)
-
format
private java.lang.String format(CPoolEntry entry)
-
resolveSocketConfig
private org.apache.http.config.SocketConfig resolveSocketConfig(org.apache.http.HttpHost host)
-
requestConnection
public ConnectionRequest requestConnection(HttpRoute route, java.lang.Object state)
Description copied from interface:HttpClientConnectionManagerReturns a newConnectionRequest, from which aHttpClientConnectioncan 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
connectin order to connect directly to the target or to the first proxy hop, optionally callingupgrademethod to upgrade the connection after having executedCONNECTmethod to all intermediate proxy hops and and finally callingrouteCompleteto mark the route as fully completed.- Specified by:
requestConnectionin interfaceHttpClientConnectionManager- Parameters:
route- HTTP route of the requested connection.state- expected state of the connection ornullif the connection is not expected to carry any state.
-
leaseConnection
protected org.apache.http.HttpClientConnection leaseConnection(java.util.concurrent.Future<CPoolEntry> future, long timeout, java.util.concurrent.TimeUnit timeUnit) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException, ConnectionPoolTimeoutException
- Throws:
java.lang.InterruptedExceptionjava.util.concurrent.ExecutionExceptionConnectionPoolTimeoutException
-
releaseConnection
public void releaseConnection(org.apache.http.HttpClientConnection managedConn, java.lang.Object state, long keepalive, java.util.concurrent.TimeUnit timeUnit)Description copied from interface:HttpClientConnectionManagerReleases the connection 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:
releaseConnectionin interfaceHttpClientConnectionManager- Parameters:
managedConn- the managed connection to release.keepalive- the duration of time this connection is valid for reuse.timeUnit- the time unit.- See Also:
HttpClientConnectionManager.closeExpiredConnections()
-
connect
public void connect(org.apache.http.HttpClientConnection managedConn, HttpRoute route, int connectTimeout, org.apache.http.protocol.HttpContext context) throws java.io.IOExceptionDescription copied from interface:HttpClientConnectionManagerConnects 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).- Specified by:
connectin interfaceHttpClientConnectionManager- Parameters:
managedConn- the managed connection.route- the route of the connection.connectTimeout- connect timeout in milliseconds.context- the actual HTTP context.- Throws:
java.io.IOException
-
upgrade
public void upgrade(org.apache.http.HttpClientConnection managedConn, HttpRoute route, org.apache.http.protocol.HttpContext context) throws java.io.IOExceptionDescription copied from interface:HttpClientConnectionManagerUpgrades the underlying connection socket to TLS/SSL (or another layering protocol) after having executedCONNECTmethod to all intermediate proxy hops- Specified by:
upgradein interfaceHttpClientConnectionManager- Parameters:
managedConn- the managed connection.route- the route of the connection.context- the actual HTTP context.- Throws:
java.io.IOException
-
routeComplete
public void routeComplete(org.apache.http.HttpClientConnection managedConn, HttpRoute route, org.apache.http.protocol.HttpContext context) throws java.io.IOExceptionDescription copied from interface:HttpClientConnectionManagerMarks the connection as fully established with all its intermediate hops completed.- Specified by:
routeCompletein interfaceHttpClientConnectionManager- Parameters:
managedConn- the managed connection.route- the route of the connection.context- the actual HTTP context.- Throws:
java.io.IOException
-
shutdown
public void shutdown()
Description copied from interface:HttpClientConnectionManagerShuts down this connection manager and releases allocated resources. This includes closing all connections, whether they are currently used or not.- Specified by:
shutdownin interfaceHttpClientConnectionManager
-
closeIdleConnections
public void closeIdleConnections(long idleTimeout, java.util.concurrent.TimeUnit timeUnit)Description copied from interface:HttpClientConnectionManagerCloses idle connections in the pool.Open 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.
- Specified by:
closeIdleConnectionsin interfaceHttpClientConnectionManager- Parameters:
idleTimeout- the idle time of connections to be closedtimeUnit- the unit for theidletime- See Also:
HttpClientConnectionManager.closeExpiredConnections()
-
closeExpiredConnections
public void closeExpiredConnections()
Description copied from interface:HttpClientConnectionManagerCloses all expired connections in the pool.Open 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.
- Specified by:
closeExpiredConnectionsin interfaceHttpClientConnectionManager
-
enumAvailable
protected void enumAvailable(org.apache.http.pool.PoolEntryCallback<HttpRoute,ManagedHttpClientConnection> callback)
-
enumLeased
protected void enumLeased(org.apache.http.pool.PoolEntryCallback<HttpRoute,ManagedHttpClientConnection> callback)
-
getMaxTotal
public int getMaxTotal()
- Specified by:
getMaxTotalin interfaceorg.apache.http.pool.ConnPoolControl<HttpRoute>
-
setMaxTotal
public void setMaxTotal(int max)
- Specified by:
setMaxTotalin interfaceorg.apache.http.pool.ConnPoolControl<HttpRoute>
-
getDefaultMaxPerRoute
public int getDefaultMaxPerRoute()
- Specified by:
getDefaultMaxPerRoutein interfaceorg.apache.http.pool.ConnPoolControl<HttpRoute>
-
setDefaultMaxPerRoute
public void setDefaultMaxPerRoute(int max)
- Specified by:
setDefaultMaxPerRoutein interfaceorg.apache.http.pool.ConnPoolControl<HttpRoute>
-
getMaxPerRoute
public int getMaxPerRoute(HttpRoute route)
- Specified by:
getMaxPerRoutein interfaceorg.apache.http.pool.ConnPoolControl<HttpRoute>
-
setMaxPerRoute
public void setMaxPerRoute(HttpRoute route, int max)
- Specified by:
setMaxPerRoutein interfaceorg.apache.http.pool.ConnPoolControl<HttpRoute>
-
getTotalStats
public org.apache.http.pool.PoolStats getTotalStats()
- Specified by:
getTotalStatsin interfaceorg.apache.http.pool.ConnPoolControl<HttpRoute>
-
getStats
public org.apache.http.pool.PoolStats getStats(HttpRoute route)
- Specified by:
getStatsin interfaceorg.apache.http.pool.ConnPoolControl<HttpRoute>
-
getRoutes
public java.util.Set<HttpRoute> getRoutes()
- Since:
- 4.4
-
getDefaultSocketConfig
public org.apache.http.config.SocketConfig getDefaultSocketConfig()
-
setDefaultSocketConfig
public void setDefaultSocketConfig(org.apache.http.config.SocketConfig defaultSocketConfig)
-
getDefaultConnectionConfig
public org.apache.http.config.ConnectionConfig getDefaultConnectionConfig()
-
setDefaultConnectionConfig
public void setDefaultConnectionConfig(org.apache.http.config.ConnectionConfig defaultConnectionConfig)
-
getSocketConfig
public org.apache.http.config.SocketConfig getSocketConfig(org.apache.http.HttpHost host)
-
setSocketConfig
public void setSocketConfig(org.apache.http.HttpHost host, org.apache.http.config.SocketConfig socketConfig)
-
getConnectionConfig
public org.apache.http.config.ConnectionConfig getConnectionConfig(org.apache.http.HttpHost host)
-
setConnectionConfig
public void setConnectionConfig(org.apache.http.HttpHost host, org.apache.http.config.ConnectionConfig connectionConfig)
-
getValidateAfterInactivity
public int getValidateAfterInactivity()
- Since:
- 4.4
- See Also:
setValidateAfterInactivity(int)
-
setValidateAfterInactivity
public void setValidateAfterInactivity(int ms)
Defines period of inactivity in milliseconds after which persistent connections must be re-validated prior to beingleasedto 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:
leaseConnection(java.util.concurrent.Future, long, java.util.concurrent.TimeUnit)
-
-