Class PoolingNHttpClientConnectionManager
- java.lang.Object
-
- org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager
-
- All Implemented Interfaces:
NHttpClientConnectionManager,org.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
@Contract(threading=SAFE) public class PoolingNHttpClientConnectionManager extends Object implements NHttpClientConnectionManager, org.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
PoolingNHttpClientConnectionManagermaintains a pool ofNHttpClientConnections 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.PoolingNHttpClientConnectionManagermaintains 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.- Since:
- 4.0
-
-
Constructor Summary
Constructors Constructor Description PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor)PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor, org.apache.http.config.Registry<SchemeIOSessionStrategy> iosessionFactoryRegistry)PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor, NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory)PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor, NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory, org.apache.http.config.Registry<SchemeIOSessionStrategy> iosessionFactoryRegistry)PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor, NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory, org.apache.http.config.Registry<SchemeIOSessionStrategy> iosessionFactoryRegistry, org.apache.http.conn.DnsResolver dnsResolver)PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor, NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory, org.apache.http.config.Registry<SchemeIOSessionStrategy> iosessionFactoryRegistry, org.apache.http.conn.SchemePortResolver schemePortResolver, org.apache.http.conn.DnsResolver dnsResolver, long timeToLive, TimeUnit tunit)PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor, NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory, org.apache.http.config.Registry<SchemeIOSessionStrategy> iosessionFactoryRegistry, org.apache.http.nio.pool.SocketAddressResolver<org.apache.http.conn.routing.HttpRoute> socketAddressResolver)PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor, NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory, org.apache.http.config.Registry<SchemeIOSessionStrategy> iosessionFactoryRegistry, org.apache.http.nio.pool.SocketAddressResolver<org.apache.http.conn.routing.HttpRoute> socketAddressResolver, long timeToLive, TimeUnit tunit)PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor, NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory, org.apache.http.conn.DnsResolver dnsResolver)PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor, NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory, org.apache.http.nio.pool.SocketAddressResolver<org.apache.http.conn.routing.HttpRoute> socketAddressResolver)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcloseExpiredConnections()Closes all expired connections in the pool.voidcloseIdleConnections(long idleTimeout, TimeUnit tunit)Closes idle connections in the pool.voidexecute(org.apache.http.nio.reactor.IOEventDispatch eventDispatch)Starts the underlying I/O reactor and initiates the dispatch of I/O event notifications to the givenIOEventDispatch.protected voidfinalize()org.apache.http.config.ConnectionConfiggetConnectionConfig(org.apache.http.HttpHost host)org.apache.http.config.ConnectionConfiggetDefaultConnectionConfig()intgetDefaultMaxPerRoute()intgetMaxPerRoute(org.apache.http.conn.routing.HttpRoute route)intgetMaxTotal()Set<org.apache.http.conn.routing.HttpRoute>getRoutes()org.apache.http.pool.PoolStatsgetStats(org.apache.http.conn.routing.HttpRoute route)org.apache.http.pool.PoolStatsgetTotalStats()booleanisRouteComplete(org.apache.http.nio.NHttpClientConnection managedConn)Determines if the given connection has been fully established and marked as route complete.voidreleaseConnection(org.apache.http.nio.NHttpClientConnection managedConn, Object state, long keepalive, TimeUnit tunit)Releases the connection back to the manager making it potentially re-usable by other consumers.Future<org.apache.http.nio.NHttpClientConnection>requestConnection(org.apache.http.conn.routing.HttpRoute route, Object state, long connectTimeout, long leaseTimeout, TimeUnit tunit, org.apache.http.concurrent.FutureCallback<org.apache.http.nio.NHttpClientConnection> callback)Returns aFuturefor aNHttpClientConnection.voidrouteComplete(org.apache.http.nio.NHttpClientConnection managedConn, org.apache.http.conn.routing.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)voidsetMaxPerRoute(org.apache.http.conn.routing.HttpRoute route, int max)voidsetMaxTotal(int max)voidshutdown()Shuts down this connection manager and releases allocated resources.voidshutdown(long waitMs)voidstartRoute(org.apache.http.nio.NHttpClientConnection managedConn, org.apache.http.conn.routing.HttpRoute route, org.apache.http.protocol.HttpContext context)Starts the process of connection initialization.voidupgrade(org.apache.http.nio.NHttpClientConnection managedConn, org.apache.http.conn.routing.HttpRoute route, org.apache.http.protocol.HttpContext context)Upgrades the underlying connection I/O session to TLS/SSL (or another layering protocol) after having executedCONNECTmethod to all intermediate proxy hops.voidvalidatePendingRequests()
-
-
-
Constructor Detail
-
PoolingNHttpClientConnectionManager
public PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor)
-
PoolingNHttpClientConnectionManager
public PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor, org.apache.http.config.Registry<SchemeIOSessionStrategy> iosessionFactoryRegistry)
-
PoolingNHttpClientConnectionManager
public PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor, NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory, org.apache.http.conn.DnsResolver dnsResolver)
-
PoolingNHttpClientConnectionManager
public PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor, NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory, org.apache.http.nio.pool.SocketAddressResolver<org.apache.http.conn.routing.HttpRoute> socketAddressResolver)
-
PoolingNHttpClientConnectionManager
public PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor, NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory)
-
PoolingNHttpClientConnectionManager
public PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor, NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory, org.apache.http.config.Registry<SchemeIOSessionStrategy> iosessionFactoryRegistry)
-
PoolingNHttpClientConnectionManager
public PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor, NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory, org.apache.http.config.Registry<SchemeIOSessionStrategy> iosessionFactoryRegistry, org.apache.http.conn.DnsResolver dnsResolver)
-
PoolingNHttpClientConnectionManager
public PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor, NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory, org.apache.http.config.Registry<SchemeIOSessionStrategy> iosessionFactoryRegistry, org.apache.http.nio.pool.SocketAddressResolver<org.apache.http.conn.routing.HttpRoute> socketAddressResolver)
-
PoolingNHttpClientConnectionManager
public PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor, NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory, org.apache.http.config.Registry<SchemeIOSessionStrategy> iosessionFactoryRegistry, org.apache.http.conn.SchemePortResolver schemePortResolver, org.apache.http.conn.DnsResolver dnsResolver, long timeToLive, TimeUnit tunit)
-
PoolingNHttpClientConnectionManager
public PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor, NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory, org.apache.http.config.Registry<SchemeIOSessionStrategy> iosessionFactoryRegistry, org.apache.http.nio.pool.SocketAddressResolver<org.apache.http.conn.routing.HttpRoute> socketAddressResolver, long timeToLive, TimeUnit tunit)
-
-
Method Detail
-
finalize
protected void finalize() throws Throwable
-
execute
public void execute(org.apache.http.nio.reactor.IOEventDispatch eventDispatch) throws IOExceptionDescription copied from interface:NHttpClientConnectionManagerStarts the underlying I/O reactor and initiates the dispatch of I/O event notifications to the givenIOEventDispatch.- Specified by:
executein interfaceNHttpClientConnectionManager- Throws:
IOException
-
shutdown
public void shutdown(long waitMs) throws IOException- Throws:
IOException
-
shutdown
public void shutdown() throws IOExceptionDescription copied from interface:NHttpClientConnectionManagerShuts down this connection manager and releases allocated resources. This includes closing all connections, whether they are currently used or not.- Specified by:
shutdownin interfaceNHttpClientConnectionManager- Throws:
IOException
-
requestConnection
public Future<org.apache.http.nio.NHttpClientConnection> requestConnection(org.apache.http.conn.routing.HttpRoute route, Object state, long connectTimeout, long leaseTimeout, TimeUnit tunit, org.apache.http.concurrent.FutureCallback<org.apache.http.nio.NHttpClientConnection> callback)
Description copied from interface:NHttpClientConnectionManagerReturns aFuturefor aNHttpClientConnection.Please note that the consumer of that connection is responsible for fully establishing the route the to the connection target by calling
startRoutein order to start the process of connection initialization, 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 interfaceNHttpClientConnectionManager- Parameters:
route- HTTP route of the requested connection.state- expected state of the connection ornullif the connection is not expected to carry any state.connectTimeout- connect timeout.leaseTimeout- connection request timeout.tunit- time unit of the previous two timeout values.callback- future callback.
-
releaseConnection
public void releaseConnection(org.apache.http.nio.NHttpClientConnection managedConn, Object state, long keepalive, TimeUnit tunit)Description copied from interface:NHttpClientConnectionManagerReleases 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 interfaceNHttpClientConnectionManager- Parameters:
managedConn- the managed connection to release.keepalive- the duration of time this connection is valid for reuse.tunit- the time unit.- See Also:
NHttpClientConnectionManager.closeExpiredConnections()
-
startRoute
public void startRoute(org.apache.http.nio.NHttpClientConnection managedConn, org.apache.http.conn.routing.HttpRoute route, org.apache.http.protocol.HttpContext context) throws IOExceptionDescription copied from interface:NHttpClientConnectionManagerStarts the process of connection initialization. Connection route may consist of several intermediate hops and may require a protocol upgrade. Once the route is fully established therouteCompletemethod must be called.- Specified by:
startRoutein interfaceNHttpClientConnectionManager- Parameters:
managedConn- the managed connection to initialize.route- the connection route.context- the context- Throws:
IOException
-
upgrade
public void upgrade(org.apache.http.nio.NHttpClientConnection managedConn, org.apache.http.conn.routing.HttpRoute route, org.apache.http.protocol.HttpContext context) throws IOExceptionDescription copied from interface:NHttpClientConnectionManagerUpgrades the underlying connection I/O session to TLS/SSL (or another layering protocol) after having executedCONNECTmethod to all intermediate proxy hops.- Specified by:
upgradein interfaceNHttpClientConnectionManager- Parameters:
managedConn- the managed connection to upgrade.route- the connection route.context- the context- Throws:
IOException
-
routeComplete
public void routeComplete(org.apache.http.nio.NHttpClientConnection managedConn, org.apache.http.conn.routing.HttpRoute route, org.apache.http.protocol.HttpContext context)Description copied from interface:NHttpClientConnectionManagerMarks the connection as fully established with all its intermediate hops completed.- Specified by:
routeCompletein interfaceNHttpClientConnectionManager- Parameters:
managedConn- the managed connection to mark as route complete.route- the connection route.context- the context
-
isRouteComplete
public boolean isRouteComplete(org.apache.http.nio.NHttpClientConnection managedConn)
Description copied from interface:NHttpClientConnectionManagerDetermines if the given connection has been fully established and marked as route complete.- Specified by:
isRouteCompletein interfaceNHttpClientConnectionManager- Parameters:
managedConn- the managed connection.
-
closeIdleConnections
public void closeIdleConnections(long idleTimeout, TimeUnit tunit)Description copied from interface:NHttpClientConnectionManagerCloses 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 interfaceNHttpClientConnectionManager- Parameters:
idleTimeout- the idle time of connections to be closedtunit- the unit for theidletime- See Also:
NHttpClientConnectionManager.closeExpiredConnections()
-
closeExpiredConnections
public void closeExpiredConnections()
Description copied from interface:NHttpClientConnectionManagerCloses 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 interfaceNHttpClientConnectionManager
-
validatePendingRequests
public void validatePendingRequests()
-
getMaxTotal
public int getMaxTotal()
- Specified by:
getMaxTotalin interfaceorg.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
-
setMaxTotal
public void setMaxTotal(int max)
- Specified by:
setMaxTotalin interfaceorg.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
-
getDefaultMaxPerRoute
public int getDefaultMaxPerRoute()
- Specified by:
getDefaultMaxPerRoutein interfaceorg.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
-
setDefaultMaxPerRoute
public void setDefaultMaxPerRoute(int max)
- Specified by:
setDefaultMaxPerRoutein interfaceorg.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
-
getMaxPerRoute
public int getMaxPerRoute(org.apache.http.conn.routing.HttpRoute route)
- Specified by:
getMaxPerRoutein interfaceorg.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
-
setMaxPerRoute
public void setMaxPerRoute(org.apache.http.conn.routing.HttpRoute route, int max)- Specified by:
setMaxPerRoutein interfaceorg.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
-
getTotalStats
public org.apache.http.pool.PoolStats getTotalStats()
- Specified by:
getTotalStatsin interfaceorg.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
-
getStats
public org.apache.http.pool.PoolStats getStats(org.apache.http.conn.routing.HttpRoute route)
- Specified by:
getStatsin interfaceorg.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
-
getRoutes
public Set<org.apache.http.conn.routing.HttpRoute> getRoutes()
- Since:
- 4.1
-
getDefaultConnectionConfig
public org.apache.http.config.ConnectionConfig getDefaultConnectionConfig()
-
setDefaultConnectionConfig
public void setDefaultConnectionConfig(org.apache.http.config.ConnectionConfig defaultConnectionConfig)
-
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)
-
-