alma::acs::util::ProcessStreamGobbler Class Reference

Collaboration diagram for alma::acs::util::ProcessStreamGobbler:
Collaboration graph
[legend]

List of all members.

Classes

class  GobblerRunnable

Public Member Functions

 ProcessStreamGobbler (Process proc, ThreadFactory tf, boolean storeStreamContent)
void gobbleAsync () throws InterruptedException
boolean gobble (long timeout, TimeUnit unit) throws InterruptedException
List< String > getStdout ()
List< String > getStderr ()
boolean hasStreamReadErrors ()
boolean hasTerminated ()
void setDebug (boolean DEBUG)

Private Attributes

final Process proc
final List< String > stdout
final List< String > stderr
final ThreadFactory tf
GobblerRunnable runErr
GobblerRunnable runOut
volatile boolean DEBUG = false

Detailed Description

Continuously reads the stdout and stderr streams of Process in separate threads, so that the OS will not block out the JVM, as it could otherwise happen if lines are read from these streams sequentially in one thread.

This class is only useful if the external process's output is either not interesting at all for the user, or if the output should be read only after the process has terminated. Otherwise the streams should be read directly without using this class.

The gobble(long, TimeUnit) call returns either when both stdout and stderr streams deliver a null, or if the timeout is reached. check if relying on the null from both streams is as robust as Process#waitFor().

Author:
hsommer

Constructor & Destructor Documentation

alma::acs::util::ProcessStreamGobbler::ProcessStreamGobbler ( Process  proc,
ThreadFactory  tf,
boolean  storeStreamContent 
) [inline]
Parameters:
proc The process whose streams we want to read.
tf ThreadFactory to be used to create the two threads that read the stdout and stderr streams
storeStreamContent If true, then the stdout and stderr content will be stored and can be read using getStdout() and getStderr() : set max sizes for the buffers

References stderr, and stdout.


Member Function Documentation

List<String> alma::acs::util::ProcessStreamGobbler::getStderr (  )  [inline]

Returns the stderr data read, or null if storeStreamContent==false

References stderr.

Referenced by alma::acs::testsupport::ProcessUtilTest::testJava().

List<String> alma::acs::util::ProcessStreamGobbler::getStdout (  )  [inline]

Returns the stdout data read, or null if storeStreamContent==false

References stdout.

Referenced by alma::acs::testsupport::ProcessUtil::getJavaPIDs(), and alma::acs::testsupport::ProcessUtilTest::testJava().

boolean alma::acs::util::ProcessStreamGobbler::gobble ( long  timeout,
TimeUnit  unit 
) throws InterruptedException [inline]

Starts fetching process output from stdout/stderr, storing it internally so that it can later be read via getStdout() and getStderr().

Use this method if you want to wait for the process to end, limited by a timeout.

: set max sizes for the buffers

Parameters:
timeout maximum time to wait for the process to finish
unit unit for the timeout
Returns:
true if returning because the process ended, otherwise false if the timeout applied (in which case the streams will continue to be read though).
Exceptions:
InterruptedException 

References DEBUG, proc, runErr, runOut, stderr, stdout, and tf.

Referenced by alma::acs::testsupport::ProcessUtil::getJavaPIDs(), gobbleAsync(), and alma::acs::testsupport::ProcessUtilTest::testJava().

void alma::acs::util::ProcessStreamGobbler::gobbleAsync (  )  throws InterruptedException [inline]

Starts fetching process output from stdout/stderr, storing it internally so that it can later be read via getStdout() and getStderr().

Use this method if you do not want to wait for the process to end. The status can anyway be checked using hasTerminated().

Parameters:
timeout maximum time to wait for the process to finish
unit unit for the timeout
Returns:
true if returning because the process ended, otherwise false if the timeout applied (in which case the streams will continue to be read though).
Exceptions:
InterruptedException 

References gobble().

boolean alma::acs::util::ProcessStreamGobbler::hasStreamReadErrors (  )  [inline]
Returns:
true if there was 1 or more errors reading the stdout or stderr stream.
Exceptions:
IllegalStateException if called before gobble(long, TimeUnit) or gobbleAsync().

References alma::acs::util::ProcessStreamGobbler::GobblerRunnable::hasReadError, runErr, and runOut.

Referenced by alma::acs::testsupport::ProcessUtil::getJavaPIDs().

boolean alma::acs::util::ProcessStreamGobbler::hasTerminated (  )  [inline]
void alma::acs::util::ProcessStreamGobbler::setDebug ( boolean  DEBUG  )  [inline]

Member Data Documentation

volatile boolean alma::acs::util::ProcessStreamGobbler::DEBUG = false [private]

Referenced by gobble().

Referenced by gobble().

final List<String> alma::acs::util::ProcessStreamGobbler::stderr [private]
final List<String> alma::acs::util::ProcessStreamGobbler::stdout [private]
final ThreadFactory alma::acs::util::ProcessStreamGobbler::tf [private]

Referenced by gobble().


The documentation for this class was generated from the following file:

Generated by  doxygen 1.6.2