Package org.apache.http.impl.conn.tsccm
Class ConnPoolByRoute
java.lang.Object
org.apache.http.impl.conn.tsccm.AbstractConnPool
org.apache.http.impl.conn.tsccm.ConnPoolByRoute
- All Implemented Interfaces:
RefQueueHandler
public class ConnPoolByRoute extends AbstractConnPool
A connection pool that maintains connections by route.
This class is derived from
MultiThreadedHttpConnectionManager
in HttpClient 3.x, see there for original authors. It implements the same
algorithm for connection re-use and connection-per-host enforcement:
- connections are re-used only for the exact same route
- connection limits are enforced per route rather than per host
poolLock in the base class,
not via synchronized methods.- Author:
- Roland Weber, Michael Becke, and others
-
Field Summary
Fields Modifier and Type Field Description protected Queue<BasicPoolEntry>freeConnectionsThe list of free connectionsprotected intmaxTotalConnectionsprotected ClientConnectionOperatoroperatorConnection operator for this poolprotected Map<HttpRoute,RouteSpecificPool>routeToPoolA map of route-specific pools.protected Queue<WaitingThread>waitingThreadsThe list of WaitingThreads waiting for a connectionFields inherited from class org.apache.http.impl.conn.tsccm.AbstractConnPool
idleConnHandler, isShutDown, issuedConnections, numConnections, poolLock, refQueue -
Constructor Summary
Constructors Constructor Description ConnPoolByRoute(ClientConnectionOperator operator, HttpParams params)Creates a new connection pool, managed by route. -
Method Summary
Modifier and Type Method Description protected BasicPoolEntrycreateEntry(RouteSpecificPool rospl, ClientConnectionOperator op)Creates a new pool entry.protected Queue<BasicPoolEntry>createFreeConnQueue()Creates the queue forfreeConnections.protected Map<HttpRoute,RouteSpecificPool>createRouteToPoolMap()Creates the map forrouteToPool.protected Queue<WaitingThread>createWaitingThreadQueue()Creates the queue forwaitingThreads.voiddeleteClosedConnections()Deletes all entries for closed connections.protected voiddeleteEntry(BasicPoolEntry entry)Deletes a given pool entry.protected voiddeleteLeastUsedEntry()Delete an old, free pool entry to make room for a new one.voidfreeEntry(BasicPoolEntry entry, boolean reusable, long validDuration, TimeUnit timeUnit)Returns an entry into the pool.intgetConnectionsInPool(HttpRoute route)protected BasicPoolEntrygetEntryBlocking(HttpRoute route, Object state, long timeout, TimeUnit tunit, WaitingThreadAborter aborter)Obtains a pool entry with a connection within the given timeout.protected BasicPoolEntrygetFreeEntry(RouteSpecificPool rospl, Object state)If available, get a free pool entry for a route.protected RouteSpecificPoolgetRoutePool(HttpRoute route, boolean create)Get a route-specific pool of available connections.protected voidhandleLostEntry(HttpRoute route)Handles cleaning up for a lost pool entry with the given route.protected RouteSpecificPoolnewRouteSpecificPool(HttpRoute route)Creates a new route-specific pool.protected WaitingThreadnewWaitingThread(Condition cond, RouteSpecificPool rospl)Creates a new waiting thread.protected voidnotifyWaitingThread(RouteSpecificPool rospl)Notifies a waiting thread that a connection is available.PoolEntryRequestrequestPoolEntry(HttpRoute route, Object state)Returns a newPoolEntryRequest, from which aBasicPoolEntrycan be obtained, or the request can be aborted.voidshutdown()Shuts down this pool and all associated resources.Methods inherited from class org.apache.http.impl.conn.tsccm.AbstractConnPool
closeConnection, closeExpiredConnections, closeIdleConnections, enableConnectionGC, getEntry, handleReference
-
Field Details
-
operator
Connection operator for this pool -
freeConnections
The list of free connections -
waitingThreads
The list of WaitingThreads waiting for a connection -
routeToPool
A map of route-specific pools. Keys are of classHttpRoute, values of classRouteSpecificPool. -
maxTotalConnections
protected final int maxTotalConnections
-
-
Constructor Details
-
ConnPoolByRoute
Creates a new connection pool, managed by route.
-
-
Method Details
-
createFreeConnQueue
Creates the queue forfreeConnections. Called once by the constructor.- Returns:
- a queue
-
createWaitingThreadQueue
Creates the queue forwaitingThreads. Called once by the constructor.- Returns:
- a queue
-
createRouteToPoolMap
Creates the map forrouteToPool. Called once by the constructor.- Returns:
- a map
-
newRouteSpecificPool
Creates a new route-specific pool. Called bygetRoutePool(org.apache.http.conn.routing.HttpRoute, boolean)when necessary.- Parameters:
route- the route- Returns:
- the new pool
-
newWaitingThread
Creates a new waiting thread. Called bygetRoutePool(org.apache.http.conn.routing.HttpRoute, boolean)when necessary.- Parameters:
cond- the condition to wait forrospl- the route specific pool, ornull- Returns:
- a waiting thread representation
-
getRoutePool
Get a route-specific pool of available connections.- Parameters:
route- the routecreate- whether to create the pool if it doesn't exist- Returns:
- the pool for the argument route,
never
nullifcreateistrue
-
getConnectionsInPool
-
requestPoolEntry
Description copied from class:AbstractConnPoolReturns a newPoolEntryRequest, from which aBasicPoolEntrycan be obtained, or the request can be aborted.- Specified by:
requestPoolEntryin classAbstractConnPool
-
getEntryBlocking
protected BasicPoolEntry getEntryBlocking(HttpRoute route, Object state, long timeout, TimeUnit tunit, WaitingThreadAborter aborter) throws ConnectionPoolTimeoutException, InterruptedExceptionObtains a pool entry with a connection within the given timeout. If aWaitingThreadis used to block,WaitingThreadAborter.setWaitingThread(WaitingThread)must be called before blocking, to allow the thread to be interrupted.- Parameters:
route- the route for which to get the connectiontimeout- the timeout, 0 or negative for no timeouttunit- the unit for thetimeout, may benullonly if there is no timeoutaborter- an object which can abort aWaitingThread.- Returns:
- pool entry holding a connection for the route
- Throws:
ConnectionPoolTimeoutException- if the timeout expiredInterruptedException- if the calling thread was interrupted
-
freeEntry
public void freeEntry(BasicPoolEntry entry, boolean reusable, long validDuration, TimeUnit timeUnit)Description copied from class:AbstractConnPoolReturns an entry into the pool. The connection of the entry is expected to be in a suitable state, either open and re-usable, or closed. The pool will not make any attempt to determine whether it can be re-used or not.- Specified by:
freeEntryin classAbstractConnPool- Parameters:
entry- the entry for the connection to releasereusable-trueif the entry is deemed reusable,falseotherwise.validDuration- The duration that the entry should remain free and reusable.timeUnit- The unit of time the duration is measured in.
-
getFreeEntry
If available, get a free pool entry for a route.- Parameters:
rospl- the route-specific pool from which to get an entry- Returns:
- an available pool entry for the given route, or
nullif none is available
-
createEntry
Creates a new pool entry. This method assumes that the new connection will be handed out immediately.- Parameters:
rospl- the route-specific pool for which to create the entryop- the operator for creating a connection- Returns:
- the new pool entry for a new connection
-
deleteEntry
Deletes a given pool entry. This closes the pooled connection and removes all references, so that it can be GCed.Note: Does not remove the entry from the freeConnections list. It is assumed that the caller has already handled this step.
- Parameters:
entry- the pool entry for the connection to delete
-
deleteLeastUsedEntry
protected void deleteLeastUsedEntry()Delete an old, free pool entry to make room for a new one. Used to replace pool entries with ones for a different route. -
handleLostEntry
Description copied from class:AbstractConnPoolHandles cleaning up for a lost pool entry with the given route. A lost pool entry corresponds to a connection that was garbage collected instead of being properly released.- Specified by:
handleLostEntryin classAbstractConnPool- Parameters:
route- the route of the pool entry that was lost
-
notifyWaitingThread
Notifies a waiting thread that a connection is available. This will wake a thread waiting in the specific route pool, if there is one. Otherwise, a thread in the connection pool will be notified.- Parameters:
rospl- the pool in which to notify, ornull
-
deleteClosedConnections
public void deleteClosedConnections()Description copied from class:AbstractConnPoolDeletes all entries for closed connections.- Specified by:
deleteClosedConnectionsin classAbstractConnPool
-
shutdown
public void shutdown()Description copied from class:AbstractConnPoolShuts down this pool and all associated resources. Overriding methods MUST call the implementation here!- Overrides:
shutdownin classAbstractConnPool
-