Class ProcessWrapper
java.lang.Object
com.pnfsoftware.jeb.util.concurrent.ProcessWrapper
- All Implemented Interfaces:
AutoCloseable2,AutoCloseable
A wrapper around
Process to execute potentially long-running commands, that may require
input. The process streams are accessible; it is the responsibility of the client to read (or
drain) the output and error streams to avoid the chance of blocking on
waitForCompletion(). This class is an auto-closable and may be treated as such
(try-with-ARM).
A higher-level, safer alternative to consider if you do not need to handle the process streams is
to use ProcessExec.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionProcessWrapper(long timeoutMs, boolean redirectErrToOut, String... cmdarray) Execute a process with timeout.ProcessWrapper(String... cmdarray) Execute a process without timeout. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Redirects tokill().Get the process standard error stream.Get the process standard input stream.Get the process standard output stream.Get the process return code.booleanisAlive()Determine whether the wrapped process is running.voidkill()Terminate the process if it is still running and record its return code when available.start()Start the process.toString()voidWait for the process to complete, then update the return code.
-
Constructor Details
-
ProcessWrapper
Execute a process without timeout. The caller is advised to take care of the target process output and error streams.- Parameters:
cmdarray- command
-
ProcessWrapper
Execute a process with timeout.- Parameters:
timeoutMs- timeout in milliseconds before the process gets killed (<= 0means no timeout)redirectErrToOut- true to redirect the process error stream to the output streamcmdarray- command
-
-
Method Details
-
start
Start the process.- Returns:
- this wrapper
- Throws:
IOException- if the process cannot be started
-
kill
public void kill()Terminate the process if it is still running and record its return code when available. -
close
public void close()Redirects tokill().- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceAutoCloseable2
-
waitForCompletion
public void waitForCompletion()Wait for the process to complete, then update the return code. Calling this method without having reader threads draining the target process's output and error is dangerous unless a timeout was set: if the process generates a lot of output, it will stall indefinitely and block the current thread. -
isAlive
public boolean isAlive()Determine whether the wrapped process is running.- Returns:
- true if the process was started and is still alive
-
getProcessInput
Get the process standard input stream.- Returns:
- the process input stream, or null if the process has not been started
-
getProcessOutput
Get the process standard output stream.- Returns:
- the process output stream, or null if the process has not been started
-
getProcessError
Get the process standard error stream.- Returns:
- the process error stream, or null if the process has not been started
-
getReturnCode
Get the process return code.- Returns:
- the process return code, or null if no return code has been recorded
-
toString
-