Class ProcessStreamDrainer
- java.lang.Object
-
- com.sun.enterprise.universal.process.ProcessStreamDrainer
-
public class ProcessStreamDrainer extends Object
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(); }
- Since:
- October 26, 2006
- Author:
- bnevins
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ProcessStreamDrainer
dispose(String processName, Process process)
Create an instance, drain and throw away the process' stderr and stdout output.static ProcessStreamDrainer
drain(String processName, Process process)
Create an instance and drain the process' stderr and stdoutString
getErrString()
String
getOutErrString()
String
getOutString()
static ProcessStreamDrainer
redirect(String processName, Process process)
Create an instance, drain and redirect the process' stderr and stdout to System.err and System.out respectively.static ProcessStreamDrainer
save(String processName, Process process)
Create an instance and drain the process' stderr and stdout and save it to strings.void
waitFor()
Wait for the drain threads to die.
-
-
-
Method Detail
-
drain
public static ProcessStreamDrainer drain(String processName, Process process)
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
public static ProcessStreamDrainer save(String processName, Process process)
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
public static ProcessStreamDrainer redirect(String processName, Process process)
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
public static ProcessStreamDrainer dispose(String processName, Process process)
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
public final void waitFor() throws InterruptedException
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
public final String getOutString()
-
getErrString
public final String getErrString()
-
getOutErrString
public final String getOutErrString()
-
-