Package io.microsphere.process
Class ProcessExecutor
- java.lang.Object
-
- io.microsphere.process.ProcessExecutor
-
public class ProcessExecutor extends java.lang.Object
Process
ExecutorExecutes operating system commands and manages the execution lifecycle, including handling input/output streams, monitoring process status, and enforcing timeouts. This class wraps command execution in a structured way to provide enhanced control and integration with the framework.
Example Usage
// Execute a simple command with options and capture output ProcessExecutor executor = new ProcessExecutor("ls", "-l", "/home"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); executor.execute(outputStream); System.out.println("Command Output: " + outputStream.toString()); // Execute a command with timeout protection try { executor.execute(outputStream, 5000); // 5 seconds timeout } catch (TimeoutException e) { System.err.println("Command timed out!"); }
- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
ProcessManager
-
-
Constructor Summary
Constructors Constructor Description ProcessExecutor(java.lang.String command, java.lang.String... options)
Constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
execute(java.io.OutputStream outputStream)
Execute current process.void
execute(java.io.OutputStream outputStream, long timeoutInMilliseconds)
Execute current process.boolean
isFinished()
Check current process finish or not.
-
-
-
Method Detail
-
execute
public void execute(java.io.OutputStream outputStream) throws java.io.IOException
Execute current process. // * @param inputStream input stream keeps output stream from process- Parameters:
outputStream
- output stream for process normal or error input stream.- Throws:
java.io.IOException
- if process execution is failed.
-
execute
public void execute(java.io.OutputStream outputStream, long timeoutInMilliseconds) throws java.io.IOException, java.util.concurrent.TimeoutException
Execute current process. // * @param inputStream input stream keeps output stream from process- Parameters:
outputStream
- output stream for process normal or error input stream.timeoutInMilliseconds
- milliseconds timeout- Throws:
java.io.IOException
- if process execution is failed.java.util.concurrent.TimeoutException
- if the execution is timeout over specifiedtimeoutInMilliseconds
-
isFinished
public boolean isFinished()
Check current process finish or not.- Returns:
true
if current process finished
-
-