Package io.microsphere.process
Class ProcessExecutor
- java.lang.Object
-
- io.microsphere.process.ProcessExecutor
-
public class ProcessExecutor extends java.lang.ObjectProcessExecutorExecutes 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
-
-
Field Summary
Fields Modifier and Type Field Description static longDEFAULT_PROCESS_EXECUTION_TIMEOUTThe default value for the timeout of process execution : 30 secondsstatic java.lang.StringDEFAULT_PROCESS_EXECUTION_TIMEOUT_PROPERTY_VAUEThe default proeprty value for the timeout of process execution : 30 secondsstatic longDEFAULT_TIMEOUTthe timeout of process executionstatic java.lang.StringPROCESS_EXECUTION_TIMEOUT_PROPERTY_NAMEThe property name for the timeout of process execution : "process.execution.timeout"
-
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 voidexecute(java.io.OutputStream outputStream)Execute current process.voidexecute(java.io.OutputStream outputStream, long timeoutInMilliseconds)Execute current process.voidexecute(java.io.OutputStream outputStream, long timeout, java.util.concurrent.TimeUnit timeUnit)Execute current process.
-
-
-
Field Detail
-
DEFAULT_PROCESS_EXECUTION_TIMEOUT_PROPERTY_VAUE
public static final java.lang.String DEFAULT_PROCESS_EXECUTION_TIMEOUT_PROPERTY_VAUE
The default proeprty value for the timeout of process execution : 30 seconds- See Also:
- Constant Field Values
-
DEFAULT_PROCESS_EXECUTION_TIMEOUT
public static final long DEFAULT_PROCESS_EXECUTION_TIMEOUT
The default value for the timeout of process execution : 30 seconds
-
PROCESS_EXECUTION_TIMEOUT_PROPERTY_NAME
@ConfigurationProperty(type=long.class, defaultValue="30000", source="system-properties") public static final java.lang.String PROCESS_EXECUTION_TIMEOUT_PROPERTY_NAMEThe property name for the timeout of process execution : "process.execution.timeout"
-
DEFAULT_TIMEOUT
public static final long DEFAULT_TIMEOUT
the timeout of process execution
-
-
Method Detail
-
execute
public void execute(java.io.OutputStream outputStream) throws java.io.IOException, java.util.concurrent.TimeoutExceptionExecute current process.- Parameters:
outputStream- output stream for process normal or error input stream.- Throws:
java.io.IOException- if process execution is failed.java.util.concurrent.TimeoutException- if the execution is timeout over specifiedDEFAULT_TIMEOUT
-
execute
public void execute(java.io.OutputStream outputStream, long timeoutInMilliseconds) throws java.io.IOException, java.util.concurrent.TimeoutExceptionExecute current 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
-
execute
public void execute(java.io.OutputStream outputStream, long timeout, java.util.concurrent.TimeUnit timeUnit) throws java.io.IOException, java.util.concurrent.TimeoutExceptionExecute current process.- Parameters:
outputStream- output stream for process normal or error input stream.timeout- the timeout valuetimeUnit-TimeUnit- Throws:
java.io.IOException- if process execution is failed.java.util.concurrent.TimeoutException- if the execution is timeout over specifiedtimeoutandtimeUnit
-
-