public class CommonsPool2TargetSource extends AbstractPoolingTargetSource
TargetSource
implementation that holds
objects in a configurable Apache Commons2 Pool.
By default, an instance of GenericObjectPool
is created.
Subclasses may change the type of ObjectPool
used by
overriding the createObjectPool()
method.
Provides many configuration properties mirroring those of the Commons Pool
GenericObjectPool
class; these properties are passed to the
GenericObjectPool
during construction. If creating a subclass of this
class to change the ObjectPool
implementation type, pass in the values
of configuration properties that are relevant to your chosen implementation.
The testOnBorrow
, testOnReturn
and testWhileIdle
properties are explicitly not mirrored because the implementation of
PoolableObjectFactory
used by this class does not implement
meaningful validation. All exposed Commons Pool properties use the
corresponding Commons Pool defaults.
Compatible with Apache Commons Pool 2.4, as of Spring 4.2.
GenericObjectPool
,
createObjectPool()
,
AbstractPoolingTargetSource.setMaxSize(int)
,
setMaxIdle(int)
,
setMinIdle(int)
,
setMaxWait(long)
,
setTimeBetweenEvictionRunsMillis(long)
,
setMinEvictableIdleTimeMillis(long)
,
Serialized FormModifier and Type | Field and Description |
---|---|
private boolean |
blockWhenExhausted |
private int |
maxIdle |
private long |
maxWait |
private long |
minEvictableIdleTimeMillis |
private int |
minIdle |
private ObjectPool |
pool
The Apache Commons
ObjectPool used to pool target objects |
private long |
timeBetweenEvictionRunsMillis |
logger
Constructor and Description |
---|
CommonsPool2TargetSource()
Create a CommonsPoolTargetSource with default settings.
|
Modifier and Type | Method and Description |
---|---|
void |
activateObject(<any> p) |
protected ObjectPool |
createObjectPool()
Subclasses can override this if they want to return a specific Commons pool.
|
protected void |
createPool()
Creates and holds an ObjectPool instance.
|
void |
destroy()
Closes the underlying
ObjectPool when destroying this object. |
void |
destroyObject(<any> p) |
int |
getActiveCount()
Return the number of active objects in the pool.
|
int |
getIdleCount()
Return the number of idle objects in the pool.
|
int |
getMaxIdle()
Return the maximum number of idle objects in the pool.
|
long |
getMaxWait()
Return the maximum waiting time for fetching an object from the pool.
|
long |
getMinEvictableIdleTimeMillis()
Return the minimum time that an idle object can sit in the pool.
|
int |
getMinIdle()
Return the minimum number of idle objects in the pool.
|
java.lang.Object |
getTarget()
Borrows an object from the
ObjectPool . |
long |
getTimeBetweenEvictionRunsMillis()
Return the time between eviction runs that check idle objects.
|
boolean |
isBlockWhenExhausted()
Specify if the call should block when the pool is exhausted.
|
<any> |
makeObject() |
void |
passivateObject(<any> p) |
void |
releaseTarget(java.lang.Object target)
Returns the specified object to the underlying
ObjectPool . |
void |
setBlockWhenExhausted(boolean blockWhenExhausted)
Set whether the call should bock when the pool is exhausted.
|
void |
setMaxIdle(int maxIdle)
Set the maximum number of idle objects in the pool.
|
void |
setMaxWait(long maxWait)
Set the maximum waiting time for fetching an object from the pool.
|
void |
setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
Set the minimum time that an idle object can sit in the pool before
it becomes subject to eviction.
|
void |
setMinIdle(int minIdle)
Set the minimum number of idle objects in the pool.
|
void |
setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)
Set the time between eviction runs that check idle objects whether
they have been idle for too long or have become invalid.
|
boolean |
validateObject(<any> p) |
getMaxSize, getPoolingConfigMixin, setBeanFactory, setMaxSize
destroyPrototypeInstance, newPrototypeInstance, writeReplace
copyFrom, equals, getBeanFactory, getTargetBeanName, getTargetClass, hashCode, isStatic, setTargetBeanName, setTargetClass, toString
private int maxIdle
private int minIdle
private long maxWait
private long timeBetweenEvictionRunsMillis
private long minEvictableIdleTimeMillis
private boolean blockWhenExhausted
private ObjectPool pool
ObjectPool
used to pool target objectspublic CommonsPool2TargetSource()
AbstractPoolingTargetSource.setMaxSize(int)
,
GenericObjectPoolConfig#setMaxTotal
public void setMaxIdle(int maxIdle)
GenericObjectPool#setMaxIdle
public int getMaxIdle()
public void setMinIdle(int minIdle)
GenericObjectPool#setMinIdle
public int getMinIdle()
public void setMaxWait(long maxWait)
GenericObjectPool#setMaxWaitMillis
public long getMaxWait()
public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)
GenericObjectPool#setTimeBetweenEvictionRunsMillis
public long getTimeBetweenEvictionRunsMillis()
public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
Note that eviction runs need to be performed to take this setting into effect.
setTimeBetweenEvictionRunsMillis(long)
,
GenericObjectPool#setMinEvictableIdleTimeMillis
public long getMinEvictableIdleTimeMillis()
public void setBlockWhenExhausted(boolean blockWhenExhausted)
public boolean isBlockWhenExhausted()
protected final void createPool()
createPool
in class AbstractPoolingTargetSource
createObjectPool()
protected ObjectPool createObjectPool()
Default is a GenericObjectPool instance with the given pool size.
ObjectPool
.GenericObjectPool
,
AbstractPoolingTargetSource.setMaxSize(int)
public java.lang.Object getTarget() throws java.lang.Exception
ObjectPool
.getTarget
in interface TargetSource
getTarget
in class AbstractPoolingTargetSource
java.lang.Exception
- we may need to deal with checked exceptions from pool
APIs, so we're forgiving with our exception signaturepublic void releaseTarget(java.lang.Object target) throws java.lang.Exception
ObjectPool
.releaseTarget
in interface TargetSource
releaseTarget
in class AbstractPoolingTargetSource
target
- object that must have been acquired from the pool
via a call to getTarget()
java.lang.Exception
- to allow pooling APIs to throw exceptionAbstractPoolingTargetSource.getTarget()
public int getActiveCount() throws java.lang.UnsupportedOperationException
PoolingConfig
java.lang.UnsupportedOperationException
- if not supported by the poolpublic int getIdleCount() throws java.lang.UnsupportedOperationException
PoolingConfig
java.lang.UnsupportedOperationException
- if not supported by the poolpublic void destroy() throws java.lang.Exception
ObjectPool
when destroying this object.java.lang.Exception
- in case of shutdown errors.
Exceptions will get logged but not rethrown to allow
other beans to release their resources too.public <any> makeObject() throws java.lang.Exception
java.lang.Exception
public void destroyObject(<any> p) throws java.lang.Exception
java.lang.Exception
public boolean validateObject(<any> p)
public void activateObject(<any> p) throws java.lang.Exception
java.lang.Exception
public void passivateObject(<any> p) throws java.lang.Exception
java.lang.Exception