Class StackObjectPool<T>
- java.lang.Object
-
- org.apache.commons.pool.BaseObjectPool<T>
-
- org.apache.commons.pool.impl.StackObjectPool<T>
-
- Type Parameters:
T
- the type of objects held in this pool
- All Implemented Interfaces:
ObjectPool<T>
public class StackObjectPool<T> extends BaseObjectPool<T> implements ObjectPool<T>
A simple,Stack
-basedObjectPool
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: 1222396 $ $Date: 2011-12-22 14:02:25 -0500 (Thu, 22 Dec 2011) $
- Author:
- Rodney Waldhoff, Dirk Verbeeck, Sandy McArthur
-
-
Constructor Summary
Constructors Constructor Description StackObjectPool(PoolableObjectFactory<T> factory)
Create a new StackObjectPool using the specified factory to create new instances.StackObjectPool(PoolableObjectFactory<T> factory, int maxIdle)
Create a new SimpleObjectPool using the specified factory to create new instances, capping the number of "sleeping" instances to maxIdle.StackObjectPool(PoolableObjectFactory<T> factory, int maxIdle, int initIdleCapacity)
Create a new StackObjectPool using the specifiedfactory
to create new instances, capping the number of "sleeping" instances tomaxIdle
, and initially allocating a container capable of containing at leastinitIdleCapacity
instances.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addObject()
Create an object, and place it on top of the stack.T
borrowObject()
Borrows an object 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.PoolableObjectFactory<T>
getFactory()
Returns thePoolableObjectFactory
used by this pool to create and manage object instances.int
getMaxSleeping()
Returns the maximum number of idle instances in the pool.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(T obj)
Invalidates an object from the pool.void
returnObject(T obj)
Returns an instance to the pool, pushing it on top of the idle instance stack after successful validation and passivation.-
Methods inherited from class org.apache.commons.pool.BaseObjectPool
isClosed
-
-
-
-
Constructor Detail
-
StackObjectPool
public StackObjectPool(PoolableObjectFactory<T> factory)
Create a new StackObjectPool using the specified factory to create new instances.- Parameters:
factory
- thePoolableObjectFactory
used to populate the pool
-
StackObjectPool
public StackObjectPool(PoolableObjectFactory<T> factory, int maxIdle)
Create a new SimpleObjectPool using the specified factory to create new instances, capping the number of "sleeping" instances to maxIdle.- Parameters:
factory
- thePoolableObjectFactory
used to populate the poolmaxIdle
- cap on the number of "sleeping" instances in the pool
-
StackObjectPool
public StackObjectPool(PoolableObjectFactory<T> factory, int maxIdle, int initIdleCapacity)
Create a new StackObjectPool using the specified
factory
to create new instances, capping the number of "sleeping" instances tomaxIdle
, and initially allocating a container capable of containing at leastinitIdleCapacity
instances. The pool is not pre-populated. TheinitIdleCapacity
parameter just determines the initial size of the underlying container, which can increase beyond this value ifmaxIdle > initIdleCapacity.
Negative values of
maxIdle
are ignored (i.e., the pool is created usingDEFAULT_MAX_SLEEPING
) as are non-positive values forinitIdleCapacity.
- Parameters:
factory
- thePoolableObjectFactory
used to populate the poolmaxIdle
- cap on the number of "sleeping" instances in the poolinitIdleCapacity
- 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 T borrowObject() throws Exception
Borrows an object from the pool. If there are idle instances available on the stack, the top element of the stack is popped to activate, validate and return to the client. If there are no idle instances available, the
makeObject
method of the pool'sPoolableObjectFactory
is invoked to create a new instance.All instances are
activated
andvalidated
before being returned to the client. If validation fails or an exception occurs activating or validating an instance popped from the idle instance stack, the failing instance isdestroyed
and the next instance on the stack is popped, validated and activated. This process continues until either the stack is empty or an instance passes validation. If the stack is empty on activation or it does not contain any valid instances, the factory'smakeObject
method is used to create a new instance. If a null instance is returned by the factory or the created instance either raises an exception on activation or fails validation,NoSuchElementException
is thrown. Exceptions thrown byMakeObject
are propagated to the caller; but other thanThreadDeath
orVirtualMachineError
, exceptions generated by activation, validation or destroy methods are swallowed silently.- Specified by:
borrowObject
in interfaceObjectPool<T>
- Specified by:
borrowObject
in classBaseObjectPool<T>
- Returns:
- an instance from the pool
- Throws:
Exception
- if an instance cannot be obtained from the pool
-
returnObject
public void returnObject(T obj) throws Exception
Returns an instance to the pool, pushing it on top of the idle instance stack after successful validation and passivation. The returning instance is destroyed if any of the following are true:
- the pool is closed
validation
failspassivation
throws an exception
maxSleeping
to be exceeded, the oldest (bottom) instance on the stack is destroyed to make room for the returning instance, which is pushed on top of the stack.Exceptions passivating or destroying instances are silently swallowed. Exceptions validating instances are propagated to the client.
- Specified by:
returnObject
in interfaceObjectPool<T>
- Specified by:
returnObject
in classBaseObjectPool<T>
- Parameters:
obj
- instance to return to the pool- Throws:
Exception
-
invalidateObject
public void invalidateObject(T obj) throws Exception
Invalidates an object from the pool.
By contract,
obj
must have been obtained usingborrowObject
.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 interfaceObjectPool<T>
- Specified by:
invalidateObject
in classBaseObjectPool<T>
- Parameters:
obj
- aborrowed
instance to be disposed.- Throws:
Exception
-
getNumIdle
public int getNumIdle()
Return the number of instances currently idle in this pool.- Specified by:
getNumIdle
in interfaceObjectPool<T>
- Overrides:
getNumIdle
in classBaseObjectPool<T>
- 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 interfaceObjectPool<T>
- Overrides:
getNumActive
in classBaseObjectPool<T>
- Returns:
- the number of instances currently borrowed from this pool
-
clear
public void clear()
Clears any objects sitting idle in the pool. Silently swallows any exceptions thrown byPoolableObjectFactory.destroyObject(Object)
.- Specified by:
clear
in interfaceObjectPool<T>
- Overrides:
clear
in classBaseObjectPool<T>
-
close
public void close() throws Exception
Close this pool, and free any resources associated with it. Invokes
clear()
to destroy and remove instances in the pool.Calling
addObject()
orborrowObject()
after invoking this method on a pool will cause them to throw anIllegalStateException
.- Specified by:
close
in interfaceObjectPool<T>
- Overrides:
close
in classBaseObjectPool<T>
- Throws:
Exception
- never - exceptions clearing the pool are swallowed
-
addObject
public void addObject() throws Exception
Create an object, and place it on top of the stack. This method is useful for "pre-loading" a pool with idle objects.
Before being added to the pool, the newly created instance is
validated
andpassivated
. If validation fails, the new instance isdestroyed
. Exceptions generated by the factorymakeObject
orpassivate
are propagated to the caller. Exceptions destroying instances are silently swallowed.If a new instance is created and successfully validated and passivated and adding this instance to the pool causes
maxSleeping
to be exceeded, the oldest (bottom) instance in the pool is destroyed to make room for the newly created instance, which is pushed on top of the stack.- Specified by:
addObject
in interfaceObjectPool<T>
- Overrides:
addObject
in classBaseObjectPool<T>
- Throws:
Exception
- when thefactory
has a problem creating or passivating an object.
-
getFactory
public PoolableObjectFactory<T> getFactory()
Returns thePoolableObjectFactory
used by this pool to create and manage object instances.- Returns:
- the factory
- Since:
- 1.5.5
-
getMaxSleeping
public int getMaxSleeping()
Returns the maximum number of idle instances in the pool.- Returns:
- maxSleeping
- Since:
- 1.5.5
-
-