Class ProcessExecutor

  • All Implemented Interfaces:
    java.util.concurrent.Callable<java.lang.Integer>

    public class ProcessExecutor
    extends java.lang.Object
    implements java.util.concurrent.Callable<java.lang.Integer>
    Helper to handle a process with possible input and getting output.
     ProcessExecutor clone = ProcessExecutor("git", "clone", repo);
     Future<Integer> i = executorService.submit(clone);
    
     // do something else.
    
     int exitCode = i.get();
     
    Each executor is made to handle one process, and can not be reused. The two output strings will be available as the program runs, so calling ex.getOutput() or ex.getError() while the process executor runs is entirely safe. You will just get the output snapshot at that time. NOTE: It is not possible to get a simultaneous snapshot of stdout and stderr. Also note that all program output is cached in byte arrays, so running programs with exceedingly large outputs can cause OOM errors.
    • Constructor Detail

      • ProcessExecutor

        public ProcessExecutor​(java.lang.String... cmd)
    • Method Detail

      • getOutput

        public java.lang.String getOutput()
        Returns:
        The programs stdout content.
      • getError

        public java.lang.String getError()
        Returns:
        The programs stderr content.
      • withJavaOptionsWorkaround

        public ProcessExecutor withJavaOptionsWorkaround()
        Handle a special case for java. Some times the java process may print a special "Picked up _JAVA_OPTIONS: ...\n" line, which is irrelevant for testing (and makes the tests flaky).
        Returns:
        The process executor.
      • setInput

        public ProcessExecutor setInput​(java.io.InputStream in)
        Set input stream to write to the process as program input.
        Parameters:
        in - The program input.
        Returns:
        The process executor.
      • setDeadlineMs

        public ProcessExecutor setDeadlineMs​(long deadlineMs)
        Set the program deadline. If not finished in this time interval, the run fails with an IOException.
        Parameters:
        deadlineMs - The new deadline in milliseconds. 0 means to wait forever. Default is 1 second.
        Returns:
        The process executor.
      • setDeadlineFlushMs

        public ProcessExecutor setDeadlineFlushMs​(long deadlineFlushMs)
        Set the deadline for completing the IO threads. If not finished in this time interval, the run fails with an IOException.
        Parameters:
        deadlineFlushMs - The new deadline in milliseconds. 0 means to wait forever. Default is 1 second.
        Returns:
        The process executor.
      • setDirectory

        public ProcessExecutor setDirectory​(java.io.File directory)
        Set the working directory of the process.
        Parameters:
        directory - The working directory of the process at start.
        Returns:
        The process executor.
      • handleOutput

        protected void handleOutput​(java.io.InputStream stdout)
                             throws java.io.IOException
        Handles the process' standard output stream.
        Parameters:
        stdout - The output stream reader.
        Throws:
        java.io.IOException - If reading stdout failed.
      • handleError

        protected void handleError​(java.io.InputStream stderr)
                            throws java.io.IOException
        Handles the process' standard error stream.
        Parameters:
        stderr - The error stream reader.
        Throws:
        java.io.IOException - If reading stderr failed.
      • handleProcessTimeout

        protected void handleProcessTimeout​(java.lang.String[] cmd)
                                     throws java.io.IOException
        Handle timeout on the process finishing.
        Parameters:
        cmd - The command that was run.
        Throws:
        java.io.IOException - If not handled otherwise.
      • call

        public java.lang.Integer call()
                               throws java.io.IOException
        Specified by:
        call in interface java.util.concurrent.Callable<java.lang.Integer>
        Throws:
        java.io.IOException