@Contract(threading=SAFE) public class PoolingNHttpClientConnectionManager extends java.lang.Object implements NHttpClientConnectionManager, org.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
PoolingNHttpClientConnectionManager
maintains a pool of
NHttpClientConnection
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 using ConnPoolControl
methods.
Modifier and Type | Class and Description |
---|---|
(package private) static class |
PoolingNHttpClientConnectionManager.ConfigData |
(package private) static class |
PoolingNHttpClientConnectionManager.InternalAddressResolver |
(package private) static class |
PoolingNHttpClientConnectionManager.InternalConnectionFactory |
Modifier and Type | Field and Description |
---|---|
private PoolingNHttpClientConnectionManager.ConfigData |
configData |
private org.apache.http.nio.reactor.ConnectingIOReactor |
ioreactor |
(package private) static java.lang.String |
IOSESSION_FACTORY_REGISTRY |
private org.apache.http.config.Registry<SchemeIOSessionStrategy> |
iosessionFactoryRegistry |
private org.apache.commons.logging.Log |
log |
private CPool |
pool |
Constructor and Description |
---|
PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor) |
PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor,
CPool pool,
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.conn.DnsResolver dnsResolver) |
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,
java.util.concurrent.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,
java.util.concurrent.TimeUnit tunit) |
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(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor,
org.apache.http.config.Registry<SchemeIOSessionStrategy> iosessionFactoryRegistry) |
Modifier and Type | Method and Description |
---|---|
void |
closeExpiredConnections()
Closes all expired connections in the pool.
|
void |
closeIdleConnections(long idleTimeout,
java.util.concurrent.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 given
IOEventDispatch . |
protected void |
finalize() |
private java.lang.String |
format(CPoolEntry entry) |
private java.lang.String |
format(org.apache.http.conn.routing.HttpRoute route,
java.lang.Object state) |
private java.lang.String |
formatStats(org.apache.http.conn.routing.HttpRoute route) |
org.apache.http.config.ConnectionConfig |
getConnectionConfig(org.apache.http.HttpHost host) |
org.apache.http.config.ConnectionConfig |
getDefaultConnectionConfig() |
int |
getDefaultMaxPerRoute() |
private static org.apache.http.config.Registry<SchemeIOSessionStrategy> |
getDefaultRegistry() |
private org.apache.http.config.Lookup<SchemeIOSessionStrategy> |
getIOSessionFactoryRegistry(org.apache.http.protocol.HttpContext context) |
int |
getMaxPerRoute(org.apache.http.conn.routing.HttpRoute route) |
int |
getMaxTotal() |
java.util.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,
java.lang.Object state,
long keepalive,
java.util.concurrent.TimeUnit tunit)
Releases the connection back to the manager making it potentially
re-usable by other consumers.
|
java.util.concurrent.Future<org.apache.http.nio.NHttpClientConnection> |
requestConnection(org.apache.http.conn.routing.HttpRoute route,
java.lang.Object state,
long connectTimeout,
long leaseTimeout,
java.util.concurrent.TimeUnit tunit,
org.apache.http.concurrent.FutureCallback<org.apache.http.nio.NHttpClientConnection> callback)
Returns a
Future for a NHttpClientConnection . |
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 executed
CONNECT method to all
intermediate proxy hops. |
void |
validatePendingRequests() |
private final org.apache.commons.logging.Log log
static final java.lang.String IOSESSION_FACTORY_REGISTRY
private final org.apache.http.nio.reactor.ConnectingIOReactor ioreactor
private final PoolingNHttpClientConnectionManager.ConfigData configData
private final CPool pool
private final org.apache.http.config.Registry<SchemeIOSessionStrategy> iosessionFactoryRegistry
public PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor)
public PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor, org.apache.http.config.Registry<SchemeIOSessionStrategy> iosessionFactoryRegistry)
public PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor, NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory, org.apache.http.conn.DnsResolver dnsResolver)
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)
public PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor, NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory)
public PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor, NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory, org.apache.http.config.Registry<SchemeIOSessionStrategy> iosessionFactoryRegistry)
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)
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)
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, java.util.concurrent.TimeUnit tunit)
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, java.util.concurrent.TimeUnit tunit)
PoolingNHttpClientConnectionManager(org.apache.http.nio.reactor.ConnectingIOReactor ioreactor, CPool pool, org.apache.http.config.Registry<SchemeIOSessionStrategy> iosessionFactoryRegistry)
private static org.apache.http.config.Registry<SchemeIOSessionStrategy> getDefaultRegistry()
protected void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable
public void execute(org.apache.http.nio.reactor.IOEventDispatch eventDispatch) throws java.io.IOException
NHttpClientConnectionManager
IOEventDispatch
.execute
in interface NHttpClientConnectionManager
java.io.IOException
public void shutdown(long waitMs) throws java.io.IOException
java.io.IOException
public void shutdown() throws java.io.IOException
NHttpClientConnectionManager
shutdown
in interface NHttpClientConnectionManager
java.io.IOException
private java.lang.String format(org.apache.http.conn.routing.HttpRoute route, java.lang.Object state)
private java.lang.String formatStats(org.apache.http.conn.routing.HttpRoute route)
private java.lang.String format(CPoolEntry entry)
public java.util.concurrent.Future<org.apache.http.nio.NHttpClientConnection> requestConnection(org.apache.http.conn.routing.HttpRoute route, java.lang.Object state, long connectTimeout, long leaseTimeout, java.util.concurrent.TimeUnit tunit, org.apache.http.concurrent.FutureCallback<org.apache.http.nio.NHttpClientConnection> callback)
NHttpClientConnectionManager
Future
for a NHttpClientConnection
.
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 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 NHttpClientConnectionManager
route
- HTTP route of the requested connection.state
- expected state of the connection or null
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.public void releaseConnection(org.apache.http.nio.NHttpClientConnection managedConn, java.lang.Object state, long keepalive, java.util.concurrent.TimeUnit tunit)
NHttpClientConnectionManager
validDuration
and timeUnit
parameters.releaseConnection
in interface NHttpClientConnectionManager
managedConn
- the managed connection to release.keepalive
- the duration of time this connection is valid for reuse.tunit
- the time unit.NHttpClientConnectionManager.closeExpiredConnections()
private org.apache.http.config.Lookup<SchemeIOSessionStrategy> getIOSessionFactoryRegistry(org.apache.http.protocol.HttpContext context)
public void startRoute(org.apache.http.nio.NHttpClientConnection managedConn, org.apache.http.conn.routing.HttpRoute route, org.apache.http.protocol.HttpContext context) throws java.io.IOException
NHttpClientConnectionManager
routeComplete
method must be called.startRoute
in interface NHttpClientConnectionManager
managedConn
- the managed connection to initialize.route
- the connection route.context
- the contextjava.io.IOException
public void upgrade(org.apache.http.nio.NHttpClientConnection managedConn, org.apache.http.conn.routing.HttpRoute route, org.apache.http.protocol.HttpContext context) throws java.io.IOException
NHttpClientConnectionManager
CONNECT
method to all
intermediate proxy hops.upgrade
in interface NHttpClientConnectionManager
managedConn
- the managed connection to upgrade.route
- the connection route.context
- the contextjava.io.IOException
public void routeComplete(org.apache.http.nio.NHttpClientConnection managedConn, org.apache.http.conn.routing.HttpRoute route, org.apache.http.protocol.HttpContext context)
NHttpClientConnectionManager
routeComplete
in interface NHttpClientConnectionManager
managedConn
- the managed connection to mark as route complete.route
- the connection route.context
- the contextpublic boolean isRouteComplete(org.apache.http.nio.NHttpClientConnection managedConn)
NHttpClientConnectionManager
isRouteComplete
in interface NHttpClientConnectionManager
managedConn
- the managed connection.public void closeIdleConnections(long idleTimeout, java.util.concurrent.TimeUnit tunit)
NHttpClientConnectionManager
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.
closeIdleConnections
in interface NHttpClientConnectionManager
idleTimeout
- the idle time of connections to be closedtunit
- the unit for the idletime
NHttpClientConnectionManager.closeExpiredConnections()
public void closeExpiredConnections()
NHttpClientConnectionManager
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.
closeExpiredConnections
in interface NHttpClientConnectionManager
public void validatePendingRequests()
public int getMaxTotal()
getMaxTotal
in interface org.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
public void setMaxTotal(int max)
setMaxTotal
in interface org.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
public int getDefaultMaxPerRoute()
getDefaultMaxPerRoute
in interface org.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
public void setDefaultMaxPerRoute(int max)
setDefaultMaxPerRoute
in interface org.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
public int getMaxPerRoute(org.apache.http.conn.routing.HttpRoute route)
getMaxPerRoute
in interface org.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
public void setMaxPerRoute(org.apache.http.conn.routing.HttpRoute route, int max)
setMaxPerRoute
in interface org.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
public org.apache.http.pool.PoolStats getTotalStats()
getTotalStats
in interface org.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
public org.apache.http.pool.PoolStats getStats(org.apache.http.conn.routing.HttpRoute route)
getStats
in interface org.apache.http.pool.ConnPoolControl<org.apache.http.conn.routing.HttpRoute>
public java.util.Set<org.apache.http.conn.routing.HttpRoute> getRoutes()
public org.apache.http.config.ConnectionConfig getDefaultConnectionConfig()
public void setDefaultConnectionConfig(org.apache.http.config.ConnectionConfig defaultConnectionConfig)
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)