Class SoftReferenceObjectPool<T>
- java.lang.Object
-
- org.apache.commons.pool.BaseObjectPool<T>
-
- org.apache.commons.pool.impl.SoftReferenceObjectPool<T>
-
- Type Parameters:
T- the type of objects held in this pool
- All Implemented Interfaces:
ObjectPool<T>
public class SoftReferenceObjectPool<T> extends BaseObjectPool<T> implements ObjectPool<T>
ASoftReferencebasedObjectPool.- Since:
- Pool 1.0
- Version:
- $Revision: 1222710 $ $Date: 2011-12-23 10:58:12 -0500 (Fri, 23 Dec 2011) $
-
-
Field Summary
Fields Modifier and Type Field Description private PoolableObjectFactory<T>_factoryprivate int_numActiveNumber of active objects.private java.util.List<java.lang.ref.SoftReference<T>>_poolMy pool.private java.lang.ref.ReferenceQueue<T>refQueueQueue of broken references that might be able to be removed from_pool.
-
Constructor Summary
Constructors Constructor Description SoftReferenceObjectPool()Deprecated.to be removed in pool 2.0.SoftReferenceObjectPool(PoolableObjectFactory<T> factory)Create aSoftReferenceObjectPoolwith the specified factory.SoftReferenceObjectPool(PoolableObjectFactory<T> factory, int initSize)Deprecated.because this is a SoftReference pool, prefilled idle obejects may be garbage collected before they are used.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddObject()Create an object, and place it into the pool.TborrowObject()Borrow 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.PoolableObjectFactory<T>getFactory()Returns thePoolableObjectFactoryused by this pool to create and manage object instances.intgetNumActive()Return the number of instances currently borrowed from this pool.intgetNumIdle()Returns an approximation not less than the of the number of idle instances in the pool.voidinvalidateObject(T obj)Invalidates an object from the pool.private voidpruneClearedReferences()If any idle objects were garbage collected, remove theirReferencewrappers from the idle object pool.voidreturnObject(T obj)Returns an instance to the pool after successful validation and passivation.voidsetFactory(PoolableObjectFactory<T> factory)Deprecated.to be removed in pool 2.0-
Methods inherited from class org.apache.commons.pool.BaseObjectPool
assertOpen, isClosed
-
-
-
-
Field Detail
-
_pool
private final java.util.List<java.lang.ref.SoftReference<T>> _pool
My pool.
-
_factory
private PoolableObjectFactory<T> _factory
-
refQueue
private final java.lang.ref.ReferenceQueue<T> refQueue
Queue of broken references that might be able to be removed from_pool. This is used to helpgetNumIdle()be more accurate with minimial performance overhead.
-
_numActive
private int _numActive
Number of active objects.
-
-
Constructor Detail
-
SoftReferenceObjectPool
@Deprecated public SoftReferenceObjectPool()
Deprecated.to be removed in pool 2.0. UseSoftReferenceObjectPool(PoolableObjectFactory).Create aSoftReferenceObjectPoolwithout a factory.setFactoryshould be called before any attempts to use the pool are made. Generally speaking you should prefer theSoftReferenceObjectPool(PoolableObjectFactory)constructor.
-
SoftReferenceObjectPool
public SoftReferenceObjectPool(PoolableObjectFactory<T> factory)
Create aSoftReferenceObjectPoolwith the specified factory.- Parameters:
factory- object factory to use.
-
SoftReferenceObjectPool
@Deprecated public SoftReferenceObjectPool(PoolableObjectFactory<T> factory, int initSize) throws java.lang.Exception, java.lang.IllegalArgumentException
Deprecated.because this is a SoftReference pool, prefilled idle obejects may be garbage collected before they are used. To be removed in Pool 2.0.Create aSoftReferenceObjectPoolwith the specified factory and initial idle object count.- Parameters:
factory- object factory to use.initSize- initial size to attempt to prefill the pool.- Throws:
java.lang.Exception- when there is a problem prefilling the pool.java.lang.IllegalArgumentException- whenfactoryisnull.
-
-
Method Detail
-
borrowObject
public T borrowObject() throws java.lang.Exception
Borrow an object from the pool. If there are no idle instances available in the pool, the configured factory's
PoolableObjectFactory.makeObject()method is invoked to create a new instance.All instances are
activatedandvalidatedbefore being returned by this method. If validation fails or an exception occurs activating or validating an idle instance, the failing instance isdestroyedand another instance is retrieved from the pool, validated and activated. This process continues until either the pool is empty or an instance passes validation. If the pool is empty on activation or it does not contain any valid instances, the factory'smakeObjectmethod is used to create a new instance. If 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:
- a valid, activated object instance
- Throws:
java.util.NoSuchElementException- if a valid object cannot be providedjava.lang.IllegalStateException- if invoked on aclosedpooljava.lang.Exception- if an exception occurs creating a new instance
-
returnObject
public void returnObject(T obj) throws java.lang.Exception
Returns an instance to the pool after successful validation and passivation. The returning instance is destroyed if any of the following are true:
- the pool is closed
validationfailspassivationthrows an exception
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:
java.lang.Exception
-
invalidateObject
public void invalidateObject(T obj) throws java.lang.Exception
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:
java.lang.Exception
-
addObject
public void addObject() throws java.lang.ExceptionCreate an object, and place it into the pool. addObject() 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.- Specified by:
addObjectin interfaceObjectPool<T>- Overrides:
addObjectin classBaseObjectPool<T>- Throws:
java.lang.IllegalStateException- if invoked on aclosedpooljava.lang.Exception- when thefactoryhas a problem creating or passivating an object.
-
getNumIdle
public int getNumIdle()
Returns an approximation not less than the of the number of idle instances in the pool.- Specified by:
getNumIdlein interfaceObjectPool<T>- Overrides:
getNumIdlein classBaseObjectPool<T>- Returns:
- estimated number of idle instances in the 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.- Specified by:
clearin interfaceObjectPool<T>- Overrides:
clearin classBaseObjectPool<T>
-
close
public void close() throws java.lang.ExceptionClose 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:
java.lang.Exception- never - exceptions clearing the pool are swallowed
-
setFactory
@Deprecated public void setFactory(PoolableObjectFactory<T> factory) throws java.lang.IllegalStateException
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:
java.lang.IllegalStateException- when the factory cannot be set at this time
-
pruneClearedReferences
private void pruneClearedReferences()
If any idle objects were garbage collected, remove theirReferencewrappers from the idle object pool.
-
getFactory
public PoolableObjectFactory<T> getFactory()
Returns thePoolableObjectFactoryused by this pool to create and manage object instances.- Returns:
- the factory
- Since:
- 1.5.5
-
-