Top Description Fields Constructors Methods
org.springframework.aop.target

public Class CommonsPool2TargetSource

extends AbstractPoolingTargetSource
implements PooledObjectFactory<Object>
Class Inheritance
All Implemented Interfaces
org.apache.commons.pool2.PooledObjectFactory
Annotations
@SuppressWarnings:rawtypes, unchecked, serial
Imports
org.apache.commons.pool2.ObjectPool, .PooledObject, .PooledObjectFactory, org.apache.commons.pool2.impl.DefaultPooledObject, .GenericObjectPool, .GenericObjectPoolConfig, org.springframework.lang.Nullable, org.springframework.util.Assert

org.springframework.aop.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.

Authors
Rod Johnson, Rob Harrop, Juergen Hoeller, Stephane Nicoll, Kazuki Shimizu
Since
4.2
See Also
GenericObjectPool, createObjectPool(), setMaxSize, setMaxIdle, setMinIdle, setMaxWait, setTimeBetweenEvictionRunsMillis, setMinEvictableIdleTimeMillis

Field Summary

Modifier and TypeField and Description
private boolean
private int
private long
private long
private int
private ObjectPool<T>
pool

The Apache Commons ObjectPool used to pool target objects.

private long

Constructor Summary

AccessConstructor and Description
public
CommonsPool2TargetSource()

Create a CommonsPoolTargetSource with default settings.

Method Summary

Modifier and TypeMethod and Description
public void
activateObject(PooledObject<Object>
a PooledObject wrapping the instance to be activated
p
)

Implements org.apache.commons.pool2.PooledObjectFactory.activateObject.

Reinitializes an instance to be returned by the pool.
protected ObjectPool<T>

Returns:

an empty Commons ObjectPool.
createObjectPool
()

Subclasses can override this if they want to return a specific Commons pool.

protected final void
createPool()

Implements abstract org.springframework.aop.target.AbstractPoolingTargetSource.createPool.

Creates and holds an ObjectPool instance.
public void
destroy()

Implements org.springframework.beans.factory.DisposableBean.destroy.

Closes the underlying ObjectPool when destroying this object.
public void
destroyObject(PooledObject<Object>
a PooledObject wrapping the instance to be destroyed
p
)

Implements org.apache.commons.pool2.PooledObjectFactory.destroyObject.

Destroys an instance no longer needed by the pool, using the default (NORMAL) DestroyMode.
public int
getActiveCount()

Implements org.springframework.aop.target.PoolingConfig.getActiveCount.

Return the number of active objects in the pool.
public int
getIdleCount()

Implements org.springframework.aop.target.PoolingConfig.getIdleCount.

Return the number of idle objects in the pool.
public int
getMaxIdle()

Return the maximum number of idle objects in the pool.

public long
getMaxWait()

Return the maximum waiting time for fetching an object from the pool.

public long
getMinEvictableIdleTimeMillis()

Return the minimum time that an idle object can sit in the pool.

public int
getMinIdle()

Return the minimum number of idle objects in the pool.

public Object
public long
getTimeBetweenEvictionRunsMillis()

Return the time between eviction runs that check idle objects.

public boolean
isBlockWhenExhausted()

Specify if the call should block when the pool is exhausted.

public PooledObject<Object>
makeObject()

Implements org.apache.commons.pool2.PooledObjectFactory.makeObject.

Creates an instance that can be served by the pool and wrap it in a PooledObject to be managed by the pool.
public void
passivateObject(PooledObject<Object>
a PooledObject wrapping the instance to be passivated
p
)

Implements org.apache.commons.pool2.PooledObjectFactory.passivateObject.

Uninitializes an instance to be returned to the idle object pool.
public void
releaseTarget(Object
object that must have been acquired from the pool via a call to getTarget()
target
)

Overrides default org.springframework.aop.TargetSource.releaseTarget.

Implements abstract org.springframework.aop.target.AbstractPoolingTargetSource.releaseTarget.

Returns the specified object to the underlying ObjectPool.
public void
setBlockWhenExhausted(boolean blockWhenExhausted)

Set whether the call should block when the pool is exhausted.

public void
setMaxIdle(int maxIdle)

Set the maximum number of idle objects in the pool.

public void
setMaxWait(long maxWait)

Set the maximum waiting time for fetching an object from the pool.

public void
setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)

Set the minimum time that an idle object can sit in the pool before it becomes subject to eviction.

public void
setMinIdle(int minIdle)

Set the minimum number of idle objects in the pool.

public 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.

