com.cafesoft.core.pool
Class DefaultObjectPool

java.lang.Object
  |
  +--com.cafesoft.core.pool.DefaultObjectPool
All Implemented Interfaces:
ObjectPool, Recyclable

public class DefaultObjectPool
extends Object
implements ObjectPool

Provides a generic implementation of the ObjectPool interface. It is recommended that this class be extended to create specific implementations such as SessionPool, Connection, etc...

Since:
10/19/01

Constructor Summary
DefaultObjectPool()
          Create a new DefaultObjectPool.
DefaultObjectPool(PoolableObjectFactory objectFactory)
          Create a new DefaultObjectPool Creates an object pool with no minimum, no maximum, and no expiration time.
DefaultObjectPool(PoolableObjectFactory objectFactory, int minimum)
          Create a new DefaultObjectPool.
DefaultObjectPool(PoolableObjectFactory objectFactory, int minimum, int maximum)
          Create a new DefaultObjectPool.
DefaultObjectPool(PoolableObjectFactory objectFactory, int minimum, int maximum, long expiration)
          Create a new DefaultObjectPool Creates an object pool with the specified minimum, specified maximum, and the specified expiration time.
 
Method Summary
 Object borrowObject()
          Borrow an object from the ObjectPool.
 void cleanIdle()
          Remove idle objects from the ObjectPool.
 void cleanIdle(int number)
          Remove the given number of idle objects from the ObjectPool.
 void close()
          Close the ObjectPool.
 long getExpiration()
          Get the expiration time of objects in the ObjectPool.
 int getMaximum()
          Get the maximum number of objects the ObjectPool must maintain.
 int getMinimum()
          Get the minimum number of objects the ObjectPool must maintain.
 PoolableObjectFactory getPoolableObjectFactory()
          Get the PoolableObjectFactory that the ObjectPool is currently using.
 int numActive()
          Get the number of active ObjectPool objects being used.
 int numIdle()
          Get the number of idle ObjectPool objects being used.
 void recycle()
          Recycle the resources of this object
 void returnObject(Object object)
          Return an object to the ObjectPool.
 void setDebug(boolean debug)
          Set debug on or off.
 void setExpiration(long expiration)
          Set the expiration time for objects in the ObjectPool.
 void setMaximum(int maximum)
          Set the maximum number of objects both active and inactive that the ObjectPool must maintain.
 void setMinimum(int minimum)
          Set the minimum number of objects both active and inactive that the ObjectPool must maintain.
 void setPoolableObjectFactory(PoolableObjectFactory poolableObjectFactory)
          Set the PoolableObjectFactory that the ObjectPool will use.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultObjectPool

public DefaultObjectPool()
                  throws Exception
Create a new DefaultObjectPool.

Creates an object pool with no minimum, no maximum, and no expiration time.


DefaultObjectPool

public DefaultObjectPool(PoolableObjectFactory objectFactory)
                  throws Exception
Create a new DefaultObjectPool

Creates an object pool with no minimum, no maximum, and no expiration time.

Parameters:
objectFactory - the PoolableObjectFactory to use to manage the objects for the ObjectPool

DefaultObjectPool

public DefaultObjectPool(PoolableObjectFactory objectFactory,
                         int minimum)
                  throws Exception
Create a new DefaultObjectPool.

Creates an object pool with the specified minimum, no maximum, and no expiration time.

Parameters:
objectFactory - the PoolableObjectFactory to use to manage the objects for the ObjectPool
minimum - the minimum number of objects the ObjectPool must maintain

DefaultObjectPool

public DefaultObjectPool(PoolableObjectFactory objectFactory,
                         int minimum,
                         int maximum)
                  throws Exception
Create a new DefaultObjectPool.

Creates an object pool with the specified minimum, specified maximum, and no expiration time.

Parameters:
objectFactory - the PoolableObjectFactory to use to manage the objects for the ObjectPool
minimum - the minimum number of objects the ObjectPool must maintain
maximum - the maximum number of objects the ObjectPool is allowed to create and maintain

DefaultObjectPool

public DefaultObjectPool(PoolableObjectFactory objectFactory,
                         int minimum,
                         int maximum,
                         long expiration)
                  throws Exception
Create a new DefaultObjectPool

Creates an object pool with the specified minimum, specified maximum, and the specified expiration time.

Parameters:
objectFactory - the PoolableObjectFactory to use to manage the objects for the ObjectPool
minimum - the minimum number of objects the ObjectPool must maintain
maximum - the maximum number of objects the ObjectPool is allowed to create and maintain.
expiration - the expiration time at which objects that are idle are recycled
Method Detail

recycle

public void recycle()
Recycle the resources of this object

Specified by:
recycle in interface Recyclable

borrowObject

public Object borrowObject()
                    throws Exception
Borrow an object from the ObjectPool.

Note: Implementing class should make this method synchronized.

This method is responsible for "checking-out" an object for use. It is recommended that the implementing class provide a Class specific get method that casts the generic object returned by this method to the appropriate class.

