org.sadun.util.pool2
Class BasePassivationManager

java.lang.Object
  extended byjava.lang.Thread
      extended byorg.sadun.util.pool2.BasePassivationManager
All Implemented Interfaces:
java.lang.Runnable, Terminable
Direct Known Subclasses:
DefaultPassivationManager

public abstract class BasePassivationManager
extends java.lang.Thread
implements Terminable

A Passivation thread is associated to one or more ObjectPools and triggers the activation/passivation state of a Pooled object.

This class is abstract. Each subclass may define alternative passivation strategies.

Author:
Cristiano Sadun

Field Summary
protected  java.io.PrintStream logStream
           
protected  java.util.Set monitoredPools
          The set of pools monitored by this thread (contained org.sadun.util.pool2.ObjectPool).
protected  boolean rotatePools
          Controls which pools are checked on a service cycle
protected  int rotationRate
           
protected  int sleepTime
          The sleep time
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
protected BasePassivationManager(ObjectPool pool, java.lang.String name, int sleepTime)
          Build a passivation thread monitoring the given pool, with the given thread name and having the given sleep time.
protected BasePassivationManager(java.lang.String name)
           
protected BasePassivationManager(java.lang.String name, int sleepTime)
           
 
Method Summary
 void addPool(ObjectPool pool)
          Add an ObjectPool to monitor.
 boolean canPassivate(java.lang.Class cls)
          Method canPassivate.
 int countPools()
          Count the monitored pools
protected abstract  void createState(ObjectPool pool, PooledObject obj)
          Used by a subclass to define an state information for a certain object belonging to a certain pool when a pool is added to the monitored set
 int getRotationRate()
          Returns the rotationRate.
 int getSleepTime()
          Returns the sleepTime.
protected abstract  void handleAcquired(ObjectPool pool, PooledObject obj)
          This method is used by the subclass to determine what to do when a pooled object is aquired.
protected abstract  void handleInvoked(ObjectPool pool, PooledObject obj)
          This method is is used by the subclass to determine what to do when a pooled object is actually invoked.
protected abstract  void handleReleased(ObjectPool pool, PooledObject obj)
          This method is invoked by the pool when a pooled object is released.
 boolean isRotatePools()
          Returns the rotatePools.
 boolean isShuttingDown()
          Return true if a shutdown has been requested.
protected abstract  boolean isToPassivate(ObjectPool pool, PooledObject obj, java.lang.Object preparationResult)
          This method is invoked to check whether an object is to passivate or not.
protected abstract  java.lang.Object prepareForPassivationCheck(ObjectPool pool)
          This method is invoked just before a pool is checked for passivation, and therefore before any isToPassivate() is invoked.
 void removePool(ObjectPool pool)
          Remove an ObjectPool from monitoring.
protected abstract  void removeState(ObjectPool pool, PooledObject obj)
          Used by a subclass to remove the state information for a certain object belonging to a certain pool when a pool is removed from the monitored set
 void run()
          The passivation thread main loop.
 void setLogStream(java.io.PrintStream logStream)
          Method setLogStream.
 void setRotatePools(boolean rotatePools)
          Sets the rotatePools.
 void setRotationRate(int rotationRate)
          Sets the rotationRate.
 void setSleepTime(int sleepTime)
          Sets the sleepTime.
 void shutdown()
          Unconditionally requests a shutdown.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

logStream

protected java.io.PrintStream logStream

monitoredPools

protected java.util.Set monitoredPools
The set of pools monitored by this thread (contained org.sadun.util.pool2.ObjectPool).


sleepTime

protected int sleepTime
The sleep time


rotatePools

protected boolean rotatePools
Controls which pools are checked on a service cycle


rotationRate

protected int rotationRate
Constructor Detail

BasePassivationManager

protected BasePassivationManager(ObjectPool pool,
                                 java.lang.String name,
                                 int sleepTime)
Build a passivation thread monitoring the given pool, with the given thread name and having the given sleep time.

Parameters:
pool - the pool to monitor. Additional pools can be monitored by the same passivation thread by using addPool()
name - the thread name

BasePassivationManager

protected BasePassivationManager(java.lang.String name,
                                 int sleepTime)

