com.google.code.rees.scope.util.monitor
Class ScheduledExecutorTimeoutMonitor<T extends Timeoutable<T>>

java.lang.Object
  extended by com.google.code.rees.scope.util.monitor.ScheduledExecutorTimeoutMonitor<T>
All Implemented Interfaces:
TimeoutListener<T>, TimeoutMonitor<T>, Serializable

public class ScheduledExecutorTimeoutMonitor<T extends Timeoutable<T>>
extends Object
implements TimeoutMonitor<T>, TimeoutListener<T>

An implementation of the TimeoutMonitor that makes use of a ScheduledExecutorService. Cache's tasks and will add them to a new ScheduledExecutorService after serialization in order to continue monitoring in cases such as cluster replication. Of note: this class depends on being given a new scheduler after serialization, is does not create its own schedulers. Likewise, it must be given an instance when it is first created as well.

Author:
rees.byars
See Also:
Serialized Form

Field Summary
static long MONITORING_DELAY
          the delay between adding a Timeoutable to the scheduler and the time of the first check of the Timeoutable's remaining time
protected  long monitoringFrequency
           
protected  Map<String,ScheduledFuture<?>> scheduledFutures
           
protected  ScheduledExecutorService scheduler
           
protected  SchedulerProvider schedulerProvider
           
protected  Map<String,TimeoutRunner<T>> timeoutRunners
           
 
Fields inherited from interface com.google.code.rees.scope.util.monitor.TimeoutMonitor
DEFAULT_MONITOR_FREQUENCY
 
Constructor Summary
protected ScheduledExecutorTimeoutMonitor()
           
 
Method Summary
 void addTimeoutable(T timeoutable)
          Adds the Timeoutable to this monitor
 void destroy()
          Destroys this monitor, stopping its background thread and clearing its Timeoutable cache.
 void init()
          Initializes this monitor, beginning a background thread for monitoring its Timeoutables.
 void onTimeout(T timeoutable)
          Called when the given Timeoutable's timeout method is called.
 void removeTimeoutable(T timeoutable)
          Removes this Timeoutable from this monitor
 void setMonitoringFrequency(long frequencyMillis)
          The frequency, in milliseconds, that this monitor will check its Timeoutables for their remaining times.
 void setSchedulerProvider(SchedulerProvider schedulerProvider)
          sets the scheduler to be used
static
<TT extends Timeoutable<TT>>
ScheduledExecutorTimeoutMonitor<TT>
spawnInstance(SchedulerProvider scheduler, long monitoringFrequency)
          used to create an instance
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MONITORING_DELAY

public static final long MONITORING_DELAY
the delay between adding a Timeoutable to the scheduler and the time of the first check of the Timeoutable's remaining time

See Also:
Constant Field Values

timeoutRunners

protected Map<String,TimeoutRunner<T extends Timeoutable<T>>> timeoutRunners

scheduledFutures

protected transient Map<String,ScheduledFuture<?>> scheduledFutures

scheduler

protected transient ScheduledExecutorService scheduler

monitoringFrequency

protected long monitoringFrequency

schedulerProvider

protected SchedulerProvider schedulerProvider
Constructor Detail

ScheduledExecutorTimeoutMonitor

protected ScheduledExecutorTimeoutMonitor()
Method Detail

setMonitoringFrequency

public void setMonitoringFrequency(long frequencyMillis)
The frequency, in milliseconds, that this monitor will check its Timeoutables for their remaining times. It should be noted that the frequency is guaranteed to not be less the the given time, but it may incrementally exceed the given time as more Timeoutables are added. This is due to the time required to check each of the Timeoutables.

Specified by:
setMonitoringFrequency in interface TimeoutMonitor<T extends Timeoutable<T>>

setSchedulerProvider

public void setSchedulerProvider(SchedulerProvider schedulerProvider)
sets the scheduler to be used


init

public void init()
Initializes this monitor, beginning a background thread for monitoring its Timeoutables.

Specified by:
init in interface TimeoutMonitor<T extends Timeoutable<T>>

destroy

public void destroy()
Destroys this monitor, stopping its background thread and clearing its Timeoutable cache.

Specified by:
destroy in interface TimeoutMonitor<T extends Timeoutable<T>>

addTimeoutable

public void addTimeoutable(T timeoutable)
Adds the Timeoutable to this monitor

Specified by:
addTimeoutable in interface TimeoutMonitor<T extends Timeoutable<T>>

removeTimeoutable

public void removeTimeoutable(T timeoutable)
Removes this Timeoutable from this monitor

Specified by:
removeTimeoutable in interface TimeoutMonitor<T extends Timeoutable<T>>

onTimeout

public void onTimeout(T timeoutable)
Called when the given Timeoutable's timeout method is called.

Specified by:
onTimeout in interface TimeoutListener<T extends Timeoutable<T>>

spawnInstance

public static <TT extends Timeoutable<TT>> ScheduledExecutorTimeoutMonitor<TT> spawnInstance(SchedulerProvider scheduler,
                                                                                             long monitoringFrequency)
used to create an instance

Type Parameters:
TT -
Parameters:
scheduler -
monitoringFrequency -
Returns:


Copyright © 2012-2013. All Rights Reserved.