Package org.springframework.aop.target

# Class CommonsPool2TargetSource

java.lang.Object
org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource
org.springframework.aop.target.AbstractPrototypeBasedTargetSource
org.springframework.aop.target.AbstractPoolingTargetSource
org.springframework.aop.target.CommonsPool2TargetSource

All Implemented Interfaces:
  `Serializable`, `org.apache.commons.pool2.PooledObjectFactory<Object>`, `PoolingConfig`, `TargetClassAware`, `TargetSource`, `org.springframework.beans.factory.Aware`, `org.springframework.beans.factory.BeanFactoryAware`, `org.springframework.beans.factory.DisposableBean`

public class CommonsPool2TargetSource extends AbstractPoolingTargetSource implements org.apache.commons.pool2.PooledObjectFactory<Object>
`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.

Since:
  4.2
Author:
  Rod Johnson, Rob Harrop, Juergen Hoeller, Stephane Nicoll, Kazuki Shimizu
See Also:
  - `GenericObjectPool`
  - `createObjectPool()`
  - `AbstractPoolingTargetSource.setMaxSize(int)`
  - `setMaxIdle(int)`
  - `setMinIdle(int)`
  - `setMaxWait(long)`
  - `setTimeBetweenEvictionRunsMillis(long)`
  - `setMinEvictableIdleTimeMillis(long)`
  - Serialized Form

## Field Summary

### Fields inherited from class org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource

`logger, targetBeanName`

## Constructor Summary

Constructors
Constructor
Description
`CommonsPool2TargetSource()`
Create a CommonsPoolTargetSource with default settings.

## Method Summary

Modifier and Type
Method
Description
`void`
`activateObject(org.apache.commons.pool2.PooledObject<Object> p)`
`protected org.apache.commons.pool2.ObjectPool`
`createObjectPool()`
Subclasses can override this if they want to return a specific Commons pool.
`protected final void`
`createPool()`
Creates and holds an ObjectPool instance.
`void`
`destroy()`
Closes the underlying `ObjectPool` when destroying this object.
`void`
`destroyObject(org.apache.commons.pool2.PooledObject<Object> 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.
`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.
`org.apache.commons.pool2.PooledObject<Object>`
`makeObject()`
`void`
`passivateObject(org.apache.commons.pool2.PooledObject<Object> p)`
`void`
`releaseTarget(Object target)`
Returns the specified object to the underlying `ObjectPool`.
`void`
`setBlockWhenExhausted(boolean blockWhenExhausted)`
Set whether the call should block 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(org.apache.commons.pool2.PooledObject<Object> p)`

### Methods inherited from class org.springframework.aop.target.AbstractPoolingTargetSource

`getMaxSize, getPoolingConfigMixin, setBeanFactory, setMaxSize`

### Methods inherited from class org.springframework.aop.target.AbstractPrototypeBasedTargetSource

`destroyPrototypeInstance, newPrototypeInstance, writeReplace`

### Methods inherited from class org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource

`copyFrom, equals, getBeanFactory, getTargetBeanName, getTargetClass, hashCode, setTargetBeanName, setTargetClass, toString`

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

### Methods inherited from interface org.apache.commons.pool2.PooledObjectFactory

`destroyObject`

### Methods inherited from interface org.springframework.aop.TargetSource

`isStatic`

## Constructor Details

### CommonsPool2TargetSource

public CommonsPool2TargetSource()
Create a CommonsPoolTargetSource with default settings. Default maximum size of the pool is 8.

See Also:
  - `AbstractPoolingTargetSource.setMaxSize(int)`
  - `GenericObjectPoolConfig.setMaxTotal(int)`

## Method Details

### setMaxIdle

public void setMaxIdle(int maxIdle)
Set the maximum number of idle objects in the pool. Default is 8.

See Also:
  - `GenericObjectPool.setMaxIdle(int)`

### getMaxIdle

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

### setMinIdle

public void setMinIdle(int minIdle)
Set the minimum number of idle objects in the pool. Default is 0.

See Also:
  - `GenericObjectPool.setMinIdle(int)`

### getMinIdle

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

### setMaxWait

public void setMaxWait(long maxWait)
Set the maximum waiting time for fetching an object from the pool. Default is -1, waiting forever.

See Also:
  - `BaseGenericObjectPool.setMaxWaitMillis(long)`

### getMaxWait

public long getMaxWait()
Return the maximum waiting time for fetching an object from the pool.

### setTimeBetweenEvictionRunsMillis

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:
  - `BaseGenericObjectPool.setTimeBetweenEvictionRunsMillis(long)`

### getTimeBetweenEvictionRunsMillis

public long getTimeBetweenEvictionRunsMillis()
Return the time between eviction runs that check idle objects.

### setMinEvictableIdleTimeMillis

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(long)`
  - `BaseGenericObjectPool.setMinEvictableIdleTimeMillis(long)`

### getMinEvictableIdleTimeMillis

public long getMinEvictableIdleTimeMillis()
Return the minimum time that an idle object can sit in the pool.

### setBlockWhenExhausted

public void setBlockWhenExhausted(boolean blockWhenExhausted)
Set whether the call should block when the pool is exhausted.

### isBlockWhenExhausted

public boolean isBlockWhenExhausted()
Specify if the call should block when the pool is exhausted.

### createPool

protected final void createPool()
Creates and holds an ObjectPool instance.

Specified by:
  `createPool` in class `AbstractPoolingTargetSource`
See Also:
  - `createObjectPool()`

### createObjectPool

protected org.apache.commons.pool2.ObjectPool 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:
  an empty Commons `ObjectPool`.
See Also:
  - `GenericObjectPool`
  - `AbstractPoolingTargetSource.setMaxSize(int)`

### getTarget

public Object getTarget() throws Exception
Borrows an object from the `ObjectPool`.

Specified by:
  `getTarget` in interface `TargetSource`
Specified by:
  `getTarget` in class `AbstractPoolingTargetSource`
Returns:
  an object from the pool
Throws:
  `Exception` - we may need to deal with checked exceptions from pool APIs, so we're forgiving with our exception signature

### releaseTarget

public void releaseTarget(Object target) throws Exception
Returns the specified object to the underlying `ObjectPool`.

Specified by:
  `releaseTarget` in interface `TargetSource`
Specified by:
  `releaseTarget` in class `AbstractPoolingTargetSource`
Parameters:
  `target` - object that must have been acquired from the pool via a call to `getTarget()`
Throws:
  `Exception` - to allow pooling APIs to throw exception
See Also:
  - `AbstractPoolingTargetSource.getTarget()`

### getActiveCount

public int getActiveCount() throws UnsupportedOperationException
Description copied from interface: `PoolingConfig`
Return the number of active objects in the pool.

Specified by:
  `getActiveCount` in interface `PoolingConfig`
Throws:
  `UnsupportedOperationException` - if not supported by the pool

### getIdleCount

public int getIdleCount() throws UnsupportedOperationException
Description copied from interface: `PoolingConfig`
Return the number of idle objects in the pool.

Specified by:
  `getIdleCount` in interface `PoolingConfig`
Throws:
  `UnsupportedOperationException` - if not supported by the pool

### destroy

public void destroy() throws Exception
Closes the underlying `ObjectPool` when destroying this object.

Specified by:
  `destroy` in interface `org.springframework.beans.factory.DisposableBean`
Throws:
  `Exception`

### makeObject

public org.apache.commons.pool2.PooledObject<Object> makeObject() throws Exception

Specified by:
  `makeObject` in interface `org.apache.commons.pool2.PooledObjectFactory<Object>`
Throws:
  `Exception`

### destroyObject

public void destroyObject(org.apache.commons.pool2.PooledObject<Object> p) throws Exception

Specified by:
  `destroyObject` in interface `org.apache.commons.pool2.PooledObjectFactory<Object>`
Throws:
  `Exception`

### validateObject

public boolean validateObject(org.apache.commons.pool2.PooledObject<Object> p)

Specified by:
  `validateObject` in interface `org.apache.commons.pool2.PooledObjectFactory<Object>`

### activateObject

public void activateObject(org.apache.commons.pool2.PooledObject<Object> p) throws Exception

Specified by:
  `activateObject` in interface `org.apache.commons.pool2.PooledObjectFactory<Object>`
Throws:
  `Exception`

### passivateObject

public void passivateObject(org.apache.commons.pool2.PooledObject<Object> p) throws Exception

Specified by:
  `passivateObject` in interface `org.apache.commons.pool2.PooledObjectFactory<Object>`
Throws:
  `Exception`