public class

ConnPoolByRoute

extends AbstractConnPool
java.lang.Object
   ↳ org.apache.http.impl.conn.tsccm.AbstractConnPool
     ↳ org.apache.http.impl.conn.tsccm.ConnPoolByRoute

Class Overview

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
Note that access to the pool data structures is synchronized via the poolLock in the base class, not via synchronized methods.

Summary

Fields
protected final ConnPerRoute connPerRoute Connections per route lookup
protected final Queue<BasicPoolEntry> freeConnections The list of free connections
protected final Set<BasicPoolEntry> leasedConnections References to issued connections
protected int maxTotalConnections
protected int numConnections
protected final ClientConnectionOperator operator Connection operator for this pool
protected final Map<HttpRouteRouteSpecificPool> routeToPool Map of route-specific pools
protected boolean shutdown
protected final Queue<WaitingThread> waitingThreads The list of WaitingThreads waiting for a connection
[Expand]
Inherited Fields
From class org.apache.http.impl.conn.tsccm.AbstractConnPool
Public Constructors
ConnPoolByRoute(ClientConnectionOperator operator, ConnPerRoute connPerRoute, int maxTotalConnections)
Creates a new connection pool, managed by route.
ConnPoolByRoute(ClientConnectionOperator operator, ConnPerRoute connPerRoute, int maxTotalConnections, long connTTL, TimeUnit connTTLTimeUnit)
ConnPoolByRoute(ClientConnectionOperator operator, HttpParams params)
Public Methods
void closeExpiredConnections()
void closeIdleConnections(long idletime, TimeUnit tunit)
Closes idle connections.
void deleteClosedConnections()
Deletes all entries for closed connections.
void freeEntry(BasicPoolEntry entry, boolean reusable, long validDuration, TimeUnit timeUnit)
Returns an entry into the pool.
int getConnectionsInPool(HttpRoute route)
int getConnectionsInPool()
int getMaxTotalConnections()
since 4.1
PoolEntryRequest requestPoolEntry(HttpRoute route, Object state)
Returns a new PoolEntryRequest, from which a BasicPoolEntry can be obtained, or the request can be aborted.
void setMaxTotalConnections(int max)
since 4.1
void shutdown()
Shuts down this pool and all associated resources.
Protected Methods
BasicPoolEntry createEntry(RouteSpecificPool rospl, ClientConnectionOperator op)
Creates a new pool entry.
Queue<BasicPoolEntry> createFreeConnQueue()
Creates the queue for freeConnections.
Map<HttpRouteRouteSpecificPool> createRouteToPoolMap()
Creates the map for routeToPool.
Queue<WaitingThread> createWaitingThreadQueue()
Creates the queue for waitingThreads.
void deleteEntry(BasicPoolEntry entry)
Deletes a given pool entry.
void deleteLeastUsedEntry()
Delete an old, free pool entry to make room for a new one.
BasicPoolEntry getEntryBlocking(HttpRoute route, Object state, long timeout, TimeUnit tunit, WaitingThreadAborter aborter)
Obtains a pool entry with a connection within the given timeout.
BasicPoolEntry getFreeEntry(RouteSpecificPool rospl, Object state)
If available, get a free pool entry for a route.
Lock getLock()
RouteSpecificPool getRoutePool(HttpRoute route, boolean create)
Get a route-specific pool of available connections.
void handleLostEntry(HttpRoute route)
RouteSpecificPool newRouteSpecificPool(HttpRoute route)
Creates a new route-specific pool.
WaitingThread newWaitingThread(Condition cond, RouteSpecificPool rospl)
Creates a new waiting thread.
void notifyWaitingThread(RouteSpecificPool rospl)
Notifies a waiting thread that a connection is available.
[Expand]
Inherited Methods
From class org.apache.http.impl.conn.tsccm.AbstractConnPool
From class java.lang.Object
From interface org.apache.http.impl.conn.tsccm.RefQueueHandler

Fields

protected final ConnPerRoute connPerRoute

Also: HttpClient

Connections per route lookup

protected final Queue<BasicPoolEntry> freeConnections

Also: HttpClient

The list of free connections

protected final Set<BasicPoolEntry> leasedConnections

Also: HttpClient

References to issued connections

protected int maxTotalConnections

Also: HttpClient

protected int numConnections

Also: HttpClient

protected final ClientConnectionOperator operator

Also: HttpClient

Connection operator for this pool

protected final Map<HttpRouteRouteSpecificPool> routeToPool

Also: HttpClient

Map of route-specific pools

protected boolean shutdown

Also: HttpClient

protected final Queue<WaitingThread> waitingThreads

Also: HttpClient

The list of WaitingThreads waiting for a connection

Public Constructors

public ConnPoolByRoute (ClientConnectionOperator operator, ConnPerRoute connPerRoute, int maxTotalConnections)

