public class GenericProcessCapture extends GenericProcessConsole
By default, this subclass of GenericProcessConsole
disables the out and err
streams from appearing in the debug/console output. Users can change this behavior
by overriding the debug() method and also by turning on
the output of either or both of the streams via GenericProcessConsole.setShowOutStream(boolean)
and
GenericProcessConsole.setShowErrStream(boolean)
.
Normal usage might be something like below:
Process process = runtime.exec(procstr); GenericProcessCapture console = new GenericProcessCapture(process); Thread athread = new Thread(console); athread.start(); //we can wait until process is finished try{ athread.join();}catch(InterruptedException x){;} console.shutdown();//precaution Vector data = console.getData();Alternatively, we can autostart the capture thread. We don't have to wait for it to end if we don't want to. The autostarted thread can be acquired, though:
Process process = runtime.exec(procstr); GenericProcessCapture console = new GenericProcessCapture(process, null, true); //we can wait until process is finished if we want try{ console.thread.join();}catch(InterruptedException x){;} Vector data = console.getData(); console.shutdown();//precautionary force shutdownData for the Error and Output streams is captured into the single Vector storage. The Error stream data is prefixed with the ERR_PREFIX, while the Output data is prefixed with the OUT_PREFIX.
This class contains no extended SAFS dependencies and can be readily packaged and distributed for non-SAFS installations.
Modifier and Type | Field and Description |
---|---|
protected java.util.Vector |
data |
java.lang.String |
monitor
If non-null, we will check to see if this CMD(Unix), IMAGE(Win), or PID is running.
|
java.lang.Thread |
thread
The (running) thread used to autostart the process capture, if applicable.
|
err, ERR_PREFIX, exceptions, exited, exitValue, in, out, OUT_PREFIX, process, showErrStream, showOutStream, shutdown
Constructor and Description |
---|
GenericProcessCapture(java.lang.Process process)
Default Constructor for GenericProcessCapture.
|
GenericProcessCapture(java.lang.Process process,
java.lang.String monitor,
boolean autostart)
Alternative constructor allowing the IO thread to remain open as long as
a secondary process/pid remains running (if specified).
|
GenericProcessCapture(java.lang.Process process,
java.lang.String monitor,
boolean autostart,
boolean debug2console)
Alternative constructor allowing the IO thread to remain open as long as
a secondary process/pid remains running (if specified).
|
Modifier and Type | Method and Description |
---|---|
java.util.Vector |
getData()
Return a snapshot(copy) of the String lines of data from the streams.
|
int |
getDataLineCount() |
void |
run()
Continuously monitors the process out and err streams routing them to the local
process System.out and System.err.
|
void |
waitOutput(long timeout,
int stdoutLineToWait,
int stderrLineToWait)
Wait for the number of line on both STDOUT and STDERR is bigger than some expected number respectively.
|
_initialize, debug, getExceptions, getExceptionsCount, getExitValue, isExited, setShowErrStream, setShowOutStream, shutdown
protected java.util.Vector data
public java.lang.String monitor
public java.lang.Thread thread
public GenericProcessCapture(java.lang.Process process)
process
- to capture IO in, out, and err streams.thread
,
GenericProcessConsole.setShowOutStream(boolean)
,
GenericProcessConsole.setShowErrStream(boolean)
public GenericProcessCapture(java.lang.Process process, java.lang.String monitor, boolean autostart)
process
- -- to capture IO in, out, and err streams.monitor
- -- optional secondary process name or pid to monitor for continued
IO output.autostart
- -- true if we should automatically start the separate capture thread.thread
,
GenericProcessConsole.setShowOutStream(boolean)
,
GenericProcessConsole.setShowErrStream(boolean)
public GenericProcessCapture(java.lang.Process process, java.lang.String monitor, boolean autostart, boolean debug2console)
process
- -- to capture IO in, out, and err streams.monitor
- -- optional secondary process name or pid to monitor for continued
IO output.autostart
- -- true if we should automatically start the separate capture thread.debug2console
- set true to have the out and err streams copied to the debug() sink.public void waitOutput(long timeout, int stdoutLineToWait, int stderrLineToWait) throws java.util.concurrent.TimeoutException
timeout
- long, the timeout in millisecond.stdoutLineToWait
- int, the number of line on STDOUT to wait.stderrLineToWait
- int, the number of line on STDERR to wait.java.util.concurrent.TimeoutException
- if the timeout has been reached before the condition is satisfied.public java.util.Vector getData()
public int getDataLineCount()
public void run()
GenericProcessConsole
run
in interface java.lang.Runnable
run
in class GenericProcessConsole
GenericProcessConsole.getExceptions()
,
GenericProcessConsole.shutdown()
Copyright © SAS Institute. All Rights Reserved.