com.bigdata.jini.start.process
Class ProcessHelper

java.lang.Object
  extended by com.bigdata.jini.start.process.ProcessHelper
Direct Known Subclasses:
JiniCoreServicesProcessHelper, JiniServiceProcessHelper, ZookeeperProcessHelper

public class ProcessHelper
extends Object

Helper object for a running Process that DOES NOT require any input. The output of the process will be logged, but not otherwise processed.

Version:
$Id$
Author:
Bryan Thompson

Field Summary
protected static org.apache.log4j.Logger log
           
 String name
          A useful name for the process.
 
Constructor Summary
ProcessHelper(String name, ProcessBuilder processBuilder, IServiceListener listener)
          Starts the Process, starts a Thread to consume its output, and registers the Process with the IServiceListener.
 
Method Summary
protected  void consumeOutput()
          Consumes the output of the process, writing each line onto a Logger.
 boolean equals(Object o)
          Only accept reference tests for equality.
 int exitValue()
          Await the exit value and return it when it becomes available.
 int exitValue(long timeout, TimeUnit unit)
          Waits up to timeout units for the Process to terminate and then returns its exit value.
static String getCommandString(ProcessBuilder processBuilder)
          Return the command line that would be executed.
static String getEnvironment(ProcessBuilder processBuilder)
          Return a String containing commands to set the environment for the processs.
 Future interruptWhenProcessDies(long timeout, TimeUnit unit)
          Interrupts the caller's Thread if the process dies within the specified timeout.
 int kill(boolean immediateShutdown)
          Kill the process, blocking until it has terminated.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

log

protected static final org.apache.log4j.Logger log

name

public final String name
A useful name for the process.

Constructor Detail

ProcessHelper

public ProcessHelper(String name,
                     ProcessBuilder processBuilder,
                     IServiceListener listener)
              throws IOException
Starts the Process, starts a Thread to consume its output, and registers the Process with the IServiceListener.

Parameters:
name - A useful name for the process.
processBuilder - The object used to start the Process.
running - A Queue of the running Processes.
Throws:
IOException
Method Detail

toString

public String toString()
Overrides:
toString in class Object

exitValue

public int exitValue()
              throws InterruptedException
Await the exit value and return it when it becomes available.

Returns:
The exit value.
Throws:
InterruptedException - if the caller's thread is interrupted while awaiting the process exit value.

exitValue

public int exitValue(long timeout,
                     TimeUnit unit)
              throws TimeoutException,
                     InterruptedException
Waits up to timeout units for the Process to terminate and then returns its exit value.

Parameters:
timeout -
unit -
Returns:
The exit value.
Throws:
TimeoutException - if the Process is still running when the specified timeout elapsed.
InterruptedException - if the caller's thread was interrupted awaiting the exit value.

kill

public int kill(boolean immediateShutdown)
         throws InterruptedException
Kill the process, blocking until it has terminated. The contract is only "kill" not "destroy" - the persistent state of the process SHOULD NOT be destroyed). Subclasses SHOULD override this method to request normal process termination where possible.

Note: processes with child processes (including any bigdata services since they start children to report OS performance counters) MUST exit normally (at least under windows) or the parent process will not be able to exit. Therefore it is very important to extend this method and send proper notice to the process requesting that it terminate itself.

Parameters:
immediateShutdown - processes with APIs that differentiate immediate shutdown and normal shutdown will use the appropriate behavior as selected by this parameter.
Returns:
The exitValue of the process.
Throws:
InterruptedException - if interrupted - the process may or may not have been killed and the listener will not have been notified.

equals

public boolean equals(Object o)
Only accept reference tests for equality.

Overrides:
equals in class Object

consumeOutput

protected void consumeOutput()
Consumes the output of the process, writing each line onto a Logger.

Note: Normally you will see the child process output by configuring logging for the child process. However, if you want to see the output of the child process within the logging of this process, then you have to raise the log level to INFO for this class.


interruptWhenProcessDies

public Future interruptWhenProcessDies(long timeout,
                                       TimeUnit unit)
Interrupts the caller's Thread if the process dies within the specified timeout.

This thread can still be running after call() has returned and it can cause a spurious interrupt. To avoid that you MUST cancel the thread monitoring for the process death using the returned Future.

Parameters:
timeout - The timeout.
unit - The timeout unit.
Returns:
The Future for the Thread awaiting the task death.
TODO:
isRunning() does not appear to be atomic with respect to the interrupt of the caller. In fact, it seems like you have to use exitValue(long, TimeUnit) with a small timeout to verify that the process is in fact dead. That is weird. I am even forcing the wait for the exit value in this method but to no avail.

getEnvironment

public static String getEnvironment(ProcessBuilder processBuilder)
Return a String containing commands to set the environment for the processs.


getCommandString

public static String getCommandString(ProcessBuilder processBuilder)
Return the command line that would be executed.

Parameters:
processBuilder -
Returns:


Copyright © 2006-2009 SYSTAP, LLC. All Rights Reserved.