org.apache.commons.pool.impl
Class StackObjectPool

java.lang.Object
  extended by org.apache.commons.pool.BaseObjectPool
      extended by org.apache.commons.pool.impl.StackObjectPool
All Implemented Interfaces:
ObjectPool

public class StackObjectPool
extends BaseObjectPool
implements ObjectPool

A simple, Stack-based ObjectPool implementation.

Given a PoolableObjectFactory, this class will maintain a simple pool of instances. A finite number of "sleeping" or idle 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: 777748 $ $Date: 2009-05-22 20:00:44 -0400 (Fri, 22 May 2009) $
Author:
Rodney Waldhoff, Dirk Verbeeck, Sandy McArthur

Field Summary
protected  PoolableObjectFactory _factory
          My PoolableObjectFactory.
protected  int _maxSleeping
          The cap on the number of "sleeping" instances in the pool.
protected  int _numActive
          Number of object borrowed but not yet returned to the pool.
protected  Stack _pool
          My pool.
protected static int DEFAULT_INIT_SLEEPING_CAPACITY
          The default initial size of the pool (this specifies the size of the container, it does not cause the pool to be pre-populated.)
protected static int DEFAULT_MAX_SLEEPING
          The default cap on the number of "sleeping" instances in the pool.
 
Constructor Summary
StackObjectPool()
          Create a new pool using no factory.
StackObjectPool(int maxIdle)
          Create a new pool using no factory.
StackObjectPool(int maxIdle, int initIdleCapacity)
          Create a new pool using no factory.
StackObjectPool(PoolableObjectFactory factory)
          Create a new StackObjectPool using the specified factory to create new instances.
StackObjectPool(PoolableObjectFactory factory, int maxIdle)
          Create a new SimpleObjectPool using the specified factory to create new instances, capping the number of "sleeping" instances to max.
StackObjectPool(PoolableObjectFactory factory, int maxIdle, int initIdleCapacity)
          Create a new SimpleObjectPool 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.
 
Method Summary
 void addObject()
          Create an object, and place it into the pool.
 Object borrowObject()
          Obtains an instance from the pool.
 void clear()
          Clears any objects sitting idle in the pool.
 void close()
          Close this pool, and free any resources associated with it.
 int getNumActive()
          Return the number of instances currently borrowed from this pool.
 int getNumIdle()
          Return the number of instances currently idle in this pool.
 void invalidateObject(Object obj)
          Invalidates an object from the pool.
 void returnObject(Object obj)
          Returns an instance to the pool.
 void setFactory(PoolableObjectFactory factory)
          Sets the factory this pool uses to create new instances.
 
Methods inherited from class org.apache.commons.pool.BaseObjectPool
assertOpen, isClosed
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_MAX_SLEEPING

protected static final int DEFAULT_MAX_SLEEPING
The default cap on the number of "sleeping" instances in the pool.

See Also:
Constant Field Values

DEFAULT_INIT_SLEEPING_CAPACITY

protected static final int DEFAULT_INIT_SLEEPING_CAPACITY
The default initial size of the pool (this specifies the size of the container, it does not cause the pool to be pre-populated.)

See Also:
Constant Field Values

_pool

protected Stack _pool
My pool.


_factory

protected PoolableObjectFactory _factory
My PoolableObjectFactory.


_maxSleeping

protected int _maxSleeping
The cap on the number of "sleeping" instances in the pool.


_numActive

protected int _numActive
Number of object borrowed but not yet returned to the pool.

Constructor Detail

StackObjectPool

public StackObjectPool()
Create a new pool using no factory. Clients must first set the factory else this pool will not behave correctly. Clients may first populate the pool using returnObject(java.lang.Object) before they can be borrowed but this useage is discouraged.

See Also:
StackObjectPool(PoolableObjectFactory)

StackObjectPool

