@ThreadSafe public class PoolingHttpClientConnectionManager extends Object implements HttpClientConnectionManager, ConnPoolControl<HttpRoute>, Closeable
HttpClientConnection
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.
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.Modifier and Type | Method and Description |
---|---|
void |
close() |
void |
closeExpiredConnections()
Closes all expired connections in the pool.
|
void |
closeIdleConnections(long idleTimeout,
TimeUnit tunit)
Closes idle connections in the pool.
|
void |
connect(HttpClientConnection managedConn,
HttpRoute route,
int connectTimeout,
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 |
finalize() |
org.apache.http.config.ConnectionConfig |
getConnectionConfig(HttpHost host) |
org.apache.http.config.ConnectionConfig |
getDefaultConnectionConfig() |
int |
getDefaultMaxPerRoute() |
org.apache.http.config.SocketConfig |
getDefaultSocketConfig() |
int |
getMaxPerRoute(HttpRoute route) |
int |
getMaxTotal() |
org.apache.http.config.SocketConfig |
getSocketConfig(HttpHost host) |
PoolStats |
getStats(HttpRoute route) |
PoolStats |
getTotalStats() |
protected HttpClientConnection |
leaseConnection(Future<org.apache.http.impl.conn.CPoolEntry> future,
long timeout,
TimeUnit tunit) |
void |
releaseConnection(HttpClientConnection managedConn,
Object state,
long keepalive,
TimeUnit tunit)
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(HttpClientConnection managedConn,
HttpRoute route,
HttpContext context)
Marks the connection as fully established with all its intermediate
hops completed.
|
void |
setConnectionConfig(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(HttpHost host,
org.apache.http.config.SocketConfig socketConfig) |
void |
shutdown()
Shuts down this connection manager and releases allocated resources.
|
void |
upgrade(HttpClientConnection managedConn,
HttpRoute route,
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 tunit)
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 tunit)
protected void finalize() throws Throwable
public void close()
close
in interface Closeable
close
in interface AutoCloseable
public ConnectionRequest requestConnection(HttpRoute route, Object state)
HttpClientConnectionManager
ConnectionRequest
, 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 HttpClientConnectionManager
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.protected HttpClientConnection leaseConnection(Future<org.apache.http.impl.conn.CPoolEntry> future, long timeout, TimeUnit tunit) throws InterruptedException, ExecutionException, ConnectionPoolTimeoutException
public void releaseConnection(HttpClientConnection managedConn, Object state, long keepalive, TimeUnit tunit)
HttpClientConnectionManager
validDuration
and timeUnit
parameters.releaseConnection
in interface HttpClientConnectionManager
managedConn
- the managed connection to release.keepalive
- the duration of time this connection is valid for reuse.tunit
- the time unit.HttpClientConnectionManager.closeExpiredConnections()
public void connect(HttpClientConnection managedConn, HttpRoute route, int connectTimeout, HttpContext context) throws IOException
HttpClientConnectionManager
connect
in interface HttpClientConnectionManager
managedConn
- the managed connection.route
- the route of the connection.connectTimeout
- connect timeout in milliseconds.context
- the actual HTTP context.IOException
public void upgrade(HttpClientConnection managedConn, HttpRoute route, HttpContext context) throws IOException
HttpClientConnectionManager
CONNECT
method to all
intermediate proxy hopsupgrade
in interface HttpClientConnectionManager
managedConn
- the managed connection.route
- the route of the connection.context
- the actual HTTP context.IOException
public void routeComplete(HttpClientConnection managedConn, HttpRoute route, HttpContext context) throws IOException
HttpClientConnectionManager
routeComplete
in interface HttpClientConnectionManager
managedConn
- the managed connection.route
- the route of the connection.context
- the actual HTTP context.IOException
public void shutdown()
HttpClientConnectionManager
shutdown
in interface HttpClientConnectionManager
public void closeIdleConnections(long idleTimeout, TimeUnit tunit)
HttpClientConnectionManager
closeIdleConnections
in interface HttpClientConnectionManager
idleTimeout
- the idle time of connections to be closedtunit
- the unit for the idletime
HttpClientConnectionManager.closeExpiredConnections()
public void closeExpiredConnections()
HttpClientConnectionManager
closeExpiredConnections
in interface HttpClientConnectionManager
public int getMaxTotal()
getMaxTotal
in interface ConnPoolControl<HttpRoute>
public void setMaxTotal(int max)
setMaxTotal
in interface ConnPoolControl<HttpRoute>
public int getDefaultMaxPerRoute()
getDefaultMaxPerRoute
in interface ConnPoolControl<HttpRoute>
public void setDefaultMaxPerRoute(int max)
setDefaultMaxPerRoute
in interface ConnPoolControl<HttpRoute>
public int getMaxPerRoute(HttpRoute route)
getMaxPerRoute
in interface ConnPoolControl<HttpRoute>
public void setMaxPerRoute(HttpRoute route, int max)
setMaxPerRoute
in interface ConnPoolControl<HttpRoute>
public PoolStats getTotalStats()
getTotalStats
in interface ConnPoolControl<HttpRoute>
public PoolStats getStats(HttpRoute route)
getStats
in interface 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(HttpHost host)
public void setSocketConfig(HttpHost host, org.apache.http.config.SocketConfig socketConfig)
public org.apache.http.config.ConnectionConfig getConnectionConfig(HttpHost host)
public void setConnectionConfig(HttpHost host, org.apache.http.config.ConnectionConfig connectionConfig)
Copyright © 1999–2013 The Apache Software Foundation. All rights reserved.