Class StackKeyedObjectPool<K,​V>

  • Type Parameters:
    K - the type of keys in this pool
    V - the type of objects held in this pool
    All Implemented Interfaces:
    KeyedObjectPool<K,​V>

    public class StackKeyedObjectPool<K,​V>
    extends BaseKeyedObjectPool<K,​V>
    implements KeyedObjectPool<K,​V>
    A simple, Stack-based KeyedObjectPool implementation.

    Given a KeyedPoolableObjectFactory, this class will maintain a simple pool of instances. A finite number of "sleeping" or inactive instances is enforced, but when the pool is empty, new instances are created to support the new load. Hence this class places no limit on the number of "active" instances created by the pool, but is quite useful for re-using Objects without introducing artificial limits.

    Since:
    Pool 1.0
    Version:
    $Revision: 1222710 $ $Date: 2011-12-23 10:58:12 -0500 (Fri, 23 Dec 2011) $
    Author:
    Rodney Waldhoff, Sandy McArthur
    See Also:
    Stack
    • Method Detail

      • returnObject

        public void returnObject​(K key,
                                 V obj)
                          throws Exception
        Returns obj to the pool under key. If adding the returning instance to the pool results in maxSleeping exceeded for the given key, the oldest instance in the idle object pool is destroyed to make room for the returning instance.
        Specified by:
        returnObject in interface KeyedObjectPool<K,​V>
        Specified by:
        returnObject in class BaseKeyedObjectPool<K,​V>
        Parameters:
        key - the pool key
        obj - returning instance
        Throws:
        Exception
      • invalidateObject

        public void invalidateObject​(K key,
                                     V obj)
                              throws Exception

        Invalidates an object from the pool.

        By contract, obj must have been obtained using borrowObject using a key that is equivalent to the one used to borrow the Object in the first place.

        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 interface KeyedObjectPool<K,​V>
        Specified by:
        invalidateObject in class BaseKeyedObjectPool<K,​V>
        Parameters:
        key - the key used to obtain the object
        obj - a borrowed instance to be returned.
        Throws:
        Exception
      • getNumIdle

        public int getNumIdle()
        Returns the total number of instances currently idle in this pool.
        Specified by:
        getNumIdle in interface KeyedObjectPool<K,​V>
        Overrides:
        getNumIdle in class BaseKeyedObjectPool<K,​V>
        Returns:
        the total number of instances currently idle in this pool
      • getNumActive

        public int getNumActive()
        Returns the total number of instances current borrowed from this pool but not yet returned.
        Specified by:
        getNumActive in interface KeyedObjectPool<K,​V>
        Overrides:
        getNumActive in class BaseKeyedObjectPool<K,​V>
        Returns:
        the total number of instances currently borrowed from this pool
      • getNumActive

        public int getNumActive​(K key)
        Returns the number of instances currently borrowed from but not yet returned to the pool corresponding to the given key.
        Specified by:
        getNumActive in interface KeyedObjectPool<K,​V>
        Overrides:
        getNumActive in class BaseKeyedObjectPool<K,​V>
        Parameters:
        key - the key to query
        Returns:
        the number of instances corresponding to the given key currently borrowed in this pool
      • getNumIdle

        public int getNumIdle​(K key)
        Returns the number of instances corresponding to the given key currently idle in this pool.
        Specified by:
        getNumIdle in interface KeyedObjectPool<K,​V>
        Overrides:
        getNumIdle in class BaseKeyedObjectPool<K,​V>
        Parameters:
        key - the key to query
        Returns:
        the number of instances corresponding to the given key currently idle in this pool
      • clear

        public void clear​(K key)
        Clears the specified pool, removing all pooled instances corresponding to the given key.
        Specified by:
        clear in interface KeyedObjectPool<K,​V>
        Overrides:
        clear in class BaseKeyedObjectPool<K,​V>
        Parameters:
        key - the key to clear
      • toString

        public String toString()
        Returns a string representation of this StackKeyedObjectPool, including the number of pools, the keys and the size of each keyed pool.
        Overrides:
        toString in class Object
        Returns:
        Keys and pool sizes
      • getPools

        public Map<K,​Stack<V>> getPools()
        Returns:
        map of keyed pools
        Since:
        1.5.5
      • getMaxSleeping

        public int getMaxSleeping()
        Returns:
        the cap on the number of "sleeping" instances in each pool.
        Since:
        1.5.5
      • getInitSleepingCapacity

        public int getInitSleepingCapacity()
        Returns:
        the initial capacity of each pool.
        Since:
        1.5.5
      • getTotActive

        public int getTotActive()
        Returns:
        the _totActive
      • getTotIdle

        public int getTotIdle()
        Returns:
        the _totIdle
      • getActiveCount

        public Map<K,​Integer> getActiveCount()
        Returns:
        the _activeCount
        Since:
        1.5.5