For example:

 ConnectionPool implements ObjectPool
 {
      public Connection getConnection() throws ConnectionException
      {
          return (Connection) borrowObject();
      }
 }
 

Specified by:
borrowObject in interface ObjectPool
Returns:
an object from the ObjectPool
Throws:
Exception - thrown if an error occurs while trying to get an instance of an object to use. Typically this will be the result of the createObject() method from the implementing PoolableObjectFactory assigned to the ObjectPool
PoolAtMaximumException - thrown if no more objects can be created to borrow.

returnObject

public void returnObject(Object object)
Return an object to the ObjectPool.

Note: Implementing class should make this method synchronized.

This method is responsible for "checking-in" an object. It is recommended that the implementing class provide a Class specific return method that accepts the specific class as a parameter.

For example:

 ConnectionPool implements ObjectPool
 {
      public void releaseConnection(Connection connection)
      {
          returnObject(connection);
      }
 }
 

Specified by:
returnObject in interface ObjectPool
Parameters:
object - The object to be returned to the ObjectPool

close

public void close()
Close the ObjectPool.

Destroy all objects and release all resources that were used by both the objects in the ObjectPool and the ObjectPool itself

Specified by:
close in interface ObjectPool

cleanIdle

public void cleanIdle(int number)
Remove the given number of idle objects from the ObjectPool.

Remove objects that are not actively used and release their resources. See the implementation for specifics as to the algorithm that is used to determine idle object status.

Specified by:
cleanIdle in interface ObjectPool
Parameters:
number - the number of idle objects to remove unless removing the specified number of objects would put the ObjectPool below it's minimum number of objects

cleanIdle

public void cleanIdle()
Remove idle objects from the ObjectPool.

Remove objects that are not actively used and release their resources. See the implementation for specifics as to the algorithm that is used to determine idle object status.

Specified by:
cleanIdle in interface ObjectPool

numActive

public int numActive()
Get the number of active ObjectPool objects being used.

Specified by:
numActive in interface ObjectPool
Returns:
an integer representing the number of active ObjectPool objects

numIdle

public int numIdle()
Get the number of idle ObjectPool objects being used.

Specified by:
numIdle in interface ObjectPool
Returns:
an integer representing the number of idle ObjectPool objects

setPoolableObjectFactory

public void setPoolableObjectFactory(PoolableObjectFactory poolableObjectFactory)
Set the PoolableObjectFactory that the ObjectPool will use.

The PoolableObjectFactory is responsible for object creation, destruction, and validation.

Specified by:
setPoolableObjectFactory in interface ObjectPool
Parameters:
poolableObjectFactory - the new poolableObjectFactory for the ObjectPool to use

getPoolableObjectFactory

public PoolableObjectFactory getPoolableObjectFactory()
Get the PoolableObjectFactory that the ObjectPool is currently using.

The PoolableObjectFactory is responsible for object creation, destruction, and validation.

Specified by:
getPoolableObjectFactory in interface ObjectPool
Returns:
the PoolableObjectFactory being used or null if a PoolableObjectFactory is currently not set.

setMaximum

public void setMaximum(int maximum)
Set the maximum number of objects both active and inactive that the ObjectPool must maintain. Use 0 to set no maximum.

Formula for determining maximum object status
numActive() + numIdle() <= maximum

Specified by:
setMaximum in interface ObjectPool
Parameters:
maximum - the new maximum number of objects that the ObjectPool must maintain.

setMinimum

public void setMinimum(int minimum)
Set the minimum number of objects both active and inactive that the ObjectPool must maintain. Use 0 to set no minimum.

Formula for determining minimum object status
numActive() + numIdle() >= minimum

Specified by:
setMinimum in interface ObjectPool
Parameters:
minimum - the new minimum number of objects that the ObjectPool must maintain

setExpiration

public void setExpiration(long expiration)
Set the expiration time for objects in the ObjectPool. Use 0 to set no expiration time.

Set the time that an object is allowed to remain in an idle state before it is collected and recycled by the pool. The implementing class is responsible for creating the mechanism to use the expiration time to expire objects.

Specified by:
setExpiration in interface ObjectPool
Parameters:
expiration - the new expiration time for the ObjectPool to use

getMaximum

public int getMaximum()
Get the maximum number of objects the ObjectPool must maintain.

Specified by:
getMaximum in interface ObjectPool
Returns:
the maximum number of objects the ObjectPool must maintain

getMinimum

public int getMinimum()
Get the minimum number of objects the ObjectPool must maintain.

Specified by:
getMinimum in interface ObjectPool
Returns:
the minimum number of objects the ObjectPool must maintain

getExpiration

public long getExpiration()
Get the expiration time of objects in the ObjectPool.

Specified by:
getExpiration in interface ObjectPool
Returns:
the expiration time of objects in the ObjectPool

setDebug

public void setDebug(boolean debug)
Set debug on or off.

Specified by:
setDebug in interface ObjectPool
Parameters:
debug - the value of debug true is on false is off


Generated on 10:40:31 AM September 10, 2003, © 1996-2003 Cafésoft LLC. All rights reserved.