public StackObjectPool(int maxIdle)
Create a new pool using no factory. Clients must first set the factory else this pool will not behave correctly. Clients may first populate the pool using returnObject(java.lang.Object) before they can be borrowed but this useage is discouraged.

Parameters:
maxIdle - cap on the number of "sleeping" instances in the pool
See Also:
StackObjectPool(PoolableObjectFactory, int)

StackObjectPool

public StackObjectPool(int maxIdle,
                       int initIdleCapacity)
Create a new pool using no factory. Clients must first set the factory else this pool will not behave correctly. Clients may first populate the pool using returnObject(java.lang.Object) before they can be borrowed but this useage is discouraged.

Parameters:
maxIdle - cap on the number of "sleeping" instances in the pool
initIdleCapacity - initial size of the pool (this specifies the size of the container, it does not cause the pool to be pre-populated.)
See Also:
StackObjectPool(PoolableObjectFactory, int, int)

StackObjectPool

public StackObjectPool(PoolableObjectFactory factory)
Create a new StackObjectPool using the specified factory to create new instances.

Parameters:
factory - the PoolableObjectFactory used to populate the pool

StackObjectPool

public StackObjectPool(PoolableObjectFactory factory,
                       int maxIdle)
Create a new SimpleObjectPool using the specified factory to create new instances, capping the number of "sleeping" instances to max.

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

StackObjectPool

public StackObjectPool(PoolableObjectFactory factory,
                       int maxIdle,
                       int initIdleCapacity)
Create a new SimpleObjectPool 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 PoolableObjectFactory used to populate the pool
maxIdle - cap on the number of "sleeping" instances in the pool
initIdleCapacity - initial size of the pool (this specifies the size of the container, it does not cause the pool to be pre-populated.)
Method Detail

borrowObject

public Object borrowObject()
                    throws Exception
Description copied from class: BaseObjectPool
Obtains an instance from the pool.

Specified by:
borrowObject in interface ObjectPool
Specified by:
borrowObject in class BaseObjectPool
Returns:
an instance from the pool
Throws:
Exception - if an instance cannot be obtained from the pool

returnObject

public void returnObject(Object obj)
                  throws Exception
Description copied from class: BaseObjectPool
Returns an instance to the pool.

Specified by:
returnObject in interface ObjectPool
Specified by:
returnObject in class BaseObjectPool
Parameters:
obj - instance to return to the pool
Throws:
Exception

invalidateObject

public void invalidateObject(Object obj)
                      throws Exception
Description copied from class: BaseObjectPool
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.

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 ObjectPool
Specified by:
invalidateObject in class BaseObjectPool
Parameters:
obj - a borrowed instance to be disposed.
Throws:
Exception

getNumIdle

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

Specified by:
getNumIdle in interface ObjectPool
Overrides:
getNumIdle in class BaseObjectPool
Returns:
the number of instances currently idle in this pool

getNumActive

public int getNumActive()
Return the number of instances currently borrowed from this pool.

Specified by:
getNumActive in interface ObjectPool
Overrides:
getNumActive in class BaseObjectPool
Returns:
the number of instances currently borrowed from this pool

clear

public void clear()
Clears any objects sitting idle in the pool.

Specified by:
clear in interface ObjectPool
Overrides:
clear in class BaseObjectPool

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 ObjectPool
Overrides:
close in class BaseObjectPool
Throws:
Exception - deprecated: implementations should silently fail if not all resources can be freed.

addObject

public void addObject()
               throws Exception
Create an object, and place it into the pool. addObject() is useful for "pre-loading" a pool with idle objects.

Specified by:
addObject in interface ObjectPool
Overrides:
addObject in class BaseObjectPool
Throws:
Exception - when the _factory has a problem creating an object.

setFactory

public void setFactory(PoolableObjectFactory factory)
                throws IllegalStateException
Sets the factory this pool uses to create new instances. Trying to change the factory while there are borrowed objects will throw an IllegalStateException.

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


Copyright © 2001-2009 The Apache Software Foundation. All Rights Reserved.