public boolean
validateObject(PooledObject<Object>
a PooledObject wrapping the instance to be validated
p
)

Implements org.apache.commons.pool2.PooledObjectFactory.validateObject.

Ensures that the instance is safe to be returned by the pool.
Inherited from org.springframework.aop.target.AbstractPoolingTargetSource:
getMaxSizegetPoolingConfigMixinsetBeanFactorysetMaxSize

Field Detail

blockWhenExhaustedback to summary
private boolean blockWhenExhausted
maxIdleback to summary
private int maxIdle
maxWaitback to summary
private long maxWait
minEvictableIdleTimeMillisback to summary
private long minEvictableIdleTimeMillis
minIdleback to summary
private int minIdle
poolback to summary
private ObjectPool<T> pool

The Apache Commons ObjectPool used to pool target objects.

Annotations
@Nullable
timeBetweenEvictionRunsMillisback to summary
private long timeBetweenEvictionRunsMillis

Constructor Detail

CommonsPool2TargetSourceback to summary
public CommonsPool2TargetSource()

Create a CommonsPoolTargetSource with default settings. Default maximum size of the pool is 8.

See Also
setMaxSize, GenericObjectPoolConfig#setMaxTotal

Method Detail

activateObjectback to summary
public void activateObject(PooledObject<Object> p) throws Exception

Implements org.apache.commons.pool2.PooledObjectFactory.activateObject.

Doc from org.apache.commons.pool2.PooledObjectFactory.activateObject.

Reinitializes an instance to be returned by the pool.

Parameters
p:PooledObject<Object>

a PooledObject wrapping the instance to be activated

Annotations
@Override
Exceptions
Exception:
if there is a problem activating obj, this exception may be swallowed by the pool.
createObjectPoolback to summary
protected ObjectPool<T> createObjectPool()

Subclasses can override this if they want to return a specific Commons pool. They should apply any configuration properties to the pool here.

Default is a GenericObjectPool instance with the given pool size.

Returns:ObjectPool<T>

an empty Commons ObjectPool.

See Also
GenericObjectPool, setMaxSize
createPoolback to summary
protected final void createPool()

Implements abstract org.springframework.aop.target.AbstractPoolingTargetSource.createPool.

Creates and holds an ObjectPool instance.

Annotations
@Override
See Also
createObjectPool()
destroyback to summary
public void destroy() throws Exception

Implements org.springframework.beans.factory.DisposableBean.destroy.

Closes the underlying ObjectPool when destroying this object.

Annotations
@Override
Exceptions
Exception:

Doc from org.springframework.beans.factory.DisposableBean.destroy.

in case of shutdown errors. Exceptions will get logged but not rethrown to allow other beans to release their resources as well.

destroyObjectback to summary
public void destroyObject(PooledObject<Object> p) throws Exception

Implements org.apache.commons.pool2.PooledObjectFactory.destroyObject.

Doc from org.apache.commons.pool2.PooledObjectFactory.destroyObject.

Destroys an instance no longer needed by the pool, using the default (NORMAL) DestroyMode.

It is important for implementations of this method to be aware that there is no guarantee about what state obj will be in and the implementation should be prepared to handle unexpected errors.

Also, an implementation must take in to consideration that instances lost to the garbage collector may never be destroyed.

Parameters
p:PooledObject<Object>

a PooledObject wrapping the instance to be destroyed

Annotations
@Override
Exceptions
Exception:
should be avoided as it may be swallowed by the pool implementation.
getActiveCountback to summary
public int getActiveCount() throws UnsupportedOperationException

Implements org.springframework.aop.target.PoolingConfig.getActiveCount.

Doc from org.springframework.aop.target.PoolingConfig.getActiveCount.

Return the number of active objects in the pool.

Annotations
@Override
Exceptions
UnsupportedOperationException:
if not supported by the pool
getIdleCountback to summary
public int getIdleCount() throws UnsupportedOperationException

Implements org.springframework.aop.target.PoolingConfig.getIdleCount.

Doc from org.springframework.aop.target.PoolingConfig.getIdleCount.

Return the number of idle objects in the pool.

Annotations
@Override
Exceptions
UnsupportedOperationException:
if not supported by the pool
getMaxIdleback to summary
public int getMaxIdle()

Return the maximum number of idle objects in the pool.

getMaxWaitback to summary
public long getMaxWait()

Return the maximum waiting time for fetching an object from the pool.

getMinEvictableIdleTimeMillisback to summary
public long getMinEvictableIdleTimeMillis()

