Class ProcessStreamDrainer
java.lang.Object
com.sun.enterprise.universal.process.ProcessStreamDrainer
If you don't drain a process' stdout and stderr it will cause a deadlock after a few hundred bytes of output.
At that point the Process is blocked because its stdout and/or stderr buffer is full and it is waiting for the Java caller
to drain it. Meanwhile the Java program is blocked waiting on the external process.
This class makes this common, but messy and tricky, procedure easier.
It creates 2 threads that drain output on stdout and stderr of the external process.
Sample Code:
ProcessBuilder pb = new ProcessBuilder("ls", "-R", "c:/as");
try
{
Process p = pb.start();
ProcessStreamDrainer psd = ProcessStreamDrainer.drain("MyProcess", p);
// or
ProcessStreamDrainer psd = ProcessStreamDrainer.redirect("MyProcess", p);
psd.waitFor(); // this is optional.
}
catch (Exception ex)
{
ex.printStackTrace();
}
- Author:
- bnevins
-
Method Summary
Modifier and TypeMethodDescriptionstatic ProcessStreamDrainerCreate an instance, drain and throw away the process' stderr and stdout output.static ProcessStreamDrainerCreate an instance and drain the process' stderr and stdoutfinal Stringfinal Stringfinal Stringstatic ProcessStreamDrainerCreate an instance, drain and redirect the process' stderr and stdout to System.err and System.out respectively.static ProcessStreamDrainerCreate an instance and drain the process' stderr and stdout and save it to strings.final voidwaitFor()Wait for the drain threads to die.
-
Method Details
-
drain
Create an instance and drain the process' stderr and stdout- Parameters:
process- The Process to drainprocessName- The name will be used to name the drainer threads
-
save
Create an instance and drain the process' stderr and stdout and save it to strings.- Parameters:
process- The Process to drainprocessName- The name will be used to name the drainer threads
-
redirect
Create an instance, drain and redirect the process' stderr and stdout to System.err and System.out respectively.- Parameters:
process- The Process to drainprocessName- The name will be used to name the drainer threads
-
dispose
Create an instance, drain and throw away the process' stderr and stdout output.- Parameters:
process- The Process to drainprocessName- The name will be used to name the drainer threads
-
waitFor
Wait for the drain threads to die. This is guaranteed to occur after the external process dies. Note that this may, of course, block indefinitely.- Throws:
InterruptedException
-
getOutString
-
getErrString
-
getOutErrString
-