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 givenProcess, consume its standard or error output and send it to aConsumerfor 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 anExecutorService.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classProcessOutputHandler.ProcessOutputTypeThe type of output from the process.static classProcessOutputHandler.ResultThe return type for the handler methods.
-
Constructor Summary
Constructors Constructor Description ProcessOutputHandler(int threadPoolSize, int bufferCapacityBytes, long sleepTimeMillis)Create new instance.ProcessOutputHandler(ProcessOutputHandlerConfig config)Create new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()ProcessOutputHandler.Resulthandle(Process process, ProcessOutputHandler.ProcessOutputType outputType, Consumer<String> outputConsumer)Handle the output or error output of the given process.ProcessOutputHandler.ResulthandleErrorOutput(Process process, Consumer<String> errorConsumer)Handle the error output of the given process.ProcessOutputHandler.ResulthandleStandardOutput(Process process, Consumer<String> outputConsumer)Handle the standard output of the given process.
-
-
-
Constructor Detail
-
ProcessOutputHandler
public ProcessOutputHandler(ProcessOutputHandlerConfig config)
Create new instance.- Parameters:
config- the configuration to use- See Also:
ProcessOutputHandler(int, int, long)
-
ProcessOutputHandler
public ProcessOutputHandler(int threadPoolSize, int bufferCapacityBytes, long sleepTimeMillis)Create new instance.- Parameters:
threadPoolSize- the number of threads to use when handling process outputbufferCapacityBytes- 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:
ProcessOutputHandler(ProcessOutputHandlerConfig)
-
-
Method Detail
-
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 processoutputConsumer- a Consumer that will handle the standard output of the process- Returns:
- the Result
- See Also:
Process.getInputStream()
-
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 processerrorConsumer- a Consumer that will handle the error output of the process- Returns:
- the Result
- See Also:
Process.getErrorStream()
-
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 processoutputType- what type of process to handleoutputConsumer- a Consumer that will handle the selected type of process output- Returns:
- the Result
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
-