com.twitter.common.net.pool
Interface ObjectPool<T>

All Known Implementing Classes:
ConnectionPool, DynamicPool, MetaPool

public interface ObjectPool<T>

A generic object pool that provides object of a given type for exclusive use by the caller. Object pools generally pool expensive resources and so offer a close() method that should be used to free these resources when the pool is no longer needed.


Field Summary
static Amount<Long,Time> NO_TIMEOUT
          A convenience constant representing a no timeout.
 
Method Summary
 void close()
          Disallows further gets from this pool, "closes" all idle objects and any outstanding objects when they are released.
 T 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.
 T 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(T resource)
          Releases a resource obtained from this pool back into the pool of available resources.
 void remove(T resource)
          Removes a resource obtained from this pool from its available resources.
 

Field Detail

NO_TIMEOUT

static final Amount<Long,Time> NO_TIMEOUT
A convenience constant representing a no timeout.

Method Detail

get

T get()
      throws ResourceExhaustedException,
             TimeoutException
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.

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

T get(Amount<Long,Time> timeout)
      throws ResourceExhaustedException,
             TimeoutException
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.

Parameters:
timeout - the maximum amount of time to wait
Returns:
a resource for exclusive use by the caller
Throws:
TimeoutException - if the specified timeout was reached before a resource became available
ResourceExhaustedException - if no resource could be obtained because this pool was exhausted

release

void release(T resource)
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.

Parameters:
resource - Resource to release.

remove

void remove(T resource)
Removes a resource obtained from this pool from its available resources. It is an error to remove a resource not obtained from this pool.

Parameters:
resource - Resource to remove.

close

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