au.com.lastweekend.jen.util
Class ProcessWatchdog

java.lang.Object
  extended by au.com.lastweekend.jen.util.LoopRunnable
      extended by au.com.lastweekend.jen.util.AbstractWatchdog
          extended by au.com.lastweekend.jen.util.ProcessWatchdog
All Implemented Interfaces:
java.lang.Runnable

public class ProcessWatchdog
extends AbstractWatchdog

Assumes the processes are well behaved. Each process under watch will have up to 4 threads - a watchdog thread. This will be waiting for the process to terminate - a thread gobbling standard output - a thread gobbling standard error (if not redirected) - a JVM thread for the Process itself. Stdout is logged at debug, Stderr logged at error.

Version:
$Id$
Author:
Grant Gardner grant@lastweekend.com.au

Constructor Summary
ProcessWatchdog(java.lang.ProcessBuilder builder)
           
 
Method Summary
 java.lang.Long getDelayTime()
           
protected  void gobbleOutput(java.lang.String line, boolean isErrorOutput)
          The output of the process's stdout and stderr is sent to this method.
protected  void handleGobblerException(java.io.IOException e, boolean isErrorOutput)
          The threads eating the stdout/stderr can throw the odd exception.
protected  void handleStandardError(java.io.InputStream stderr)
          Do something with stderr.
protected  void handleStandardInput(java.io.OutputStream stdin)
          Do something with the stdin
protected  void handleStandardOutput(java.io.InputStream stdout)
          Do something with stdout
protected  void processDestroy()
          Called when the process is destroyed forcibly during the stop sequence.
protected  void processExit(int result)
          Called when the process terminates of its own accord (or some external kill)
protected  void processStart()
          Called each time the process is started
 void setDelayTime(long delayTime)
          If the process exits within this period after starting, the watchdog thread will sleep for this period before trying again.
 java.lang.Thread start(java.lang.String name)
          Convenience method to start this runnable in a new thread.
protected  void startException(java.io.IOException e)
          Handle the IO exception thrown from builder.start()
protected  void stopped()
          Called when the loop exits.
protected  void waitInterrupted(java.lang.InterruptedException e)
          Handle the interrupt exception during the stop process.
 
Methods inherited from class au.com.lastweekend.jen.util.AbstractWatchdog
getBuilder, loop
 
Methods inherited from class au.com.lastweekend.jen.util.LoopRunnable
run, stop, stopAtShutdown
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ProcessWatchdog

public ProcessWatchdog(java.lang.ProcessBuilder builder)
Method Detail

setDelayTime

public void setDelayTime(long delayTime)
If the process exits within this period after starting, the watchdog thread will sleep for this period before trying again. Prevents tight loops.

default 0 = no delay.

Parameters:
delayTime - (milliseconds)

getDelayTime

public java.lang.Long getDelayTime()

start

public java.lang.Thread start(java.lang.String name)
Description copied from class: LoopRunnable
Convenience method to start this runnable in a new thread.

Overrides:
start in class LoopRunnable
Returns:
a new, started thread.

startException

protected void startException(java.io.IOException e)
Description copied from class: AbstractWatchdog
Handle the IO exception thrown from builder.start()

Specified by:
startException in class AbstractWatchdog

waitInterrupted

protected void waitInterrupted(java.lang.InterruptedException e)
Description copied from class: AbstractWatchdog
Handle the interrupt exception during the stop process.

Overrides:
waitInterrupted in class AbstractWatchdog

handleStandardError

protected void handleStandardError(java.io.InputStream stderr)
Description copied from class: AbstractWatchdog
Do something with stderr. Will not be called if builder.redirectErrorStream()

Specified by:
handleStandardError in class AbstractWatchdog

handleStandardOutput

protected void handleStandardOutput(java.io.InputStream stdout)
Description copied from class: AbstractWatchdog
Do something with stdout

Specified by:
handleStandardOutput in class AbstractWatchdog

handleStandardInput

protected void handleStandardInput(java.io.OutputStream stdin)
Description copied from class: AbstractWatchdog
Do something with the stdin

Specified by:
handleStandardInput in class AbstractWatchdog

processStart

protected void processStart()
Description copied from class: AbstractWatchdog
Called each time the process is started

Overrides:
processStart in class AbstractWatchdog

processDestroy

protected void processDestroy()
Description copied from class: AbstractWatchdog
Called when the process is destroyed forcibly during the stop sequence.

Overrides:
processDestroy in class AbstractWatchdog

processExit

protected void processExit(int result)
Description copied from class: AbstractWatchdog
Called when the process terminates of its own accord (or some external kill)

Overrides:
processExit in class AbstractWatchdog

stopped

protected void stopped()
Description copied from class: LoopRunnable
Called when the loop exits.

Overrides:
stopped in class LoopRunnable

handleGobblerException

protected void handleGobblerException(java.io.IOException e,
                                      boolean isErrorOutput)
The threads eating the stdout/stderr can throw the odd exception. We log it here (at debug)

Parameters:
e -
isErrorOutput -

gobbleOutput

protected void gobbleOutput(java.lang.String line,
                            boolean isErrorOutput)
The output of the process's stdout and stderr is sent to this method. We log it line by line, stdout at debug, stderr at error.

Parameters:
line -
isErrorOutput -


Copyright © 2008. All Rights Reserved.