InaccurateTimeoutWatchdogpublic class InaccurateTimeoutWatchdog extends org.apache.avalon.framework.logger.AbstractLogEnabled implements org.apache.avalon.framework.activity.Disposable, Watchdog, RunnableThis class represents an watchdog process that serves to
monitor a situation and triggers an action after a certain time has
passed. This implementation is deliberately inaccurate, trading
accuracy for minimal impact on reset. This should be used when
the time of the Watchdog trigger is not critical, and a high number
of resets are expected. |
Fields Summary |
---|
private volatile boolean | isCheckingWhether the watchdog is currently checking the trigger condition | private volatile boolean | isResetWhether the watchdog has been reset since the thread slept. | private final long | timeoutThe number of milliseconds until the watchdog times out. | private volatile long | lastResetThe last time the internal timer was reset, as measured in milliseconds since
January 1, 1970 00:00:00.000 GMT. | private WatchdogTarget | triggerTargetThe WatchdogTarget whose execute() method will be called upon triggering
of the condition. | private Thread | watchdogThreadThe thread that runs the watchdog. | private org.apache.excalibur.thread.ThreadPool | myThreadPoolThe thread pool used to generate InaccurateTimeoutWatchdogs |
Constructors Summary |
---|
public InaccurateTimeoutWatchdog(long timeout, WatchdogTarget target, org.apache.excalibur.thread.ThreadPool threadPool)The sole constructor for the InaccurateTimeoutWatchdog
if (target == null) {
throw new IllegalArgumentException("The WatchdogTarget for this TimeoutWatchdog cannot be null.");
}
if (threadPool == null) {
throw new IllegalArgumentException("The thread pool for this TimeoutWatchdog cannot be null.");
}
this.timeout = timeout;
triggerTarget = target;
myThreadPool = threadPool;
|
Methods Summary |
---|
public void | dispose()
synchronized(this) {
isChecking = false;
if (watchdogThread != null) {
getLogger().debug("Calling disposeWatchdog() " + watchdogThread.getName());
} else {
getLogger().debug("Calling disposeWatchdog() for inactive watchdog");
}
if (watchdogThread != null) {
watchdogThread = null;
notifyAll();
}
ContainerUtil.dispose(triggerTarget);
triggerTarget = null;
}
| public void | reset()Reset this Watchdog. Tells the Watchdog thread to reset
the timer when it next awakens.
if (watchdogThread != null) {
getLogger().debug("Calling reset() " + watchdogThread.getName());
} else {
getLogger().debug("Calling reset() for inactive watchdog");
}
isReset = true;
| public void | run()Execute the body of the Watchdog, triggering as appropriate.
try {
watchdogThread = Thread.currentThread();
while ((!(Thread.currentThread().interrupted())) && (watchdogThread != null)) {
try {
if (!isChecking) {
if (getLogger().isDebugEnabled()) {
getLogger().debug("Watchdog " + Thread.currentThread().getName() + " is not active - going to exit.");
}
synchronized (this) {
if (!isChecking) {
watchdogThread = null;
}
continue;
}
} else {
long currentTime = System.currentTimeMillis();
if (isReset) {
isReset = false;
lastReset = currentTime;
}
long timeToSleep = lastReset + timeout - currentTime;
if (watchdogThread != null) {
getLogger().debug("Watchdog " + watchdogThread.getName() + " has time to sleep " + timeToSleep);
} else {
getLogger().debug("Watchdog has time to sleep " + timeToSleep);
}
if (timeToSleep <= 0) {
try {
synchronized (this) {
if ((isChecking) && (triggerTarget != null)) {
triggerTarget.execute();
}
watchdogThread = null;
}
} catch (Throwable t) {
getLogger().error("Encountered error while executing Watchdog target.", t);
}
isChecking = false;
continue;
} else {
synchronized(this) {
wait(timeToSleep);
}
}
}
} catch (InterruptedException ie) {
}
}
synchronized( this ) {
watchdogThread = null;
}
} finally {
// Ensure that the thread is in a non-interrupted state when it gets returned
// to the pool.
Thread.currentThread().interrupted();
}
getLogger().debug("Watchdog " + Thread.currentThread().getName() + " is exiting run().");
| public void | start()Start this Watchdog, causing it to begin checking.
getLogger().debug("Calling start()");
lastReset = System.currentTimeMillis();
isChecking = true;
synchronized(this) {
if ( watchdogThread == null) {
myThreadPool.execute(this);
}
}
| public void | stop()Stop this Watchdog, causing the Watchdog to stop checking the trigger
condition. The monitor can be restarted with a call to startWatchdog.
if (watchdogThread != null) {
getLogger().debug("Calling stop() " + watchdogThread.getName());
} else {
getLogger().debug("Calling stop() for inactive watchdog");
}
isChecking = false;
|
|