Class ProcessOutputHandler
java.lang.Object
org.kiwiproject.beta.base.process.ProcessOutputHandler
- All Implemented Interfaces:
Closeable,AutoCloseable
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.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe type of output from the process.static enumThe return type for the handler methods. -
Constructor Summary
ConstructorsConstructorDescriptionProcessOutputHandler(int threadPoolSize, int bufferCapacityBytes, long sleepTimeMillis) Create a new instance.Create a new instance. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()handle(Process process, ProcessOutputHandler.ProcessOutputType outputType, Consumer<String> outputConsumer) Handle the output or error output of the given process.handleErrorOutput(Process process, Consumer<String> errorConsumer) Handle the error output of the given process.handleStandardOutput(Process process, Consumer<String> outputConsumer) Handle the standard output of the given process.
-
Constructor Details
-
ProcessOutputHandler
Create a new instance.- Parameters:
config- the configuration to use- See Also:
-
ProcessOutputHandler
public ProcessOutputHandler(int threadPoolSize, int bufferCapacityBytes, long sleepTimeMillis) Create a 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:
-
-
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 processoutputConsumer- 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 processerrorConsumer- 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 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
-