org.apache.commons.pool.impl
Class StackKeyedObjectPool

java.lang.Object
  extended by org.apache.commons.pool.BaseKeyedObjectPool
      extended by org.apache.commons.pool.impl.StackKeyedObjectPool
All Implemented Interfaces:
KeyedObjectPool

public class StackKeyedObjectPool
extends BaseKeyedObjectPool
implements KeyedObjectPool

A simple, Stack-based KeyedObjectPool implementation.

Given a KeyedPoolableObjectFactory, this class will maintain a simple pool of instances. A finite number of "sleeping" or inactive instances is enforced, but when the pool is empty, new instances are created to support the new load. Hence this class places no limit on the number of "active" instances created by the pool, but is quite useful for re-using Objects without introducing artificial limits.

Since:
Pool 1.0
Version:
$Revision: 778880 $ $Date: 2009-05-26 16:46:22 -0400 (Tue, 26 May 2009) $
Author:
Rodney Waldhoff, Sandy McArthur
See Also:
Stack

Constructor Summary
StackKeyedObjectPool()
          Create a new pool using no factory.
StackKeyedObjectPool(int max)
          Create a new pool using no factory.
StackKeyedObjectPool(int max, int init)
          Create a new pool using no factory.
StackKeyedObjectPool(KeyedPoolableObjectFactory factory)
          Create a new SimpleKeyedObjectPool using the specified factory to create new instances.
StackKeyedObjectPool(KeyedPoolableObjectFactory factory, int max)
          Create a new SimpleKeyedObjectPool using the specified factory to create new instances.
StackKeyedObjectPool(KeyedPoolableObjectFactory factory, int max, int init)
          Create a new SimpleKeyedObjectPool using the specified factory to create new instances.
 
Method Summary
 void addObject(Object key)
          Create an object using the factory, passivate it, and then placed in the idle object pool.
 Object borrowObject(Object key)
          Obtains an instance from this pool for the specified key.
 void clear()
          Clears the pool, removing all pooled instances.
 void clear(Object key)
          Clears the specified pool, removing all pooled instances corresponding to the given key.
 void close()
          Close this pool, and free any resources associated with it.
 int getNumActive()
          Returns the total number of instances current borrowed from this pool but not yet returned.
 int getNumActive(Object key)
          Returns the number of instances currently borrowed from but not yet returned to the pool corresponding to the given key.
 int getNumIdle()
          Returns the total number of instances currently idle in this pool.
 int getNumIdle(Object key)
          Returns the number of instances corresponding to the given key currently idle in this pool.
 void invalidateObject(Object key, Object obj)
          Invalidates an object from the pool By contract, obj must have been obtained using borrowObject or a related method as defined in an implementation or sub-interface using a key that is equivalent to the one used to borrow the Object in the first place.
 void returnObject(Object key, Object obj)
          Return an instance to the pool.
 void setFactory(KeyedPoolableObjectFactory factory)
          Sets the factory the pool uses to create new instances.
 String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

StackKeyedObjectPool

public StackKeyedObjectPool()
Create a new pool using no factory. Clients must first set the factory or may populate the pool using returnObject before they can be borrowed.

See Also:
StackKeyedObjectPool(KeyedPoolableObjectFactory), setFactory(KeyedPoolableObjectFactory)

StackKeyedObjectPool

public StackKeyedObjectPool(int max)
Create a new pool using no factory. Clients must first set the factory or may populate the pool using returnObject before they can be borrowed.

Parameters:
max - cap on the number of "sleeping" instances in the pool
See Also:
StackKeyedObjectPool(KeyedPoolableObjectFactory, int), setFactory(KeyedPoolableObjectFactory)

StackKeyedObjectPool

public StackKeyedObjectPool(int max,
                            int init)
Create a new pool using no factory. Clients must first set the factory or may populate the pool using returnObject before they can be borrowed.

Parameters:
max - cap on the number of "sleeping" instances in the pool
init - initial size of the pool (this specifies the size of the container, it does not cause the pool to be pre-populated.)
See Also:
StackKeyedObjectPool(KeyedPoolableObjectFactory, int, int), setFactory(KeyedPoolableObjectFactory)

StackKeyedObjectPool

public StackKeyedObjectPool(KeyedPoolableObjectFactory factory)
Create a new SimpleKeyedObjectPool using the specified factory to create new instances.

Parameters:
factory - the KeyedPoolableObjectFactory used to populate the pool

StackKeyedObjectPool

public StackKeyedObjectPool(KeyedPoolableObjectFactory factory,
                            int max)
Create a new SimpleKeyedObjectPool using the specified factory to create new instances. capping the number of "sleeping" instances to max

Parameters:
factory - the KeyedPoolableObjectFactory used to populate the pool
max - cap on the number of "sleeping" instances in the pool

StackKeyedObjectPool

public StackKeyedObjectPool(KeyedPoolableObjectFactory factory,
                            int max,
                            int init)
Create a new SimpleKeyedObjectPool using the specified factory to create new instances. capping the number of "sleeping" instances to max, and initially allocating a container capable of containing at least init instances.

Parameters:
factory - the KeyedPoolableObjectFactory used to populate the pool
max - cap on the number of "sleeping" instances in the pool
init - initial size of the pool (this specifies the size of the container, it does not cause the pool to be pre-populated.)
Method Detail

addObject

