Class SystemCommandTasklet
java.lang.Object
org.springframework.batch.core.step.tasklet.SystemCommandTasklet
- All Implemented Interfaces:
StoppableTasklet,Tasklet,StepExecutionListener,StepListener,org.springframework.beans.factory.InitializingBean
public class SystemCommandTasklet
extends Object
implements StepExecutionListener, StoppableTasklet, org.springframework.beans.factory.InitializingBean
Tasklet that executes a system command.
The system command is executed asynchronously using injected
setTaskExecutor(TaskExecutor) - timeout value is required to be set, so that
the batch job does not hang forever if the external process hangs.
Tasklet periodically checks for termination status (i.e. setCommand(String...)
finished its execution or setTimeout(long) expired or job was interrupted).
The check interval is given by setTerminationCheckInterval(long).
When job interrupt is detected tasklet's execution is terminated immediately by
throwing JobInterruptedException.
setInterruptOnCancel(boolean) specifies whether the tasklet should attempt to
interrupt the thread that executes the system command if it is still running when
tasklet exits (abnormally).
- Author:
- Robert Kasanicky, Will Schipp, Mahmoud Ben Hassine, Injae Kim
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidvoidbeforeStep(StepExecution stepExecution) Get a reference toStepExecutionfor interrupt checks during system command execution.execute(StepContribution contribution, ChunkContext chunkContext) Execute system command and map its exit code toExitStatususingSystemProcessExitCodeMapper.voidsetCommand(String... command) Set the command to execute along with its arguments.voidsetCommandRunner(CommandRunner commandRunner) Injection setter for theCommandRunner.voidsetEnvironmentParams(String[] envp) voidsetInterruptOnCancel(boolean interruptOnCancel) Iftruetasklet will attempt to interrupt the thread executing the system command ifsetTimeout(long)has been exceeded or user interrupts the job.voidsetJobExplorer(JobExplorer jobExplorer) voidsetSystemProcessExitCodeMapper(SystemProcessExitCodeMapper systemProcessExitCodeMapper) voidsetTaskExecutor(org.springframework.core.task.TaskExecutor taskExecutor) Sets the task executor that will be used to execute the system command NB!voidsetTerminationCheckInterval(long checkInterval) The time interval how often the tasklet will check for termination status.voidsetTimeout(long timeout) Timeout in milliseconds.voidvoidstop()Will interrupt the thread executing the system command only ifsetInterruptOnCancel(boolean)has been set to true.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.batch.core.StepExecutionListener
afterStep
-
Field Details
-
logger
protected static final org.apache.commons.logging.Log logger
-
-
Constructor Details
-
SystemCommandTasklet
public SystemCommandTasklet()
-
-
Method Details
-
execute
@Nullable public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception Execute system command and map its exit code toExitStatususingSystemProcessExitCodeMapper.- Specified by:
executein interfaceTasklet- Parameters:
contribution- mutable state to be passed back to update the current step executionchunkContext- attributes shared between invocations but not between restarts- Returns:
- an
RepeatStatusindicating whether processing is continuable. Returningnullis interpreted asRepeatStatus.FINISHED - Throws:
Exception- thrown if error occurs during execution.
-
setCommandRunner
Injection setter for theCommandRunner.- Parameters:
commandRunner-CommandRunnerinstance to be used by SystemCommandTasklet instance. Defaults toJvmCommandRunner.- Since:
- 5.0
-
setCommand
Set the command to execute along with its arguments. For example:setCommand("myCommand", "myArg1", "myArg2");- Parameters:
command- command to be executed in a separate system process.
-
setEnvironmentParams
- Parameters:
envp- environment parameter values, inherited from parent process when not set (or set to null).
-
setWorkingDirectory
- Parameters:
dir- working directory of the spawned process, inherited from parent process when not set (or set to null).
-
afterPropertiesSet
- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean- Throws:
Exception
-
setJobExplorer
-
setSystemProcessExitCodeMapper
- Parameters:
systemProcessExitCodeMapper- maps system process return value toExitStatusreturned by Tasklet.SimpleSystemProcessExitCodeMapperis used by default.
-
setTimeout
public void setTimeout(long timeout) Timeout in milliseconds.- Parameters:
timeout- upper limit for how long the execution of the external program is allowed to last.
-
setTerminationCheckInterval
public void setTerminationCheckInterval(long checkInterval) The time interval how often the tasklet will check for termination status.- Parameters:
checkInterval- time interval in milliseconds (1 second by default).
-
beforeStep
Get a reference toStepExecutionfor interrupt checks during system command execution.- Specified by:
beforeStepin interfaceStepExecutionListener- Parameters:
stepExecution- instance ofStepExecution.
-
setTaskExecutor
public void setTaskExecutor(org.springframework.core.task.TaskExecutor taskExecutor) Sets the task executor that will be used to execute the system command NB! Avoid using a synchronous task executor- Parameters:
taskExecutor- instance ofTaskExecutor.
-
setInterruptOnCancel
public void setInterruptOnCancel(boolean interruptOnCancel) Iftruetasklet will attempt to interrupt the thread executing the system command ifsetTimeout(long)has been exceeded or user interrupts the job.falseby default- Parameters:
interruptOnCancel- boolean determines if process should be interrupted
-
stop
public void stop()Will interrupt the thread executing the system command only ifsetInterruptOnCancel(boolean)has been set to true. Otherwise the underlying command will be allowed to finish before the tasklet ends.- Specified by:
stopin interfaceStoppableTasklet- Since:
- 3.0
- See Also:
-