org.apache.http.impl.conn.tsccm
Class ConnPoolByRoute

java.lang.Object
  extended by org.apache.http.impl.conn.tsccm.AbstractConnPool
      extended by 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:

Author:
Roland Weber, Michael Becke, and others

Nested Class Summary
protected static class ConnPoolByRoute.WaitingThread
          A thread and the pool in which it is waiting.
 
Field Summary
 
Fields inherited from class org.apache.http.impl.conn.tsccm.AbstractConnPool
connManager, idleConnHandler, isShutDown, issuedConnections, numConnections, params, refQueue
 
Constructor Summary
ConnPoolByRoute(ClientConnectionManager mgr)
          Creates a new connection pool, managed by route.
 
Method Summary
protected  BasicPoolEntry createEntry(RouteSpecificPool rospl, ClientConnectionOperator op)
          Creates a new pool entry.
 void deleteClosedConnections()
          Deletes all entries for closed connections.
protected  void deleteEntry(BasicPoolEntry entry)
          Deletes a given pool entry.
protected  void deleteLeastUsedEntry()
          Delete an old, free pool entry to make room for a new one.
 void freeEntry(BasicPoolEntry entry)
          Returns an entry into the pool.
 int getConnectionsInPool(HttpRoute route)
           
 BasicPoolEntry getEntry(HttpRoute route, long timeout, ClientConnectionOperator operator)
          Obtains a pool entry with a connection within the given timeout.
protected  BasicPoolEntry getFreeEntry(RouteSpecificPool rospl)
          If available, get a free pool entry for a route.
protected  RouteSpecificPool getRoutePool(HttpRoute route, boolean create)
          Get a route-specific pool of available connections.
protected  void handleLostEntry(HttpRoute route)
          Handles cleaning up for a lost pool entry with the given route.
protected  RouteSpecificPool newRouteSpecificPool(HttpRoute route)
          Creates a new route-specific pool.
protected  void notifyWaitingThread(RouteSpecificPool rospl)
          Notifies a waiting thread that a connection is available.
 void shutdown()
          Shuts down this pool and all associated resources.
 
Methods inherited from class org.apache.http.impl.conn.tsccm.AbstractConnPool
closeConnection, closeIdleConnections, handleReference
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConnPoolByRoute

public ConnPoolByRoute(ClientConnectionManager mgr)
Creates a new connection pool, managed by route.

Parameters:
mgr - the connection manager
Method Detail

getRoutePool

protected RouteSpecificPool getRoutePool(HttpRoute route,
                                         boolean create)
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

newRouteSpecificPool

protected RouteSpecificPool newRouteSpecificPool(HttpRoute route)
Creates a new route-specific pool. Called by getRoutePool, if necessary.

Parameters:
route - the route
Returns:
the new pool

getConnectionsInPool

public int getConnectionsInPool(HttpRoute route)

getEntry

public BasicPoolEntry getEntry(HttpRoute route,
                               long timeout,
                               ClientConnectionOperator operator)
                        throws ConnectionPoolTimeoutException,
                               java.lang.InterruptedException
Description copied from class: AbstractConnPool
Obtains a pool entry with a connection within the given timeout.

Specified by:
getEntry in class AbstractConnPool
Parameters:
route - the route for which to get the connection
timeout - the timeout, or 0 for no timeout
operator - the connection operator, in case a connection has to be created
Returns:
pool entry holding a connection for the route
Throws:
ConnectionPoolTimeoutException - if the timeout expired
java.lang.InterruptedException - if the calling thread was interrupted

freeEntry

public void freeEntry(BasicPoolEntry entry)
Description copied from class: AbstractConnPool
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.

Specified by:
freeEntry in class AbstractConnPool
Parameters:
entry - the entry for the connection to release

getFreeEntry

protected BasicPoolEntry getFreeEntry(RouteSpecificPool rospl)
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

createEntry

protected BasicPoolEntry createEntry(RouteSpecificPool rospl,
                                     ClientConnectionOperator op)
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

deleteEntry

protected void deleteEntry(BasicPoolEntry entry)
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

protected void handleLostEntry(HttpRoute route)
Description copied from class: AbstractConnPool
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.

Specified by:
handleLostEntry in class AbstractConnPool
Parameters:
route - the route of the pool entry that was lost

notifyWaitingThread

protected void notifyWaitingThread(RouteSpecificPool rospl)
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

deleteClosedConnections

public void deleteClosedConnections()
Description copied from class: AbstractConnPool
Deletes all entries for closed connections.

Specified by:
deleteClosedConnections in class AbstractConnPool

shutdown

public void shutdown()
Description copied from class: AbstractConnPool
Shuts down this pool and all associated resources. Overriding methods MUST call the implementation here!

Overrides:
shutdown in class AbstractConnPool


Copyright © 2000-2007 Apache Software Foundation. All Rights Reserved.