FileDocCategorySizeDatePackage
ThreadSafeClientConnManager.javaAPI DocAndroid 1.5 API10119Wed May 06 22:41:10 BST 2009org.apache.http.impl.conn.tsccm

ThreadSafeClientConnManager

public class ThreadSafeClientConnManager extends Object implements ClientConnectionManager
Manages a pool of {@link OperatedClientConnection client connections}.

This class is derived from MultiThreadedHttpConnectionManager in HttpClient 3. See there for original authors.

author
Roland Weber
author
Michael Becke
version
$Revision: 673450 $ $Date: 2008-07-02 10:35:05 -0700 (Wed, 02 Jul 2008) $
since
4.0

Fields Summary
private final Log
log
protected SchemeRegistry
schemeRegistry
The schemes supported by this connection manager.
protected final AbstractConnPool
connectionPool
The pool of connections being managed.
protected ClientConnectionOperator
connOperator
The operator for opening and updating connections.
Constructors Summary
public ThreadSafeClientConnManager(HttpParams params, SchemeRegistry schreg)
Creates a new thread safe connection manager.

param
params the parameters for this manager
param
schreg the scheme registry, or null for the default registry

    


                                                         
      
                                         

        if (params == null) {
            throw new IllegalArgumentException("HTTP parameters may not be null");
        }
        this.schemeRegistry = schreg;
        this.connOperator   = createConnectionOperator(schreg);
        this.connectionPool = createConnectionPool(params);

    
Methods Summary
public voidcloseExpiredConnections()

        connectionPool.closeExpiredConnections();
        connectionPool.deleteClosedConnections();
    
public voidcloseIdleConnections(long idleTimeout, java.util.concurrent.TimeUnit tunit)

        // combine these two in a single call?
        connectionPool.closeIdleConnections(idleTimeout, tunit);
        connectionPool.deleteClosedConnections();
    
protected org.apache.http.conn.ClientConnectionOperatorcreateConnectionOperator(org.apache.http.conn.scheme.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 {@link DefaultClientConnectionOperator DefaultClientConnectionOperator}.

param
schreg the scheme registry to use, or null
return
the connection operator to use


        return new DefaultClientConnectionOperator(schreg);
    
protected org.apache.http.impl.conn.tsccm.AbstractConnPoolcreateConnectionPool(org.apache.http.params.HttpParams params)
Hook for creating the connection pool.

return
the connection pool to use


        AbstractConnPool acp = new ConnPoolByRoute(connOperator, params);
        boolean conngc = true; //@@@ check parameters to decide
        if (conngc) {
            acp.enableConnectionGC();
        }
        return acp;
    
protected voidfinalize()

        shutdown();
        super.finalize();
    
public intgetConnectionsInPool()
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.

return
the total number of pooled connections

        synchronized (connectionPool) {
            return connectionPool.numConnections; //@@@
        }
    
public intgetConnectionsInPool(org.apache.http.conn.routing.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.

param
route the route in question
return
the total number of pooled connections for that route

        return ((ConnPoolByRoute)connectionPool).getConnectionsInPool(
                route);
    
public org.apache.http.conn.scheme.SchemeRegistrygetSchemeRegistry()

        return this.schemeRegistry;
    
public voidreleaseConnection(org.apache.http.conn.ManagedClientConnection conn, long validDuration, java.util.concurrent.TimeUnit timeUnit)


        if (!(conn instanceof BasicPooledConnAdapter)) {
            throw new IllegalArgumentException
                ("Connection class mismatch, " +
                 "connection not obtained from this manager.");
        }
        BasicPooledConnAdapter hca = (BasicPooledConnAdapter) conn;
        if ((hca.getPoolEntry() != null) && (hca.getManager() != this)) {
            throw new IllegalArgumentException
                ("Connection not obtained from this manager.");
        }

        try {
            // make sure that the response has been read completely
            if (hca.isOpen() && !hca.isMarkedReusable()) {
                if (log.isDebugEnabled()) {
                    log.debug
                        ("Released connection open but not marked reusable.");
                }
                // In MTHCM, there would be a call to
                // SimpleHttpConnectionManager.finishLastResponse(conn);
                // Consuming the response is handled outside in 4.0.

                // make sure this connection will not be re-used
                // Shut down rather than close, we might have gotten here
                // because of a shutdown trigger.
                // Shutdown of the adapter also clears the tracked route.
                hca.shutdown();
            }
        } catch (IOException iox) {
            //@@@ log as warning? let pass?
            if (log.isDebugEnabled())
                log.debug("Exception shutting down released connection.",
                          iox);
        } finally {
            BasicPoolEntry entry = (BasicPoolEntry) hca.getPoolEntry();
            boolean reusable = hca.isMarkedReusable();
            hca.detach();
            if (entry != null) {
                connectionPool.freeEntry(entry, reusable, validDuration, timeUnit);
            }
        }
    
public org.apache.http.conn.ClientConnectionRequestrequestConnection(org.apache.http.conn.routing.HttpRoute route, java.lang.Object state)

        
        final PoolEntryRequest poolRequest = connectionPool.requestPoolEntry(
                route, state);
        
        return new ClientConnectionRequest() {
            
            public void abortRequest() {
                poolRequest.abortRequest();
            }
            
            public ManagedClientConnection getConnection(
                    long timeout, TimeUnit tunit) throws InterruptedException,
                    ConnectionPoolTimeoutException {
                if (route == null) {
                    throw new IllegalArgumentException("Route may not be null.");
                }

                if (log.isDebugEnabled()) {
                    log.debug("ThreadSafeClientConnManager.getConnection: "
                        + route + ", timeout = " + timeout);
                }

                BasicPoolEntry entry = poolRequest.getPoolEntry(timeout, tunit);
                return new BasicPooledConnAdapter(ThreadSafeClientConnManager.this, entry);
            }
            
        };
        
    
public voidshutdown()

        connectionPool.shutdown();