com.jpeterson.pool
Class ObjectPool

java.lang.Object
  |
  +--com.jpeterson.pool.ObjectPool
Direct Known Subclasses:
SocketObjectPool

public abstract class ObjectPool
extends java.lang.Object

Base functionality for implementating a pool of objects. Based on the article Build your own ObjectPool in Java to boost app speed from the June 1998 issue of JavaWorld.


Field Summary
static int DEFAULT_EXPIRATION
          Default expiration is 30 seconds.
protected  java.util.Hashtable locked
          This data element contains the objects that are currently checked out.
protected  java.util.Hashtable unlocked
          This data element contains the objects are available in the pool.
 
Constructor Summary
ObjectPool()
          Create an ObjectPool with default expiration.
ObjectPool(long expiration)
          Create an ObjectPool with the specified expiration.
 
Method Summary
protected  void broken(java.lang.Object o)
          Return a broken object to the pool.
protected  void checkIn(java.lang.Object o)
          Return an object to the pool.
protected  java.lang.Object checkOut()
          Retrieve an available object from the pool.
protected abstract  java.lang.Object create()
          Called by checkOut when a new object is needed to fulfill a request.
protected abstract  void expire(java.lang.Object o)
          Called by checkOut when an object fails validation.
 long getExpiration()
          Get the expiration for objects in the pool.
 void setExpiration(long expiration)
          Set the expiration for objects in the pool.
protected abstract  boolean validate(java.lang.Object o)
          Called by checkOut when using an object from the pool.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

locked

protected java.util.Hashtable locked
This data element contains the objects that are currently checked out. The actual object is the key of the hashtable with a Long containing the time when the object was checked out in milliseconds as the value in the hashtable.

unlocked

protected java.util.Hashtable unlocked
This data element contains the objects are available in the pool. The actual object is the key of the hashtable with a Long containing the expiration time of the object in milliseconds as the value in the hashtable.

DEFAULT_EXPIRATION

public static final int DEFAULT_EXPIRATION
Default expiration is 30 seconds.
Constructor Detail

ObjectPool

public ObjectPool()
Create an ObjectPool with default expiration.

ObjectPool

public ObjectPool(long expiration)
Create an ObjectPool with the specified expiration.
Parameters:
expiration - Duration that objects can be in the pool before expiring. Expressed in milliseconds.
Method Detail

setExpiration

public void setExpiration(long expiration)
Set the expiration for objects in the pool.
Parameters:
expiration - Duration that objects can be in the pool before expiring. Expressed in milliseconds.

getExpiration

public long getExpiration()
Get the expiration for objects in the pool.
Returns:
The duration that objects can be in the pool before expiring. Expressed in milliseconds.

create

protected abstract java.lang.Object create()
Called by checkOut when a new object is needed to fulfill a request. May return null if a new object can not be created.
Returns:
New object or null if a new object can not be created.

validate

protected abstract boolean validate(java.lang.Object o)
Called by checkOut when using an object from the pool. Objects created via create are assumed to be valid. Object that are valid are assumed to be ready for use.
Parameters:
o - The object to validate.
Returns:
Returns true if the object is "valid", false otherwise.

expire

protected abstract void expire(java.lang.Object o)
Called by checkOut when an object fails validation. This gives an object a chance to free any of its allocated resources.
Parameters:
o - The object that has just expired.

checkOut

protected java.lang.Object checkOut()
Retrieve an available object from the pool. If no objects can be provided, null is returned.
Returns:
An object from the available pool. May be null if no objects are available.

checkIn

protected void checkIn(java.lang.Object o)
Return an object to the pool.
Parameters:
o - Object originally checked out of the pool.

broken

protected void broken(java.lang.Object o)
Return a broken object to the pool. The method expire will be called to expire the object.
Parameters:
o - Object originally checked out of the pool.