Class SoftReferenceObjectPool<T>

  • Type Parameters:
    T - the type of objects held in this pool
    All Implemented Interfaces:
    ObjectPool<T>

    public class SoftReferenceObjectPool<T>
    extends BaseObjectPool<T>
    implements ObjectPool<T>
    Since:
    Pool 1.0
    Version:
    $Revision: 1222710 $ $Date: 2011-12-23 10:58:12 -0500 (Fri, 23 Dec 2011) $
    Author:
    Rodney Waldhoff, Sandy McArthur
    • Constructor Detail

      • SoftReferenceObjectPool

        public SoftReferenceObjectPool​(PoolableObjectFactory<T> factory)
        Create a SoftReferenceObjectPool with the specified factory.
        Parameters:
        factory - object factory to use.
    • Method Detail

      • borrowObject

        public T borrowObject()
                       throws Exception

        Borrow an object from the pool. If there are no idle instances available in the pool, the configured factory's PoolableObjectFactory.makeObject() method is invoked to create a new instance.

        All instances are activated and validated before being returned by this method. If validation fails or an exception occurs activating or validating an idle instance, the failing instance is destroyed and another instance is retrieved from the pool, validated and activated. This process continues until either the pool is empty or an instance passes validation. If the pool is empty on activation or it does not contain any valid instances, the factory's makeObject method is used to create a new instance. If the created instance either raises an exception on activation or fails validation, NoSuchElementException is thrown. Exceptions thrown by MakeObject are propagated to the caller; but other than ThreadDeath or VirtualMachineError, exceptions generated by activation, validation or destroy methods are swallowed silently.

        Specified by:
        borrowObject in interface ObjectPool<T>
        Specified by:
        borrowObject in class BaseObjectPool<T>
        Returns:
        a valid, activated object instance
        Throws:
        NoSuchElementException - if a valid object cannot be provided
        IllegalStateException - if invoked on a closed pool
        Exception - if an exception occurs creating a new instance
      • returnObject

        public void returnObject​(T obj)
                          throws Exception

        Returns an instance to the pool after successful validation and passivation. The returning instance is destroyed if any of the following are true:

        Exceptions passivating or destroying instances are silently swallowed. Exceptions validating instances are propagated to the client.

        Specified by:
        returnObject in interface ObjectPool<T>
        Specified by:
        returnObject in class BaseObjectPool<T>
        Parameters:
        obj - instance to return to the pool
        Throws:
        Exception
      • invalidateObject

        public void invalidateObject​(T obj)
                              throws Exception

        Invalidates an object from the pool.

        By contract, obj must have been obtained using borrowObject.

        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 ObjectPool<T>
        Specified by:
        invalidateObject in class BaseObjectPool<T>
        Parameters:
        obj - a borrowed instance to be disposed.
        Throws:
        Exception
      • addObject

        public void addObject()
                       throws Exception

        Create an object, and place it into the pool. addObject() is useful for "pre-loading" a pool with idle objects.

        Before being added to the pool, the newly created instance is validated and passivated. If validation fails, the new instance is destroyed. Exceptions generated by the factory makeObject or passivate are propagated to the caller. Exceptions destroying instances are silently swallowed.

        Specified by:
        addObject in interface ObjectPool<T>
        Overrides:
        addObject in class BaseObjectPool<T>
        Throws:
        IllegalStateException - if invoked on a closed pool
        Exception - when the factory has a problem creating or passivating an object.
      • getNumIdle

        public int getNumIdle()
        Returns an approximation not less than the of the number of idle instances in the pool.
        Specified by:
        getNumIdle in interface ObjectPool<T>
        Overrides:
        getNumIdle in class BaseObjectPool<T>
        Returns:
        estimated number of idle instances in the pool
      • getNumActive

        public int getNumActive()
        Return the number of instances currently borrowed from this pool.
        Specified by:
        getNumActive in interface ObjectPool<T>
        Overrides:
        getNumActive in class BaseObjectPool<T>
        Returns:
        the number of instances currently borrowed from this pool