Class AbstractConnPool<T,C,E extends PoolEntry<T,C>>

java.lang.Object
org.apache.http.pool.AbstractConnPool<T,C,E>
Type Parameters:
T - the route type that represents the opposite endpoint of a pooled connection.
C - the connection type.
E - the type of the pool entry containing a pooled connection.
All Implemented Interfaces:
ConnPool<T,E>, ConnPoolControl<T>
Direct Known Subclasses:
BasicConnPool

@Contract(threading=SAFE_CONDITIONAL) public abstract class AbstractConnPool<T,C,E extends PoolEntry<T,C>> extends Object implements ConnPool<T,E>, ConnPoolControl<T>
Abstract synchronous (blocking) pool of connections.

Please note that this class does not maintain its own pool of execution Threads. Therefore, one must call Future.get() or Future.get(long, TimeUnit) method on the Future object returned by the lease(Object, Object, FutureCallback) method in order for the lease operation to complete.

Since:
4.2
  • Field Details

    • lock

      private final Lock lock
    • condition

      private final Condition condition
    • connFactory

      private final ConnFactory<T,C> connFactory
    • routeToPool

      private final Map<T,RouteSpecificPool<T,C,E extends PoolEntry<T,C>>> routeToPool
    • leased

      private final Set<E extends PoolEntry<T,C>> leased
    • available

      private final LinkedList<E extends PoolEntry<T,C>> available
    • pending

      private final LinkedList<Future<E extends PoolEntry<T,C>>> pending
    • maxPerRoute

      private final Map<T,Integer> maxPerRoute
    • isShutDown

      private volatile boolean isShutDown
    • defaultMaxPerRoute

      private volatile int defaultMaxPerRoute
    • maxTotal

      private volatile int maxTotal
    • validateAfterInactivity

      private volatile int validateAfterInactivity
  • Constructor Details

    • AbstractConnPool

      public AbstractConnPool(ConnFactory<T,C> connFactory, int defaultMaxPerRoute, int maxTotal)
  • Method Details

    • createEntry

      protected abstract E createEntry(T route, C conn)
      Creates a new entry for the given connection with the given route.
    • onLease

      protected void onLease(E entry)
      Since:
      4.3
    • onRelease

      protected void onRelease(E entry)
      Since:
      4.3
    • onReuse

      protected void onReuse(E entry)
      Since:
      4.4
    • validate

      protected boolean validate(E entry)
      Since:
      4.4
    • isShutdown

      public boolean isShutdown()
    • shutdown

      public void shutdown() throws IOException
      Shuts down the pool.
      Throws:
      IOException
    • getPool

      private RouteSpecificPool<T,C,E> getPool(T route)
    • operationAborted

      private static Exception operationAborted()
    • lease

      public Future<E> lease(T route, Object state, FutureCallback<E> callback)
      Attempts to lease a connection for the given route and with the given state from the pool.

      Please note that this class does not maintain its own pool of execution Threads. Therefore, one must call Future.get() or Future.get(long, TimeUnit) method on the Future returned by this method in order for the lease operation to complete.

      Specified by:
      lease in interface ConnPool<T,C>
      Parameters:
      route - route of the connection.
      state - arbitrary object that represents a particular state (usually a security principal or a unique token identifying the user whose credentials have been used while establishing the connection). May be null.
      callback - operation completion callback.
      Returns:
      future for a leased pool entry.
    • lease

      public Future<E> lease(T route, Object state)
      Attempts to lease a connection for the given route and with the given state from the pool.

      Please note that this class does not maintain its own pool of execution Threads. Therefore, one must call Future.get() or Future.get(long, TimeUnit) method on the Future returned by this method in order for the lease operation to complete.

      Parameters:
      route - route of the connection.
      state - arbitrary object that represents a particular state (usually a security principal or a unique token identifying the user whose credentials have been used while establishing the connection). May be null.
      Returns:
      future for a leased pool entry.
    • getPoolEntryBlocking

      private E getPoolEntryBlocking(T route, Object state, long timeout, TimeUnit timeUnit, Future<E> future) throws IOException, InterruptedException, ExecutionException, TimeoutException
      Throws:
      IOException
      InterruptedException
      ExecutionException
      TimeoutException
    • release

      public void release(E entry, boolean reusable)
      Description copied from interface: ConnPool
      Releases the pool entry back to the pool.
      Specified by:
      release in interface ConnPool<T,C>
      Parameters:
      entry - pool entry leased from the pool
      reusable - flag indicating whether or not the released connection is in a consistent state and is safe for further use.
    • getMax

      private int getMax(T route)
    • setMaxTotal

      public void setMaxTotal(int max)
      Specified by:
      setMaxTotal in interface ConnPoolControl<T>
    • getMaxTotal

      public int getMaxTotal()
      Specified by:
      getMaxTotal in interface ConnPoolControl<T>
    • setDefaultMaxPerRoute

      public void setDefaultMaxPerRoute(int max)
      Specified by:
      setDefaultMaxPerRoute in interface ConnPoolControl<T>
    • getDefaultMaxPerRoute

      public int getDefaultMaxPerRoute()
      Specified by:
      getDefaultMaxPerRoute in interface ConnPoolControl<T>
    • setMaxPerRoute

      public void setMaxPerRoute(T route, int max)
      Specified by:
      setMaxPerRoute in interface ConnPoolControl<T>
    • getMaxPerRoute

      public int getMaxPerRoute(T route)
      Specified by:
      getMaxPerRoute in interface ConnPoolControl<T>
    • getTotalStats

      public PoolStats getTotalStats()
      Specified by:
      getTotalStats in interface ConnPoolControl<T>
    • getStats

      public PoolStats getStats(T route)
      Specified by:
      getStats in interface ConnPoolControl<T>
    • getRoutes

      public Set<T> getRoutes()
      Returns snapshot of all knows routes
      Returns:
      the set of routes
      Since:
      4.4
    • enumAvailable

      protected void enumAvailable(PoolEntryCallback<T,C> callback)
      Enumerates all available connections.
      Since:
      4.3
    • enumLeased

      protected void enumLeased(PoolEntryCallback<T,C> callback)
      Enumerates all leased connections.
      Since:
      4.3
    • purgePoolMap

      private void purgePoolMap()
    • closeIdle

      public void closeIdle(long idletime, TimeUnit timeUnit)
      Closes connections that have been idle longer than the given period of time and evicts them from the pool.
      Parameters:
      idletime - maximum idle time.
      timeUnit - time unit.
    • closeExpired

      public void closeExpired()
      Closes expired connections and evicts them from the pool.
    • getValidateAfterInactivity

      public int getValidateAfterInactivity()
      Returns:
      the number of milliseconds
      Since:
      4.4
    • setValidateAfterInactivity

      public void setValidateAfterInactivity(int ms)
      Parameters:
      ms - the number of milliseconds
      Since:
      4.4
    • toString

      public String toString()
      Overrides:
      toString in class Object