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>
PoolingNHttpClientConnectionManager
maintains a pool ofNHttpClientConnection
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 services by leasing a connection from the pool rather than creating a brand new connection.PoolingNHttpClientConnectionManager
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 usingConnPoolControl
methods.- 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 void
closeExpiredConnections()
Closes all expired connections in the pool.void
closeIdleConnections(long idleTimeout, TimeUnit tunit)
Closes idle connections in the pool.void
execute(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 void
finalize()
org.apache.http.config.ConnectionConfig
getConnectionConfig(org.apache.http.HttpHost host)
org.apache.http.config.ConnectionConfig
getDefaultConnectionConfig()
int
getDefaultMaxPerRoute()
int
getMaxPerRoute(org.apache.http.conn.routing.HttpRoute route)
int
getMaxTotal()
Set<org.apache.http.conn.routing.HttpRoute>
getRoutes()
org.apache.http.pool.PoolStats
getStats(org.apache.http.conn.routing.HttpRoute route)
org.apache.http.pool.PoolStats
getTotalStats()
boolean
isRouteComplete(org.apache.http.nio.NHttpClientConnection managedConn)
Determines if the given connection has been fully established and marked as route complete.void
releaseConnection(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 aFuture
for aNHttpClientConnection
.void
routeComplete(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.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
setMaxPerRoute(org.apache.http.conn.routing.HttpRoute route, int max)
void
setMaxTotal(int max)
void
shutdown()
Shuts down this connection manager and releases allocated resources.void
shutdown(long waitMs)
void
startRoute(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.void
upgrade(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 executedCONNECT
method to all intermediate proxy hops.void
validatePendingRequests()
-
-
-
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 IOException
Description copied from interface:NHttpClientConnectionManager
Starts the underlying I/O reactor and initiates the dispatch of I/O event notifications to the givenIOEventDispatch
.- Specified by:
execute
in interfaceNHttpClientConnectionManager
- Throws:
IOException
-
shutdown
public void shutdown(long waitMs) throws IOException
- Throws:
IOException
-
shutdown
public void shutdown() throws IOException
Description copied from interface:NHttpClientConnectionManager
Shuts down this connection manager and releases allocated resources. This includes closing all connections, whether they are currently used or not.- Specified by:
shutdown
in 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:NHttpClientConnectionManager
Returns aFuture
for aNHttpClientConnection
.Please note that the consumer of that connection is responsible for fully establishing the route the to the connection target by calling
startRoute
in order to start the process of connection initialization, optionally callingupgrade
method to upgrade the connection after having executedCONNECT
method to all intermediate proxy hops and and finally callingrouteComplete
to mark the route as fully completed.- Specified by:
requestConnection
in interfaceNHttpClientConnectionManager
- Parameters:
route
- HTTP route of the requested connection.state
- expected state of the connection ornull
if 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:NHttpClientConnectionManager
Releases 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 usingvalidDuration
andtimeUnit
parameters.- Specified by:
releaseConnection
in 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 IOException
Description copied from interface:NHttpClientConnectionManager
Starts 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 therouteComplete
method must be called.- Specified by:
startRoute
in 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 IOException
Description copied from interface:NHttpClientConnectionManager
Upgrades the underlying connection I/O session to TLS/SSL (or another layering protocol) after having executedCONNECT
method to all intermediate proxy hops.- Specified by:
upgrade
in 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:NHttpClientConnectionManager
Marks the connection as fully established with all its intermediate hops completed.- Specified by:
routeComplete
in 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:NHttpClientConnectionManager
Determines if the given connection has been fully established and marked as route complete.- Specified by:
isRouteComplete
in interfaceNHttpClientConnectionManager
- Parameters:
managedConn
- the managed connection.
-
closeIdleConnections
public void closeIdleConnections(long idleTimeout, TimeUnit tunit)
Description copied from interface:NHttpClientConnectionManager
Closes 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:
closeIdleConnections
in 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:NHttpClientConnectionManager
Closes 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:
closeExpiredConnections
in interfaceNHttpClientConnectionManager
-
validatePendingRequests
public void validatePendingRequests()
-
getMaxTotal
public int getMaxTotal()
- Specified by:
getMaxTotal
in interfaceorg.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
-
setMaxTotal
public void setMaxTotal(int max)
- Specified by:
setMaxTotal
in interfaceorg.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
-
getDefaultMaxPerRoute
public int getDefaultMaxPerRoute()
- Specified by:
getDefaultMaxPerRoute
in interfaceorg.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
-
setDefaultMaxPerRoute
public void setDefaultMaxPerRoute(int max)
- Specified by:
setDefaultMaxPerRoute
in interfaceorg.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
-
getMaxPerRoute
public int getMaxPerRoute(org.apache.http.conn.routing.HttpRoute route)
- Specified by:
getMaxPerRoute
in 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:
setMaxPerRoute
in interfaceorg.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
-
getTotalStats
public org.apache.http.pool.PoolStats getTotalStats()
- Specified by:
getTotalStats
in 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:
getStats
in 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)
-
-