soap tutorial

adriani.jws.template.db
Class ObjectPool

java.lang.Object
  extended by adriani.jws.template.db.ObjectPool
Direct Known Subclasses:
JDBCConnectionPool

public abstract class ObjectPool
extends java.lang.Object

Abstract class managing a generic pool of objects (usually threads or database connections).
The class offers two hashtables: the locked hashtable and the unlocked hashtable. The locked hashtable stores a reference to the "busy" objects: if the objects are DB connections, then a "busy" object could be a connection already in use. The unlocked hashtable stores all the "free" objects (available).
The normal life cycle of the objects should be:

1. The object is instantiated by the create() abstract method.
2. The object is retrieved by calling the checkOut() method.
3. The object is released by calling the checkIn() method.

Implementation of the create() abstract method is left to the child class and should not be called directly: it's up to the checkOut() method to decide if it's necessary to create a new object (usually because the unlocked hashtable is empty) or if it's possible to return an already existing object.


Field Summary
 int expirationTime
          Sleeping time for the cleanup thread (in seconds).
protected static java.lang.String leadingName
          Beginning of each logging message produced by this class.
protected static WSLogger logger
          Retrieving the logger instance to be used for this class.
static int maximum
          Maximum allowed number of objects in the pool.
 
Constructor Summary
ObjectPool(int expirationTime)
          Constructor.
 
Method Summary
(package private)  void checkIn(java.lang.Object object)
          Called by the user when he finishes to use an object previously retrieved using checkOut().
(package private)  java.lang.Object checkOut()
          Return a ready-to-use object to the user.
(package private)  void cleanUp()
          Removes from the unlocked list all the expired objects.
(package private) abstract  java.lang.Object create()
          Implemented by the child class (Example: see the JDBCConnectionPool class).
(package private) abstract  void expire(java.lang.Object object)
          Implemented by the child class (Example: see the JDBCConnectionPool class).
 void finalize()
          Method useful for debug and testing, the test the scope of the pool objects.
 java.lang.StringBuffer getPoolData()
          Return a text message describing the status of this pool: number of locked objects, number of unlocked objects and the thread status.
 int getPoolSize(int max)
          Accessor method.
 java.lang.String printObject(java.lang.Object object)
          Print the serialize reference number of the input object.
 void setPoolSize(int max)
          Mutator method.
(package private)  void shutdown()
          Terminates the cleaner thread.
(package private) abstract  boolean validate(java.lang.Object object)
          Implemented by the child class (Example: see the JDBCConnectionPool class).
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

expirationTime

public int expirationTime
Sleeping time for the cleanup thread (in seconds).


maximum

public static int maximum
Maximum allowed number of objects in the pool.


logger

protected static WSLogger logger
Retrieving the logger instance to be used for this class.


leadingName

protected static java.lang.String leadingName
Beginning of each logging message produced by this class.

Constructor Detail

ObjectPool

public ObjectPool(int expirationTime)
Constructor.

Parameters:
expirationTime - the sleep time for the CleanUpThread, in seconds
Method Detail

checkIn

void checkIn(java.lang.Object object)
Called by the user when he finishes to use an object previously retrieved using checkOut(). The object is removed from the locked table and put again in the unlocked table.

Parameters:
object - a reference to the object to check in

checkOut

java.lang.Object checkOut()
                    throws java.lang.Exception
Return a ready-to-use object to the user.

This method can be considered the inner core of the pool mechanism, which is: first we look in the unlocked hash table, if we find a valid object we store such object in the locked table and then we return the object. On the opposite, if there isn't any valid object in the unlocked hash table, we check the maximum pool value: if we are below this threshold we create a brand new object, using the create() method. If we have already reached the maximum pool capacity we return null and log a severe message.

Returns:
object a reference to object just checked out
Throws:
java.lang.Exception

cleanUp

void cleanUp()
Removes from the unlocked list all the expired objects.
This method is invoked automatically by the cleaner thread (see expirationTime).

Note: after cleanup of the unlocked list, the method calls System.gc() to delete all the memory footsteps.

See Also:
CleanUpThread

shutdown

void shutdown()
Terminates the cleaner thread.

See Also:
CleanUpThread

setPoolSize

public void setPoolSize(int max)
Mutator method.


getPoolSize

public int getPoolSize(int max)
Accessor method.


create

abstract java.lang.Object create()
                          throws java.lang.Exception
Implemented by the child class (Example: see the JDBCConnectionPool class).

Returns:
object a reference to object just checked out
Throws:
java.lang.Exception

validate

abstract boolean validate(java.lang.Object object)
Implemented by the child class (Example: see the JDBCConnectionPool class).

Parameters:
object - a reference to object to validate

expire

abstract void expire(java.lang.Object object)
Implemented by the child class (Example: see the JDBCConnectionPool class).

Parameters:
object - a reference to object to destroy

printObject

public java.lang.String printObject(java.lang.Object object)
Print the serialize reference number of the input object.

Parameters:
object - a generic Java object, e.g. MyObject@1bd4722
Returns:
the serial number of the input object, e.g. 1bd4722

getPoolData

public java.lang.StringBuffer getPoolData()
Return a text message describing the status of this pool: number of locked objects, number of unlocked objects and the thread status.

Returns:
a StringBuffer containing the printable message

finalize

public void finalize()
Method useful for debug and testing, the test the scope of the pool objects. This method should be called only when the application server is shut down, since this class is stored as a static object in a static container class (see for example the Database class).

Overrides:
finalize in class java.lang.Object

Build 19/03/2014

Morpheus Technologies - making the Zion software
Copyright © 2009-2014 Morpheus Technologies. All Rights Reserved