Class ProxiedObjectPool<T>
- java.lang.Object
-
- org.apache.commons.pool2.proxy.ProxiedObjectPool<T>
-
- Type Parameters:
T- type of the pooled object
- All Implemented Interfaces:
Closeable,AutoCloseable,ObjectPool<T>
public class ProxiedObjectPool<T> extends Object implements ObjectPool<T>
Create a new object pool where the pooled objects are wrapped in proxies allowing better control of pooled objects and in particular the prevention of the continued use of an object by a client after that client returns the object to the pool.- Since:
- 2.0
-
-
Constructor Summary
Constructors Constructor Description ProxiedObjectPool(ObjectPool<T> pool, org.apache.commons.pool2.proxy.ProxySource<T> proxySource)Constructs a new proxied object pool.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddObject()Creates an object using thefactoryor other implementation dependent mechanism, passivate it, and then place it in the idle object pool.TborrowObject()Borrows an instance from this pool.voidclear()Clears any objects sitting idle in the pool, releasing any associated resources (optional operation).voidclose()Closes this pool, and free any resources associated with it.intgetNumActive()Gets the number of instances currently borrowed from this pool.intgetNumIdle()Gets the number of instances currently idle in this pool.voidinvalidateObject(T proxy)Invalidates an object from the pool.voidreturnObject(T proxy)Returns an instance to the pool.StringtoString()-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.commons.pool2.ObjectPool
addObjects, invalidateObject
-
-
-
-
Constructor Detail
-
ProxiedObjectPool
public ProxiedObjectPool(ObjectPool<T> pool, org.apache.commons.pool2.proxy.ProxySource<T> proxySource)
Constructs a new proxied object pool.- Parameters:
pool- The object pool to wrapproxySource- The source of the proxy objects
-
-
Method Detail
-
addObject
public void addObject() throws Exception
Description copied from interface:ObjectPoolCreates an object using thefactoryor other implementation dependent mechanism, passivate it, and then place it in the idle object pool.addObjectis useful for "pre-loading" a pool with idle objects. (Optional operation).- Specified by:
addObjectin interfaceObjectPool<T>- Throws:
Exception- whenPooledObjectFactory.makeObject()fails.IllegalStateException- afterObjectPool.close()has been called on this pool.UnsupportedOperationException- when this pool cannot add new idle objects.
-
borrowObject
public T borrowObject() throws Exception
Description copied from interface:ObjectPoolBorrows an instance from this pool.Instances returned from this method will have been either newly created with
PooledObjectFactory.makeObject()or will be a previously idle object and have been activated withPooledObjectFactory.activateObject(org.apache.commons.pool2.PooledObject<T>)and then validated withPooledObjectFactory.validateObject(org.apache.commons.pool2.PooledObject<T>).By contract, clients must return the borrowed instance using
ObjectPool.returnObject(T),ObjectPool.invalidateObject(T), or a related method as defined in an implementation or sub-interface.The behavior of this method when the pool has been exhausted is not strictly specified (although it may be specified by implementations).
- Specified by:
borrowObjectin interfaceObjectPool<T>- Returns:
- an instance from this pool.
- Throws:
IllegalStateException- afterclosehas been called on this pool.Exception- whenPooledObjectFactory.makeObject()throws an exception.NoSuchElementException- when the pool is exhausted and cannot or will not return another instance.
-
clear
public void clear() throws Exception
Description copied from interface:ObjectPoolClears any objects sitting idle in the pool, releasing any associated resources (optional operation). Idle objects cleared must bePooledObjectFactory.destroyObject(PooledObject).- Specified by:
clearin interfaceObjectPool<T>- Throws:
UnsupportedOperationException- if this implementation does not support the operationException- if the pool cannot be cleared
-
close
public void close()
Description copied from interface:ObjectPoolCloses this pool, and free any resources associated with it.Calling
ObjectPool.addObject()orObjectPool.borrowObject()after invoking this method on a pool will cause them to throw anIllegalStateException.Implementations should silently fail if not all resources can be freed.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceObjectPool<T>
-
getNumActive
public int getNumActive()
Description copied from interface:ObjectPoolGets the number of instances currently borrowed from this pool. Returns a negative value if this information is not available.- Specified by:
getNumActivein interfaceObjectPool<T>- Returns:
- the number of instances currently borrowed from this pool.
-
getNumIdle
public int getNumIdle()
Description copied from interface:ObjectPoolGets the number of instances currently idle in this pool. This may be considered an approximation of the number of objects that can beborrowedwithout creating any new instances. Returns a negative value if this information is not available.- Specified by:
getNumIdlein interfaceObjectPool<T>- Returns:
- the number of instances currently idle in this pool.
-
invalidateObject
public void invalidateObject(T proxy) throws Exception
Description copied from interface:ObjectPoolInvalidates an object from the pool.By contract,
objmust have been obtained usingObjectPool.borrowObject()or a related method as defined in an implementation or sub-interface.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>- Parameters:
proxy- aborrowedinstance to be disposed.- Throws:
Exception- if the instance cannot be invalidated
-
returnObject
public void returnObject(T proxy) throws Exception
Description copied from interface:ObjectPoolReturns an instance to the pool. By contract,objmust have been obtained usingObjectPool.borrowObject()or a related method as defined in an implementation or sub-interface.- Specified by:
returnObjectin interfaceObjectPool<T>- Parameters:
proxy- aborrowedinstance to be returned.- Throws:
IllegalStateException- if an attempt is made to return an object to the pool that is in any state other than allocated (i.e. borrowed). Attempting to return an object more than once or attempting to return an object that was never borrowed from the pool will trigger this exception.Exception- if an instance cannot be returned to the pool
-
-