Class SoftReferenceObjectPool<T>

Type Parameters:
T - Type of element pooled in this pool.
All Implemented Interfaces:
Closeable, AutoCloseable, ObjectPool<T>

public class SoftReferenceObjectPool<T> extends BaseObjectPool<T>
A SoftReference based ObjectPool.

This class is intended to be thread-safe.

  • Constructor Details

  • Method Details

    • addObject

      public void addObject() throws Exception
      Creates an object, and places 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>
      addObject in class BaseObjectPool<T>
      IllegalStateException - if invoked on a closed pool
      Exception - when the factory has a problem creating or passivating an object.
    • borrowObject

      public T borrowObject() throws Exception
      Borrows an object from the pool. If there are no idle instances available in the pool, the configured factory's PooledObjectFactory.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>
      a valid, activated object instance
      NoSuchElementException - if a valid object cannot be provided
      IllegalStateException - if invoked on a closed pool
      Exception - if an exception occurs creating a new instance
    • clear

      public void clear()
      Clears any objects sitting idle in the pool.
      Specified by:
      clear in interface ObjectPool<T>
      clear in class BaseObjectPool<T>
    • close

      public void close()
      Closes this pool, and frees any resources associated with it. Invokes clear() to destroy and remove instances in the pool.

      Calling addObject() or borrowObject() after invoking this method on a pool will cause them to throw an IllegalStateException.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface ObjectPool<T>
      close in class BaseObjectPool<T>
    • getFactory

      Gets the PooledObjectFactory used by this pool to create and manage object instances.
      the factory
    • getNumActive

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

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

      public void invalidateObject(T obj) throws Exception
      Invalidates an object from the pool.

      By contract, obj must have been obtained using ObjectPool.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:
      invalidateObject in interface ObjectPool<T>
      Specified by:
      invalidateObject in class BaseObjectPool<T>
      obj - a borrowed instance to be disposed.
      Exception - if the instance cannot be invalidated
    • 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>
      obj - instance to return to the pool
      IllegalArgumentException - if obj is not currently part of this pool
      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
    • toStringAppendFields

      protected void toStringAppendFields(StringBuilder builder)
      Description copied from class: BaseObject
      Used by sub-classes to include the fields defined by the sub-class in the BaseObject.toString() output.
      toStringAppendFields in class BaseObjectPool<T>
      builder - Field names and values are appended to this object