public void addObject(Object key)
               throws Exception
Create an object using the factory, passivate it, and then placed in the idle object pool. addObject is useful for "pre-loading" a pool with idle objects.

Specified by:
addObject in interface KeyedObjectPool
Overrides:
addObject in class BaseKeyedObjectPool
Parameters:
key - the key a new instance should be added to
Throws:
Exception - when KeyedPoolableObjectFactory.makeObject(java.lang.Object) fails.
IllegalStateException - when no factory has been set or after close() has been called on this pool.

borrowObject

public Object borrowObject(Object key)
                    throws Exception
Description copied from interface: KeyedObjectPool
Obtains an instance from this pool for the specified key.

Instances returned from this method will have been either newly created with makeObject or will be a previously idle object and have been activated with activateObject and then validated with validateObject.

By contract, clients must return the borrowed object using returnObject, invalidateObject, or a related method as defined in an implementation or sub-interface, using a key that is equivalent to the one used to borrow the instance in the first place.

The behaviour of this method when the pool has been exhausted is not strictly specified (although it may be specified by implementations). Older versions of this method would return null to indicate exhaustion, newer versions are encouraged to throw a NoSuchElementException.

Specified by:
borrowObject in interface KeyedObjectPool
Specified by:
borrowObject in class BaseKeyedObjectPool
Parameters:
key - the key used to obtain the object
Returns:
an instance from this pool.
Throws:
IllegalStateException - after close has been called on this pool
Exception - when makeObject throws an exception
NoSuchElementException - when the pool is exhausted and cannot or will not return another instance

clear

public void clear()
Clears the pool, removing all pooled instances.

Specified by:
clear in interface KeyedObjectPool
Overrides:
clear in class BaseKeyedObjectPool

clear

public void clear(Object key)
Clears the specified pool, removing all pooled instances corresponding to the given key.

Specified by:
clear in interface KeyedObjectPool
Overrides:
clear in class BaseKeyedObjectPool
Parameters:
key - the key to clear

close

public void close()
           throws Exception
Close this pool, and free any resources associated with it.

Calling addObject or borrowObject after invoking this method on a pool will cause them to throw an IllegalStateException.

Specified by:
close in interface KeyedObjectPool
Overrides:
close in class BaseKeyedObjectPool
Throws:
Exception - deprecated: implementations should silently fail if not all resources can be freed.

getNumActive

public int getNumActive()
Returns the total number of instances current borrowed from this pool but not yet returned.

Specified by:
getNumActive in interface KeyedObjectPool
Overrides:
getNumActive in class BaseKeyedObjectPool
Returns:
the total number of instances currently borrowed from this pool

getNumActive

public int getNumActive(Object key)
Returns the number of instances currently borrowed from but not yet returned to the pool corresponding to the given key.

Specified by:
getNumActive in interface KeyedObjectPool
Overrides:
getNumActive in class BaseKeyedObjectPool
Parameters:
key - the key to query
Returns:
the number of instances corresponding to the given key currently borrowed in this pool

getNumIdle

public int getNumIdle()
Returns the total number of instances currently idle in this pool.

Specified by:
getNumIdle in interface KeyedObjectPool
Overrides:
getNumIdle in class BaseKeyedObjectPool
Returns:
the total number of instances currently idle in this pool

getNumIdle

public int getNumIdle(Object key)
Returns the number of instances corresponding to the given key currently idle in this pool.

Specified by:
getNumIdle in interface KeyedObjectPool
Overrides:
getNumIdle in class BaseKeyedObjectPool
Parameters:
key - the key to query
Returns:
the number of instances corresponding to the given key currently idle in this pool

invalidateObject

public void invalidateObject(Object key,
                             Object obj)
                      throws Exception
Description copied from interface: KeyedObjectPool
Invalidates an object from the pool By contract, obj must have been obtained using borrowObject or a related method as defined in an implementation or sub-interface using a key that is equivalent to the one used to borrow the Object in the first place.

This method should be used when an object that has been borrowed is determined (due to an exception or other problem) to be invalid.

Specified by:
invalidateObject in interface KeyedObjectPool
Specified by:
invalidateObject in class BaseKeyedObjectPool
Parameters:
key - the key used to obtain the object
obj - a borrowed instance to be returned.
Throws:
Exception

returnObject

public void returnObject(Object key,
                         Object obj)
                  throws Exception
Description copied from interface: KeyedObjectPool
Return an instance to the pool. By contract, obj must have been obtained using borrowObject or a related method as defined in an implementation or sub-interface using a key that is equivalent to the one used to borrow the instance in the first place.

Specified by:
returnObject in interface KeyedObjectPool
Specified by:
returnObject in class BaseKeyedObjectPool
Parameters:
key - the key used to obtain the object
obj - a borrowed instance to be returned.
Throws:
Exception

setFactory

public void setFactory(KeyedPoolableObjectFactory factory)
                throws IllegalStateException
Sets the factory the pool uses to create new instances. Trying to change the factory after a pool has been used will frequently throw an UnsupportedOperationException.

Specified by:
setFactory in interface KeyedObjectPool
Overrides:
setFactory in class BaseKeyedObjectPool
Parameters:
factory - the KeyedPoolableObjectFactory used to create new instances.
Throws:
IllegalStateException - when the factory cannot be set at this time

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2001-2003 Apache Software Foundation. Documenation generated November 18 2012.