com.sun.enterprise.universal.process
Class ProcessStreamDrainer

java.lang.Object
  extended by 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();
 }
 

Author:
bnevins

Method Summary
static ProcessStreamDrainer drain(String processName, Process process)
          Create an instance and drain the process' stderr and stdout
 String 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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 drain
processName - 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 drain
processName - 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 drain
processName - 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()


Copyright © 2012 GlassFish Community. All Rights Reserved.