org.apache.commons.pool.impl
Class SoftReferenceObjectPool

java.lang.Object
  extended by org.apache.commons.pool.BaseObjectPool
      extended by org.apache.commons.pool.impl.SoftReferenceObjectPool
All Implemented Interfaces:
ObjectPool

public class SoftReferenceObjectPool
extends BaseObjectPool
implements ObjectPool

A SoftReference based ObjectPool.

Since:
Pool 1.0
Version:
$Revision: 965338 $ $Date: 2010-07-18 21:09:29 -0400 (Sun, 18 Jul 2010) $
Author:
Rodney Waldhoff, Sandy McArthur

Constructor Summary
SoftReferenceObjectPool(PoolableObjectFactory factory)
          Create a SoftReferenceObjectPool with the specified factory.
 
Method Summary
 void addObject()
          Create an object, and place it into the pool.
 java.lang.Object borrowObject()
          Borrow an object from the pool.
 void clear()
          Clears any objects sitting idle in the pool.
 void close()
          Close this pool, and free any resources associated with it.
 PoolableObjectFactory getFactory()
          Returns the PoolableObjectFactory used by this pool to create and manage object instances.
 int getNumActive()
          Return the number of instances currently borrowed from this pool.
 int getNumIdle()
          Returns an approximation not less than the of the number of idle instances in the pool.
 void invalidateObject(java.lang.Object obj)
          Invalidates an object from the pool.
 void returnObject(java.lang.Object obj)
          Returns an instance to the pool after successful validation and passivation.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SoftReferenceObjectPool

public SoftReferenceObjectPool(PoolableObjectFactory factory)
Create a SoftReferenceObjectPool with the specified factory.

Parameters:
factory - object factory to use.
Method Detail

addObject

public void addObject()
               throws java.lang.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
Overrides:
addObject in class BaseObjectPool
Throws:
java.lang.IllegalStateException - if invoked on a closed pool
java.lang.Exception - when the factory has a problem creating or passivating an object.

borrowObject

public java.lang.Object borrowObject()
                              throws java.lang.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
Specified by:
borrowObject in class BaseObjectPool
Returns:
a valid, activated object instance
Throws:
java.util.NoSuchElementException - if a valid object cannot be provided
java.lang.IllegalStateException - if invoked on a closed pool
java.lang.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
Overrides:
clear in class BaseObjectPool

close

public void close()
           throws java.lang.Exception

Close this pool, and free 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 ObjectPool
Overrides:
close in class BaseObjectPool
Throws:
java.lang.Exception - never - exceptions clearing the pool are swallowed

getFactory

public PoolableObjectFactory getFactory()
Returns the PoolableObjectFactory used by this pool to create and manage object instances.

Returns:
the factory
Since:
1.5.5

getNumActive

public int getNumActive()
Return the number of instances currently borrowed from this pool.

Specified by:
getNumActive in interface ObjectPool
Overrides:
getNumActive in class BaseObjectPool
Returns:
the number of instances currently borrowed from this pool

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
Overrides:
getNumIdle in class BaseObjectPool
Returns:
estimated number of idle instances in the pool

invalidateObject

public void invalidateObject(java.lang.Object obj)
                      throws java.lang.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
Specified by:
invalidateObject in class BaseObjectPool
Parameters:
obj - a borrowed instance to be disposed.
Throws:
java.lang.Exception

returnObject

public void returnObject(java.lang.Object obj)
                  throws java.lang.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
Specified by:
returnObject in class BaseObjectPool
Parameters:
obj - instance to return to the pool
Throws:
java.lang.Exception


Copyright © 2001-2003 Apache Software Foundation. Documenation generated February 1 2011.