Package org.jppf.node.protocol
Class CommandLineTask<T>
java.lang.Object
org.jppf.node.protocol.AbstractTask<T>
org.jppf.node.protocol.CommandLineTask<T>
- Type Parameters:
T- the type of results returned by this task.
- All Implemented Interfaces:
Serializable,Runnable,EventListener,Interruptibility,PositionalElement<Task<T>>,Task<T>,ProcessWrapperEventListener
public abstract class CommandLineTask<T>
extends AbstractTask<T>
implements ProcessWrapperEventListener
Instances of this class encapsulate the execution of an external process, program or shell script.
This task starts and external process using command line arguments, environment variables, and a list of input and/or output files to use or generated by the external process.
This task also captures the standard and error output (i.e. equivalent to System.out and System.err) of the external process.
This task starts and external process using command line arguments, environment variables, and a list of input and/or output files to use or generated by the external process.
This task also captures the standard and error output (i.e. equivalent to System.out and System.err) of the external process.
- Author:
- Laurent Cohen
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor.CommandLineTask(String... commands) Create an instance of this class and set the parameters of the external process or script to launch.Create an instance of this class and set the parameters of the external process or script to launch. -
Method Summary
Modifier and TypeMethodDescriptionvoidNotification that the process has written to its error stream.Get the list of command-line arguments.getEnv()Get the environment variables to set.Get the content of the error output for the process.intGet the exit code returned by the sub-process.Get the process that is launched by this task.Get the content of the standard output for the process.Get the directory to start the command in.booleanDetermines whether the process output is captured.intRun the external process or script.voidNotification that the process has written to its output stream.setCaptureOutput(boolean captureOutput) Specifies whether the process output is captured.setCommandList(String... commands) Set the list of command-line arguments.setCommandList(List<String> commandList) Set the list of command-line arguments.Get the environment variables to set.setStartDir(String startDir) Set the directory to start the command in.Methods inherited from class org.jppf.node.protocol.AbstractTask
compute, fireNotification, getDataProvider, getId, getJob, getMaxResubmits, getNode, getPosition, getResult, getTaskClassLoader, getTaskObject, getThrowable, getTimeoutSchedule, isInNode, isInterruptible, isResubmit, onCancel, onTimeout, run, setDataProvider, setExecutionDispatcher, setId, setInNode, setJob, setMaxResubmits, setNode, setPosition, setResubmit, setResult, setThrowable, setTimeoutSchedule
-
Constructor Details
-
CommandLineTask
public CommandLineTask()Default constructor. -
CommandLineTask
Create an instance of this class and set the parameters of the external process or script to launch.- Parameters:
commands- the list of command-line arguments.
-
CommandLineTask
Create an instance of this class and set the parameters of the external process or script to launch.- Parameters:
env- the environment variables to set.startDir- the directory to start the command in.commands- the list of command-line arguments.
-
-
Method Details
-
launchProcess
Run the external process or script.- Returns:
- the exit code returned by the sub-process.
- Throws:
Exception- if an error occurs.
-
isCaptureOutput
public boolean isCaptureOutput()Determines whether the process output is captured.- Returns:
- true if the output is captured, false otherwise.
-
setCaptureOutput
Specifies whether the process output is captured.- Parameters:
captureOutput- true if the output is captured, false otherwise.- Returns:
- this task, for method chaining.
-
getStandardOutput
Get the content of the standard output for the process.- Returns:
- the output as a string.
-
getErrorOutput
Get the content of the error output for the process.- Returns:
- the output as a string.
-
getCommandList
Get the list of command-line arguments.- Returns:
- a list of arguments as strings.
-
setCommandList
Set the list of command-line arguments.- Parameters:
commandList- a list of arguments as strings.- Returns:
- this task, for method chaining.
-
setCommandList
Set the list of command-line arguments.- Parameters:
commands- a list of arguments as strings.- Returns:
- this task, for method chaining.
-
getEnv
Get the environment variables to set.- Returns:
- a map of variable names to their corresponding values.
-
setEnv
Get the environment variables to set.- Parameters:
env- a map of variable names to their corresponding values.- Returns:
- this task, for method chaining.
-
getStartDir
Get the directory to start the command in.- Returns:
- the start directory as a string.
-
setStartDir
Set the directory to start the command in.- Parameters:
startDir- the start directory as a string.- Returns:
- this task, for method chaining.
-
outputStreamAltered
Notification that the process has written to its output stream.- Specified by:
outputStreamAlteredin interfaceProcessWrapperEventListener- Parameters:
event- encapsulates the output stream's content.
-
errorStreamAltered
Notification that the process has written to its error stream.- Specified by:
errorStreamAlteredin interfaceProcessWrapperEventListener- Parameters:
event- encapsulate the error stream's content.
-
getExitCode
public int getExitCode()Get the exit code returned by the sub-process.- Returns:
- the value of the exit code returned by the sub-process. A negative value indicates the process was never launched or never returned.
-
getProcess
Get the process that is launched by this task.- Returns:
- a
Processinstance.
-