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

java.lang.Object
  extended by org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager
All Implemented Interfaces:
ClientConnectionManager

public class ThreadSafeClientConnManager
extends Object
implements ClientConnectionManager

Manages a pool of client connections and is able to service connection requests from multiple execution threads. Connections are pooled on a per route basis. A request for a route which already the manager has persistent connections for available in the pool will be services by leasing a connection from the pool rather than creating a brand new connection.

ThreadSafeClientConnManager maintains a maximum limit of connection on a per route basis and in total. Per default this implementation will create no more than than 2 concurrent connections per given route and no more 20 connections in total. For many real-world applications these limits may prove too constraining, especially if they use HTTP as a transport protocol for their services. Connection limits, however, can be adjusted using HTTP parameters.

The following parameters can be used to customize the behavior of this class:

Since:
4.0
See Also:
ConnPerRoute, ConnPerRouteBean

Field Summary
protected  AbstractConnPool connectionPool
          The pool of connections being managed.
protected  ClientConnectionOperator connOperator
          The operator for opening and updating connections.
protected  SchemeRegistry schemeRegistry
          The schemes supported by this connection manager.
 
Constructor Summary
ThreadSafeClientConnManager(HttpParams params, SchemeRegistry schreg)
          Creates a new thread safe connection manager.
 
Method Summary
 void closeExpiredConnections()
          Closes all expired connections in the pool.
 void closeIdleConnections(long idleTimeout, TimeUnit tunit)
          Closes idle connections in the pool.
protected  ClientConnectionOperator createConnectionOperator(SchemeRegistry schreg)
          Hook for creating the connection operator.
protected  AbstractConnPool createConnectionPool(HttpParams params)
          Hook for creating the connection pool.
protected  void finalize()
           
 int getConnectionsInPool()
          Gets the total number of pooled connections.
 int getConnectionsInPool(HttpRoute route)
          Gets the total number of pooled connections for the given route.
 SchemeRegistry getSchemeRegistry()
          Obtains the scheme registry used by this manager.
 void releaseConnection(ManagedClientConnection conn, long validDuration, TimeUnit timeUnit)
          Releases a connection for use by others.
 ClientConnectionRequest requestConnection(HttpRoute route, Object state)
          Returns a new ClientConnectionRequest, from which a ManagedClientConnection can be obtained or the request can be aborted.
 void shutdown()
          Shuts down this connection manager and releases allocated resources.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

schemeRegistry

protected final SchemeRegistry schemeRegistry
The schemes supported by this connection manager.


connectionPool

protected final AbstractConnPool connectionPool
The pool of connections being managed.


connOperator

protected final ClientConnectionOperator connOperator
The operator for opening and updating connections.

Constructor Detail

ThreadSafeClientConnManager

public ThreadSafeClientConnManager(HttpParams params,
                                   SchemeRegistry schreg)
Creates a new thread safe connection manager.

Parameters:
params - the parameters for this manager.
schreg - the scheme registry.
Method Detail

finalize

protected void finalize()
                 throws Throwable
Overrides:
finalize in class Object
Throws:
Throwable

createConnectionPool

protected AbstractConnPool createConnectionPool(HttpParams params)
Hook for creating the connection pool.

Returns:
the connection pool to use

createConnectionOperator

protected ClientConnectionOperator createConnectionOperator(SchemeRegistry schreg)
Hook for creating the connection operator. It is called by the constructor. Derived classes can override this method to change the instantiation of the operator. The default implementation here instantiates DefaultClientConnectionOperator.

Parameters:
schreg - the scheme registry.
Returns:
the connection operator to use

getSchemeRegistry

public SchemeRegistry getSchemeRegistry()
Description copied from interface: ClientConnectionManager
Obtains the scheme registry used by this manager.

Specified by:
getSchemeRegistry in interface ClientConnectionManager
Returns:
the scheme registry, never null

requestConnection

public ClientConnectionRequest requestConnection(HttpRoute route,
                                                 Object state)
Description copied from interface: ClientConnectionManager
Returns a new ClientConnectionRequest, from which a ManagedClientConnection can be obtained or the request can be aborted.

Specified by:
requestConnection in interface ClientConnectionManager

releaseConnection

public void releaseConnection(ManagedClientConnection conn,
                              long validDuration,
                              TimeUnit timeUnit)
Description copied from interface: ClientConnectionManager
Releases a connection for use by others. You may optionally specify how long the connection is valid to be reused. Values <= 0 are considered to be valid forever. If the connection is not marked as reusable, the connection will not be reused regardless of the valid duration. If the connection has been released before, the call will be ignored.

Specified by:
releaseConnection in interface ClientConnectionManager
Parameters:
conn - the connection to release
validDuration - the duration of time this connection is valid for reuse
timeUnit - the unit of time validDuration is measured in
See Also:
ClientConnectionManager.closeExpiredConnections()

shutdown

public void shutdown()
Description copied from interface: ClientConnectionManager
Shuts down this connection manager and releases allocated resources. This includes closing all connections, whether they are currently used or not.

Specified by:
shutdown in interface ClientConnectionManager

getConnectionsInPool

public int getConnectionsInPool(HttpRoute route)
Gets the total number of pooled connections for the given route. This is the total number of connections that have been created and are still in use by this connection manager for the route. This value will not exceed the maximum number of connections per host.

Parameters:
route - the route in question
Returns:
the total number of pooled connections for that route

getConnectionsInPool

public int getConnectionsInPool()
Gets the total number of pooled connections. This is the total number of connections that have been created and are still in use by this connection manager. This value will not exceed the maximum number of connections in total.

Returns:
the total number of pooled connections

closeIdleConnections

public void closeIdleConnections(long idleTimeout,
                                 TimeUnit tunit)
Description copied from interface: ClientConnectionManager
Closes idle connections in the pool. Open connections in the pool that have not been used for the timespan given by the argument will be closed. Currently allocated connections are not subject to this method. Times will be checked with milliseconds precision All expired connections will also be closed.

Specified by:
closeIdleConnections in interface ClientConnectionManager
Parameters:
idleTimeout - the idle time of connections to be closed
tunit - the unit for the idletime
See Also:
ClientConnectionManager.closeExpiredConnections()

closeExpiredConnections

public void closeExpiredConnections()
Description copied from interface: ClientConnectionManager
Closes all expired connections in the pool. Open connections in the pool that have not been used for the timespan defined when the connection was released will be closed. Currently allocated connections are not subject to this method. Times will be checked with milliseconds precision.

Specified by:
closeExpiredConnections in interface ClientConnectionManager


Copyright © 1999-2010 Apache Software Foundation. All Rights Reserved.