BasePassivationManager

protected BasePassivationManager(java.lang.String name)
Method Detail

addPool

public void addPool(ObjectPool pool)
Add an ObjectPool to monitor.

Parameters:
pool - the pool to add

canPassivate

public boolean canPassivate(java.lang.Class cls)
Method canPassivate.

Returns:
boolean

createState

protected abstract void createState(ObjectPool pool,
                                    PooledObject obj)
Used by a subclass to define an state information for a certain object belonging to a certain pool when a pool is added to the monitored set

Parameters:
obj - the object
pool - the pool the object belongs to

removeState

protected abstract void removeState(ObjectPool pool,
                                    PooledObject obj)
Used by a subclass to remove the state information for a certain object belonging to a certain pool when a pool is removed from the monitored set

Parameters:
obj - the object
pool - the pool the object belongs to

removePool

public void removePool(ObjectPool pool)
Remove an ObjectPool from monitoring.

Parameters:
pool - the pool to remove

countPools

public int countPools()
Count the monitored pools


run

public final void run()
The passivation thread main loop.

The thread awakes any sleepTime milliseconds and runs a service cycle, by verifying the passivability of all the objects in all or some of the monitored pools (see rotatePools).

The method isToPassivate() is invoked on every object. If the method returns true the corresponding object is passivated.

Specified by:
run in interface java.lang.Runnable

prepareForPassivationCheck

protected abstract java.lang.Object prepareForPassivationCheck(ObjectPool pool)
This method is invoked just before a pool is checked for passivation, and therefore before any isToPassivate() is invoked.

A subclass may execute some preparation before the pool's object are actually scanned, and return an object as result. Such object is the passed to each subsequent isToPassivate() invocation.

Parameters:
pool -

isToPassivate

protected abstract boolean isToPassivate(ObjectPool pool,
                                         PooledObject obj,
                                         java.lang.Object preparationResult)
This method is invoked to check whether an object is to passivate or not.


handleInvoked

protected abstract void handleInvoked(ObjectPool pool,
                                      PooledObject obj)
This method is is used by the subclass to determine what to do when a pooled object is actually invoked.

The passivation thread may collect this information to implements its passivation policy.

Parameters:
obj - the PooledObject which is being invoked

handleAcquired

protected abstract void handleAcquired(ObjectPool pool,
                                       PooledObject obj)
This method is used by the subclass to determine what to do when a pooled object is aquired.

The passivation thread may collect this information to implements its passivation policy.

Parameters:
obj - the PooledObject which is being acquired

handleReleased

protected abstract void handleReleased(ObjectPool pool,
                                       PooledObject obj)
This method is invoked by the pool when a pooled object is released.

The passivation thread may collect this information to implements its passivation policy.

Parameters:
obj - the PooledObject which is being released

isShuttingDown

public boolean isShuttingDown()
Description copied from interface: Terminable
Return true if a shutdown has been requested.

Specified by:
isShuttingDown in interface Terminable
Returns:
true if a shutdown has been requested
See Also:
Terminable.isShuttingDown()

shutdown

public void shutdown()
Description copied from interface: Terminable
Unconditionally requests a shutdown. The runnable will complete its last atomic operation, and gracefully exit the run() method.

Specified by:
shutdown in interface Terminable
See Also:
Terminable.shutdown()

getSleepTime

public int getSleepTime()
Returns the sleepTime.

Returns:
int

setSleepTime

public void setSleepTime(int sleepTime)
Sets the sleepTime.

Parameters:
sleepTime - The sleepTime to set

isRotatePools

public boolean isRotatePools()
Returns the rotatePools.

Returns:
boolean

setRotatePools

public void setRotatePools(boolean rotatePools)
Sets the rotatePools.

Parameters:
rotatePools - The rotatePools to set

getRotationRate

public int getRotationRate()
Returns the rotationRate.

Returns:
int

setRotationRate

public void setRotationRate(int rotationRate)
Sets the rotationRate.

Parameters:
rotationRate - The rotationRate to set

setLogStream

public void setLogStream(java.io.PrintStream logStream)
Method setLogStream.

Parameters:
logStream -