com.twitter.common.net.pool
Class ConnectionPool<S extends Connection<?,?>>

java.lang.Object
  extended by com.twitter.common.net.pool.ConnectionPool<S>
All Implemented Interfaces:
ObjectPool<S>

public final class ConnectionPool<S extends Connection<?,?>>
extends Object
implements ObjectPool<S>

A generic connection pool that delegates growth policy to a ConnectionFactory and connection choice to a supplied strategy.

TODO(John Sirois): implement a reaper to clean up connections that may become invalid when not in use.

TODO(John Sirois): take a ShutdownRegistry and register a close command


Field Summary
 
Fields inherited from interface com.twitter.common.net.pool.ObjectPool
NO_TIMEOUT
 
Constructor Summary
ConnectionPool(ConnectionFactory<S> connectionFactory)
          Creates a connection pool with a connection picker that selects the first item in the set of available connections, exporting statistics to stats provider Stats.STATS_PROVIDER.
ConnectionPool(ConnectionFactory<S> connectionFactory, StatsProvider statsProvider)
          Creates a connection pool with a connection picker that selects the first item in the set of available connections and uses the supplied StatsProvider to register stats with.
 
Method Summary
 void close()
          Disallows further gets from this pool, "closes" all idle objects and any outstanding objects when they are released.
 S get()
          Gets a resource potentially blocking for as long as it takes to either create a new one or wait for one to be released.
 S get(Amount<Long,Time> timeout)
          Gets a resource; timing out if there are none available and it takes longer than specified to create a new one or wait for one to be released.
 void release(S connection)
          Releases a resource obtained from this pool back into the pool of available resources.
 void remove(S connection)
          Equivalent to releasing a Connection with isValid() == false.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ConnectionPool

public ConnectionPool(ConnectionFactory<S> connectionFactory)
Creates a connection pool with a connection picker that selects the first item in the set of available connections, exporting statistics to stats provider Stats.STATS_PROVIDER.

Parameters:
connectionFactory - Factory to create and destroy connections.

ConnectionPool

public ConnectionPool(ConnectionFactory<S> connectionFactory,
                      StatsProvider statsProvider)
Creates a connection pool with a connection picker that selects the first item in the set of available connections and uses the supplied StatsProvider to register stats with.

Parameters:
connectionFactory - Factory to create and destroy connections.
statsProvider - Stats export provider.
Method Detail

toString

public String toString()
Overrides:
toString in class Object

get

public S get()
                              throws ResourceExhaustedException,
                                     TimeoutException
Description copied from interface: ObjectPool
Gets a resource potentially blocking for as long as it takes to either create a new one or wait for one to be released. Callers must release the connection when they are done with it.

Specified by:
get in interface ObjectPool<S extends Connection<?,?>>
Returns:
a resource for exclusive use by the caller
Throws:
ResourceExhaustedException - if no resource could be obtained because this pool was exhausted
TimeoutException - if we timed out while trying to fetch a resource

get

public S get(Amount<Long,Time> timeout)
                              throws ResourceExhaustedException,
                                     TimeoutException
Description copied from interface: ObjectPool
Gets a resource; timing out if there are none available and it takes longer than specified to create a new one or wait for one to be released. Callers must (Object) release the connection when they are done with it.

Specified by:
get in interface ObjectPool<S extends Connection<?,?>>
Parameters:
timeout - the maximum amount of time to wait
Returns:
a resource for exclusive use by the caller
Throws:
ResourceExhaustedException - if no resource could be obtained because this pool was exhausted
TimeoutException - if the specified timeout was reached before a resource became available

release

public void release(S connection)
Description copied from interface: ObjectPool
Releases a resource obtained from this pool back into the pool of available resources. It is an error to release a resource not obtained from this pool.

Specified by:
release in interface ObjectPool<S extends Connection<?,?>>
Parameters:
connection - Resource to release.

remove

public void remove(S connection)
Equivalent to releasing a Connection with isValid() == false.

Specified by:
remove in interface ObjectPool<S extends Connection<?,?>>
Parameters:
connection - Resource to remove.
See Also:
ObjectPool.remove(Object)

close

public void close()
Description copied from interface: ObjectPool
Disallows further gets from this pool, "closes" all idle objects and any outstanding objects when they are released.

Specified by:
close in interface ObjectPool<S extends Connection<?,?>>