Return the minimum time that an idle object can sit in the pool.

getMinIdleback to summary
public int getMinIdle()

Return the minimum number of idle objects in the pool.

getTargetback to summary
public Object getTarget() throws Exception

Implements abstract org.springframework.aop.target.AbstractPoolingTargetSource.getTarget.

Implements org.springframework.aop.TargetSource.getTarget.

Borrows an object from the ObjectPool.

Returns:Object

Doc from org.springframework.aop.target.AbstractPoolingTargetSource.getTarget.

an object from the pool

Annotations
@Override
Exceptions
Exception:

Doc from org.springframework.aop.target.AbstractPoolingTargetSource.getTarget.

we may need to deal with checked exceptions from pool APIs, so we're forgiving with our exception signature

getTimeBetweenEvictionRunsMillisback to summary
public long getTimeBetweenEvictionRunsMillis()

Return the time between eviction runs that check idle objects.

isBlockWhenExhaustedback to summary
public boolean isBlockWhenExhausted()

Specify if the call should block when the pool is exhausted.

makeObjectback to summary
public PooledObject<Object> makeObject() throws Exception

Implements org.apache.commons.pool2.PooledObjectFactory.makeObject.

Doc from org.apache.commons.pool2.PooledObjectFactory.makeObject.

Creates an instance that can be served by the pool and wrap it in a PooledObject to be managed by the pool.

Returns:PooledObject<Object>

a PooledObject wrapping an instance that can be served by the pool, not null.

Annotations
@Override
Exceptions
Exception:
if there is a problem creating a new instance, this will be propagated to the code requesting an object.
passivateObjectback to summary
public void passivateObject(PooledObject<Object> p) throws Exception

Implements org.apache.commons.pool2.PooledObjectFactory.passivateObject.

Doc from org.apache.commons.pool2.PooledObjectFactory.passivateObject.

Uninitializes an instance to be returned to the idle object pool.

Parameters
p:PooledObject<Object>

a PooledObject wrapping the instance to be passivated

Annotations
@Override
Exceptions
Exception:
if there is a problem passivating obj, this exception may be swallowed by the pool.
releaseTargetback to summary
public void releaseTarget(Object target) throws Exception

Overrides default org.springframework.aop.TargetSource.releaseTarget.

Implements abstract org.springframework.aop.target.AbstractPoolingTargetSource.releaseTarget.

Returns the specified object to the underlying ObjectPool.

Parameters
target:Object

Doc from org.springframework.aop.target.AbstractPoolingTargetSource.releaseTarget.

object that must have been acquired from the pool via a call to getTarget()

Annotations
@Override
Exceptions
Exception:

Doc from org.springframework.aop.target.AbstractPoolingTargetSource.releaseTarget.

to allow pooling APIs to throw exception

setBlockWhenExhaustedback to summary
public void setBlockWhenExhausted(boolean blockWhenExhausted)

Set whether the call should block when the pool is exhausted.

setMaxIdleback to summary
public void setMaxIdle(int maxIdle)

Set the maximum number of idle objects in the pool. Default is 8.

See Also
GenericObjectPool#setMaxIdle
setMaxWaitback to summary
public void setMaxWait(long maxWait)

Set the maximum waiting time for fetching an object from the pool. Default is -1, waiting forever.

See Also
GenericObjectPool#setMaxWaitMillis
setMinEvictableIdleTimeMillisback to summary
public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)

Set the minimum time that an idle object can sit in the pool before it becomes subject to eviction. Default is 1800000 (30 minutes).

Note that eviction runs need to be performed to take this setting into effect.

See Also
setTimeBetweenEvictionRunsMillis, GenericObjectPool#setMinEvictableIdleTimeMillis
setMinIdleback to summary
public void setMinIdle(int minIdle)

Set the minimum number of idle objects in the pool. Default is 0.

See Also
GenericObjectPool#setMinIdle
setTimeBetweenEvictionRunsMillisback to summary
public 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. Default is -1, not performing any eviction.

See Also
GenericObjectPool#setTimeBetweenEvictionRunsMillis
validateObjectback to summary
public boolean validateObject(PooledObject<Object> p)

Implements org.apache.commons.pool2.PooledObjectFactory.validateObject.

Doc from org.apache.commons.pool2.PooledObjectFactory.validateObject.

Ensures that the instance is safe to be returned by the pool.

Parameters
p:PooledObject<Object>

a PooledObject wrapping the instance to be validated

Returns:boolean

false if obj is not valid and should be dropped from the pool, true otherwise.

Annotations
@Override