Package org.apache.http.impl.conn.tsccm
Class AbstractConnPool
java.lang.Object
org.apache.http.impl.conn.tsccm.AbstractConnPool
- All Implemented Interfaces:
RefQueueHandler
- Direct Known Subclasses:
ConnPoolByRoute
public abstract class AbstractConnPool extends Object implements RefQueueHandler
An abstract connection pool.
It is used by the
ThreadSafeClientConnManager.
The abstract pool includes a poolLock, which is used to
synchronize access to the internal pool datastructures.
Don't use synchronized for that purpose!-
Field Summary
Fields Modifier and Type Field Description protected IdleConnectionHandleridleConnHandlerThe handler for idle connections.protected booleanisShutDownIndicates whether this pool is shut down.protected Set<BasicPoolEntryRef>issuedConnectionsReferences to issued connections.protected intnumConnectionsThe current total number of connections.protected LockpoolLockThe global lock for this pool.protected ReferenceQueue<Object>refQueueA reference queue to track loss of pool entries to GC. -
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractConnPool()Creates a new connection pool. -
Method Summary
Modifier and Type Method Description protected voidcloseConnection(OperatedClientConnection conn)Closes a connection from this pool.voidcloseExpiredConnections()voidcloseIdleConnections(long idletime, TimeUnit tunit)Closes idle connections.abstract voiddeleteClosedConnections()Deletes all entries for closed connections.voidenableConnectionGC()Enables connection garbage collection (GC).abstract voidfreeEntry(BasicPoolEntry entry, boolean reusable, long validDuration, TimeUnit timeUnit)Returns an entry into the pool.BasicPoolEntrygetEntry(HttpRoute route, Object state, long timeout, TimeUnit tunit)Obtains a pool entry with a connection within the given timeout.protected abstract voidhandleLostEntry(HttpRoute route)Handles cleaning up for a lost pool entry with the given route.voidhandleReference(Reference ref)Invoked when a reference is found on the queue.abstract 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.
-
Field Details
-
poolLock
The global lock for this pool. -
issuedConnections
References to issued connections. Objects in this set are of classBasicPoolEntryRef, and point to the pool entry for the issued connection. GCed connections are detected by the missing pool entries. -
idleConnHandler
The handler for idle connections. -
numConnections
protected int numConnectionsThe current total number of connections. -
refQueue
A reference queue to track loss of pool entries to GC. The same queue is used to track loss of the connection manager, so we cannot specialize the type. -
isShutDown
protected volatile boolean isShutDownIndicates whether this pool is shut down.
-
-
Constructor Details
-
AbstractConnPool
protected AbstractConnPool()Creates a new connection pool.
-
-
Method Details
-
enableConnectionGC
Enables connection garbage collection (GC). This method must be called immediately after creating the connection pool. It is not possible to enable connection GC after pool entries have been created. Neither is it possible to disable connection GC.- Throws:
IllegalStateException- if connection GC is already enabled, or if it cannot be enabled because there already are pool entries
-
getEntry
public final BasicPoolEntry getEntry(HttpRoute route, Object state, long timeout, TimeUnit tunit) throws ConnectionPoolTimeoutException, InterruptedExceptionObtains a pool entry with a connection within the given timeout.- 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 timeout- Returns:
- pool entry holding a connection for the route
- Throws:
ConnectionPoolTimeoutException- if the timeout expiredInterruptedException- if the calling thread was interrupted
-
requestPoolEntry
Returns a newPoolEntryRequest, from which aBasicPoolEntrycan be obtained, or the request can be aborted. -
freeEntry
public abstract void freeEntry(BasicPoolEntry entry, boolean reusable, long validDuration, TimeUnit timeUnit)Returns 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.- 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.
-
handleReference
Description copied from interface:RefQueueHandlerInvoked when a reference is found on the queue.- Specified by:
handleReferencein interfaceRefQueueHandler- Parameters:
ref- the reference to handle
-
handleLostEntry
Handles 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.- Parameters:
route- the route of the pool entry that was lost
-
closeIdleConnections
Closes idle connections.- Parameters:
idletime- the time the connections should have been idle in order to be closed nowtunit- the unit for theidletime
-
closeExpiredConnections
public void closeExpiredConnections() -
deleteClosedConnections
public abstract void deleteClosedConnections()Deletes all entries for closed connections. -
shutdown
public void shutdown()Shuts down this pool and all associated resources. Overriding methods MUST call the implementation here! -
closeConnection
Closes a connection from this pool.- Parameters:
conn- the connection to close, ornull
-