Class ProcessOutputHandler

java.lang.Object
org.kiwiproject.beta.base.process.ProcessOutputHandler
All Implemented Interfaces:
Closeable, AutoCloseable

@Beta public class ProcessOutputHandler extends Object implements Closeable
For a given Process, consume its standard or error output and send it to a Consumer for processing. The main use case is when your code launches a process and needs to receive and handle the standard output and/or error of that process. Each process is handled asynchronously via tasks submitted to an ExecutorService.
  • Constructor Details

    • ProcessOutputHandler

      public ProcessOutputHandler(ProcessOutputHandlerConfig config)
      Create new instance.
      Parameters:
      config - the configuration to use
      See Also:
    • ProcessOutputHandler

      public ProcessOutputHandler(int threadPoolSize, int bufferCapacityBytes, long sleepTimeMillis)
      Create new instance.
      Parameters:
      threadPoolSize - the number of threads to use when handling process output
      bufferCapacityBytes - the size of the buffer that will be used when reading process output (in bytes)
      sleepTimeMillis - the amount of time to sleep between reading output from the process (in milliseconds)
      See Also:
  • Method Details

    • handleStandardOutput

      public ProcessOutputHandler.Result handleStandardOutput(Process process, Consumer<String> outputConsumer)
      Handle the standard output of the given process.

      Note that the consumer will receive output in chunks up to the size of the buffer capacity. Each chunk might only be a fraction of the buffer size.

      Parameters:
      process - the process
      outputConsumer - a Consumer that will handle the standard output of the process
      Returns:
      the Result
      See Also:
    • handleErrorOutput

      public ProcessOutputHandler.Result handleErrorOutput(Process process, Consumer<String> errorConsumer)
      Handle the error output of the given process.

      Note that the consumer will receive output in chunks up to the size of the buffer capacity. Each chunk might only be a fraction of the buffer size.

      Parameters:
      process - the process
      errorConsumer - a Consumer that will handle the error output of the process
      Returns:
      the Result
      See Also:
    • handle

      public ProcessOutputHandler.Result handle(Process process, ProcessOutputHandler.ProcessOutputType outputType, Consumer<String> outputConsumer)
      Handle the output or error output of the given process.

      Note that the consumer will receive output in chunks up to the size of the buffer capacity. Each chunk might only be a fraction of the buffer size.

      Parameters:
      process - the process
      outputType - what type of process to handle
      outputConsumer - a Consumer that will handle the selected type of process output
      Returns:
      the Result
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable