Class StackObjectPool<T>
- Type Parameters:
T- the type of objects held in this pool
- All Implemented Interfaces:
ObjectPool<T>
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: 1222396 $ $Date: 2011-12-22 14:02:25 -0500 (Thu, 22 Dec 2011) $
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected PoolableObjectFactory<T> Deprecated.protected intDeprecated.to be made private in pool 2.0 - usegetMaxSleeping()protected intDeprecated.to be made private in pool 2.0 - usegetNumActive()Deprecated.to be made private in pool 2.0protected static final intThe 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 final intThe cap on the number of "sleeping" instances in the pool. -
Constructor Summary
ConstructorsConstructorDescriptionDeprecated.to be removed in pool 2.0 - useStackObjectPool(PoolableObjectFactory)StackObjectPool(int maxIdle) Deprecated.to be removed in pool 2.0 - useStackObjectPool(PoolableObjectFactory, int)StackObjectPool(int maxIdle, int initIdleCapacity) Deprecated.to be removed in pool 2.0 - useStackObjectPool(PoolableObjectFactory, int, int)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 specifiedfactoryto create new instances, capping the number of "sleeping" instances tomaxIdle, and initially allocating a container capable of containing at leastinitIdleCapacityinstances. -
Method Summary
Modifier and TypeMethodDescriptionvoidCreate an object, and place it on top of the stack.Borrows an object from the pool.voidclear()Clears any objects sitting idle in the pool.voidclose()Close this pool, and free any resources associated with it.Returns thePoolableObjectFactoryused by this pool to create and manage object instances.intReturns the maximum number of idle instances in the pool.intReturn the number of instances currently borrowed from this pool.intReturn the number of instances currently idle in this pool.voidinvalidateObject(T obj) Invalidates an object from the pool.voidreturnObject(T obj) Returns an instance to the pool, pushing it on top of the idle instance stack after successful validation and passivation.voidsetFactory(PoolableObjectFactory<T> factory) Deprecated.to be removed in pool 2.0Methods inherited from class org.apache.commons.pool.BaseObjectPool
assertOpen, isClosed
-
Field Details
-
DEFAULT_MAX_SLEEPING
protected static final int DEFAULT_MAX_SLEEPINGThe cap on the number of "sleeping" instances in the pool.- See Also:
-
DEFAULT_INIT_SLEEPING_CAPACITY
protected static final int DEFAULT_INIT_SLEEPING_CAPACITYThe 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:
-
_pool
Deprecated.to be made private in pool 2.0My pool. -
_factory
Deprecated.to be made private in pool 2.0 - usegetFactory() -
_maxSleeping
Deprecated.to be made private in pool 2.0 - usegetMaxSleeping()The cap on the number of "sleeping" instances in the pool. -
_numActive
Deprecated.to be made private in pool 2.0 - usegetNumActive()Number of objects borrowed but not yet returned to the pool.
-
-
Constructor Details
-
StackObjectPool
Deprecated.to be removed in pool 2.0 - useStackObjectPool(PoolableObjectFactory)Create a new pool using no factory. Clients must firstset the factoryor else this pool will not behave correctly. Clients may first populate the pool usingreturnObject(java.lang.Object)before they can beborrowedbut this usage is discouraged.- See Also:
-
StackObjectPool
Deprecated.to be removed in pool 2.0 - useStackObjectPool(PoolableObjectFactory, int)Create a new pool using no factory. Clients must firstset the factoryor else this pool will not behave correctly. Clients may first populate the pool usingreturnObject(java.lang.Object)before they can beborrowedbut this usage is discouraged.- Parameters:
maxIdle- cap on the number of "sleeping" instances in the pool- See Also:
-
StackObjectPool
Deprecated.to be removed in pool 2.0 - useStackObjectPool(PoolableObjectFactory, int, int)Create a new pool using no factory. Clients must firstset the factoryor else this pool will not behave correctly. Clients may first populate the pool usingreturnObject(java.lang.Object)before they can beborrowedbut this usage is discouraged.- Parameters:
maxIdle- 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.)- See Also:
-
StackObjectPool
Create a new StackObjectPool using the specified factory to create new instances.- Parameters:
factory- thePoolableObjectFactoryused to populate the pool
-
StackObjectPool
Create a new SimpleObjectPool using the specified factory to create new instances, capping the number of "sleeping" instances to maxIdle.- Parameters:
factory- thePoolableObjectFactoryused to populate the poolmaxIdle- cap on the number of "sleeping" instances in the pool
-
StackObjectPool
Create a new StackObjectPool using the specified
factoryto create new instances, capping the number of "sleeping" instances tomaxIdle, and initially allocating a container capable of containing at leastinitIdleCapacityinstances. The pool is not pre-populated. TheinitIdleCapacityparameter just determines the initial size of the underlying container, which can increase beyond this value ifmaxIdle > initIdleCapacity.Negative values of
maxIdleare ignored (i.e., the pool is created usingDEFAULT_MAX_SLEEPING) as are non-positive values forinitIdleCapacity.- Parameters:
factory- thePoolableObjectFactoryused 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 Details
-
borrowObject
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
makeObjectmethod of the pool'sPoolableObjectFactoryis invoked to create a new instance.All instances are
activatedandvalidatedbefore 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 isdestroyedand 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'smakeObjectmethod 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,NoSuchElementExceptionis thrown. Exceptions thrown byMakeObjectare propagated to the caller; but other thanThreadDeathorVirtualMachineError, exceptions generated by activation, validation or destroy methods are swallowed silently.- Specified by:
borrowObjectin interfaceObjectPool<T>- Specified by:
borrowObjectin classBaseObjectPool<T>- Returns:
- an instance from the pool
- Throws:
Exception- if an instance cannot be obtained from the pool
-
returnObject
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
validationfailspassivationthrows an exception
maxSleepingto 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:
returnObjectin interfaceObjectPool<T>- Specified by:
returnObjectin classBaseObjectPool<T>- Parameters:
obj- instance to return to the pool- Throws:
Exception
-
invalidateObject
Invalidates an object from the pool.
By contract,
objmust 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:
invalidateObjectin interfaceObjectPool<T>- Specified by:
invalidateObjectin classBaseObjectPool<T>- Parameters:
obj- aborrowedinstance to be disposed.- Throws:
Exception
-
getNumIdle
public int getNumIdle()Return the number of instances currently idle in this pool.- Specified by:
getNumIdlein interfaceObjectPool<T>- Overrides:
getNumIdlein 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:
getNumActivein interfaceObjectPool<T>- Overrides:
getNumActivein 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:
clearin interfaceObjectPool<T>- Overrides:
clearin classBaseObjectPool<T>
-
close
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:
closein interfaceObjectPool<T>- Overrides:
closein classBaseObjectPool<T>- Throws:
Exception- never - exceptions clearing the pool are swallowed
-
addObject
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
validatedandpassivated. If validation fails, the new instance isdestroyed. Exceptions generated by the factorymakeObjectorpassivateare 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
maxSleepingto 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:
addObjectin interfaceObjectPool<T>- Overrides:
addObjectin classBaseObjectPool<T>- Throws:
Exception- when thefactoryhas a problem creating or passivating an object.
-
setFactory
Deprecated.to be removed in pool 2.0Sets thefactorythis pool uses to create new instances. Trying to change thefactorywhile there are borrowed objects will throw anIllegalStateException.- Specified by:
setFactoryin interfaceObjectPool<T>- Overrides:
setFactoryin classBaseObjectPool<T>- Parameters:
factory- thePoolableObjectFactoryused to create new instances.- Throws:
IllegalStateException- when the factory cannot be set at this time
-
getFactory
Returns thePoolableObjectFactoryused 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
-
getFactory()