Also: HttpClient

Creates a new connection pool, managed by route.

public ConnPoolByRoute (ClientConnectionOperator operator, ConnPerRoute connPerRoute, int maxTotalConnections, long connTTL, TimeUnit connTTLTimeUnit)

Also: HttpClient

public ConnPoolByRoute (ClientConnectionOperator operator, HttpParams params)

Also: HttpClient

This constructor is deprecated.
use ConnPoolByRoute(ClientConnectionOperator, ConnPerRoute, int)

Creates a new connection pool, managed by route.

Public Methods

public void closeExpiredConnections ()

Also: HttpClient

public void closeIdleConnections (long idletime, TimeUnit tunit)

Also: HttpClient

Closes idle connections.

Parameters
idletime the time the connections should have been idle in order to be closed now
tunit the unit for the idletime

public void deleteClosedConnections ()

Also: HttpClient

Deletes all entries for closed connections.

public void freeEntry (BasicPoolEntry entry, boolean reusable, long validDuration, TimeUnit timeUnit)

Also: HttpClient

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 release
reusable true if the entry is deemed reusable, false otherwise.
validDuration The duration that the entry should remain free and reusable.
timeUnit The unit of time the duration is measured in.

public int getConnectionsInPool (HttpRoute route)

Also: HttpClient

public int getConnectionsInPool ()

Also: HttpClient

public int getMaxTotalConnections ()

Also: HttpClient

since 4.1

public PoolEntryRequest requestPoolEntry (HttpRoute route, Object state)

Also: HttpClient

Returns a new PoolEntryRequest, from which a BasicPoolEntry can be obtained, or the request can be aborted.

public void setMaxTotalConnections (int max)

Also: HttpClient

since 4.1

public void shutdown ()

Also: HttpClient

Shuts down this pool and all associated resources. Overriding methods MUST call the implementation here!

Protected Methods

protected BasicPoolEntry createEntry (RouteSpecificPool rospl, ClientConnectionOperator op)

Also: HttpClient

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 entry
op the operator for creating a connection
Returns
  • the new pool entry for a new connection

protected Queue<BasicPoolEntry> createFreeConnQueue ()

Also: HttpClient

Creates the queue for freeConnections. Called once by the constructor.

Returns
  • a queue

protected Map<HttpRouteRouteSpecificPool> createRouteToPoolMap ()

Also: HttpClient

Creates the map for routeToPool. Called once by the constructor.

Returns
  • a map

protected Queue<WaitingThread> createWaitingThreadQueue ()

Also: HttpClient

Creates the queue for waitingThreads. Called once by the constructor.

Returns
  • a queue

protected void deleteEntry (BasicPoolEntry entry)

Also: HttpClient

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

protected void deleteLeastUsedEntry ()

Also: HttpClient

Delete an old, free pool entry to make room for a new one. Used to replace pool entries with ones for a different route.

protected BasicPoolEntry getEntryBlocking (HttpRoute route, Object state, long timeout, TimeUnit tunit, WaitingThreadAborter aborter)

Also: HttpClient

Obtains a pool entry with a connection within the given timeout. If a WaitingThread is used to block, setWaitingThread(WaitingThread) must be called before blocking, to allow the thread to be interrupted.

Parameters
route the route for which to get the connection
timeout the timeout, 0 or negative for no timeout
tunit the unit for the timeout, may be null only if there is no timeout
aborter an object which can abort a WaitingThread.
Returns
  • pool entry holding a connection for the route
Throws
ConnectionPoolTimeoutException if the timeout expired
InterruptedException if the calling thread was interrupted

protected BasicPoolEntry getFreeEntry (RouteSpecificPool rospl, Object state)

Also: HttpClient

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 null if none is available

protected Lock getLock ()

Also: HttpClient

protected RouteSpecificPool getRoutePool (HttpRoute route, boolean create)

Also: HttpClient

Get a route-specific pool of available connections.

Parameters
route the route
create whether to create the pool if it doesn't exist
Returns
  • the pool for the argument route, never null if create is true

protected void handleLostEntry (HttpRoute route)

Also: HttpClient

protected RouteSpecificPool newRouteSpecificPool (HttpRoute route)

Also: HttpClient

Creates a new route-specific pool. Called by getRoutePool(HttpRoute, boolean) when necessary.

Parameters
route the route
Returns
  • the new pool

protected WaitingThread newWaitingThread (Condition cond, RouteSpecificPool rospl)

Also: HttpClient

Creates a new waiting thread. Called by getRoutePool(HttpRoute, boolean) when necessary.

Parameters
cond the condition to wait for
rospl the route specific pool, or null
Returns
  • a waiting thread representation

protected void notifyWaitingThread (RouteSpecificPool rospl)

Also: HttpClient

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, or null