Class SoftReferenceObjectPool<T>
- Type Parameters:
T
- the type of objects held in this pool
- All Implemented Interfaces:
ObjectPool<T>
SoftReference
based
ObjectPool
.- Since:
- Pool 1.0
- Version:
- $Revision: 1222710 $ $Date: 2011-12-23 10:58:12 -0500 (Fri, 23 Dec 2011) $
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate PoolableObjectFactory
<T> private int
Number of active objects.private final List
<SoftReference<T>> My pool.private final ReferenceQueue
<T> Queue of broken references that might be able to be removed from_pool
. -
Constructor Summary
ConstructorsConstructorDescriptionDeprecated.to be removed in pool 2.0.SoftReferenceObjectPool
(PoolableObjectFactory<T> factory) Create aSoftReferenceObjectPool
with 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
Modifier and TypeMethodDescriptionvoid
Create an object, and place it into the pool.Borrow 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.Returns thePoolableObjectFactory
used by this pool to create and manage object instances.int
Return the number of instances currently borrowed from this pool.int
Returns an approximation not less than the of the number of idle instances in the pool.void
invalidateObject
(T obj) Invalidates an object from the pool.private void
If any idle objects were garbage collected, remove theirReference
wrappers from the idle object pool.void
returnObject
(T obj) Returns an instance to the pool after successful validation and passivation.void
setFactory
(PoolableObjectFactory<T> factory) Deprecated.to be removed in pool 2.0Methods inherited from class org.apache.commons.pool.BaseObjectPool
assertOpen, isClosed
-
Field Details
-
_pool
My pool. -
_factory
-
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 _numActiveNumber of active objects.
-
-
Constructor Details
-
SoftReferenceObjectPool
Deprecated.to be removed in pool 2.0. UseSoftReferenceObjectPool(PoolableObjectFactory)
.Create aSoftReferenceObjectPool
without a factory.setFactory
should be called before any attempts to use the pool are made. Generally speaking you should prefer theSoftReferenceObjectPool(PoolableObjectFactory)
constructor.- See Also:
-
SoftReferenceObjectPool
Create aSoftReferenceObjectPool
with the specified factory.- Parameters:
factory
- object factory to use.
-
SoftReferenceObjectPool
@Deprecated public SoftReferenceObjectPool(PoolableObjectFactory<T> factory, int initSize) throws Exception, 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 aSoftReferenceObjectPool
with the specified factory and initial idle object count.- Parameters:
factory
- object factory to use.initSize
- initial size to attempt to prefill the pool.- Throws:
Exception
- when there is a problem prefilling the pool.IllegalArgumentException
- whenfactory
isnull
.
-
-
Method Details
-
borrowObject
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
activated
andvalidated
before being returned by this method. If validation fails or an exception occurs activating or validating an idle instance, the failing instance isdestroyed
and 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'smakeObject
method is used to create a new instance. If 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:
- a valid, activated object instance
- Throws:
NoSuchElementException
- if a valid object cannot be providedIllegalStateException
- if invoked on aclosed
poolException
- if an exception occurs creating a new instance
-
returnObject
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
validation
failspassivation
throws an exception
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
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
-
addObject
Create 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
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.- Specified by:
addObject
in interfaceObjectPool<T>
- Overrides:
addObject
in classBaseObjectPool<T>
- Throws:
IllegalStateException
- if invoked on aclosed
poolException
- when thefactory
has 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:
getNumIdle
in interfaceObjectPool<T>
- Overrides:
getNumIdle
in 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:
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.- Specified by:
clear
in interfaceObjectPool<T>
- Overrides:
clear
in 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:
close
in interfaceObjectPool<T>
- Overrides:
close
in classBaseObjectPool<T>
- Throws:
Exception
- never - exceptions clearing the pool are swallowed
-
setFactory
Deprecated.to be removed in pool 2.0Sets thefactory
this pool uses to create new instances. Trying to change thefactory
while there are borrowed objects will throw anIllegalStateException
.- Specified by:
setFactory
in interfaceObjectPool<T>
- Overrides:
setFactory
in classBaseObjectPool<T>
- Parameters:
factory
- thePoolableObjectFactory
used to create new instances.- Throws:
IllegalStateException
- when the factory cannot be set at this time
-
pruneClearedReferences
private void pruneClearedReferences()If any idle objects were garbage collected, remove theirReference
wrappers from the idle object pool. -
getFactory
Returns thePoolableObjectFactory
used by this pool to create and manage object instances.- Returns:
- the factory
- Since:
- 1.5.5
-