public class SystemCommandTasklet extends StepExecutionListenerSupport implements 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).Modifier and Type | Field and Description |
---|---|
protected static org.apache.commons.logging.Log |
logger |
Constructor and Description |
---|
SystemCommandTasklet() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet() |
void |
beforeStep(StepExecution stepExecution)
Get a reference to
StepExecution for interrupt checks during
system command execution. |
RepeatStatus |
execute(StepContribution contribution,
ChunkContext chunkContext)
Execute system command and map its exit code to
ExitStatus using
SystemProcessExitCodeMapper . |
void |
setCommand(java.lang.String command) |
void |
setEnvironmentParams(java.lang.String[] envp) |
void |
setInterruptOnCancel(boolean interruptOnCancel)
If
true tasklet will attempt to interrupt the thread
executing the system command if setTimeout(long) has been
exceeded or user interrupts the job. |
void |
setJobExplorer(JobExplorer jobExplorer) |
void |
setSystemProcessExitCodeMapper(SystemProcessExitCodeMapper systemProcessExitCodeMapper) |
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
|
void |
setTerminationCheckInterval(long checkInterval)
The time interval how often the tasklet will check for termination
status.
|
void |
setTimeout(long timeout)
Timeout in milliseconds.
|
void |
setWorkingDirectory(java.lang.String dir) |
void |
stop()
Will interrupt the thread executing the system command only if
setInterruptOnCancel(boolean) has been set to true. |
afterStep
@Nullable public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws java.lang.Exception
ExitStatus
using
SystemProcessExitCodeMapper
.execute
in interface Tasklet
contribution
- mutable state to be passed back to update the current
step executionchunkContext
- attributes shared between invocations but not between
restartsRepeatStatus
indicating whether processing is
continuable. Returning null
is interpreted as RepeatStatus.FINISHED
java.lang.Exception
- thrown if error occurs during execution.public void setCommand(java.lang.String command)
command
- command to be executed in a separate system processpublic void setEnvironmentParams(java.lang.String[] envp)
envp
- environment parameter values, inherited from parent process
when not set (or set to null).public void setWorkingDirectory(java.lang.String dir)
dir
- working directory of the spawned process, inherited from
parent process when not set (or set to null).public void afterPropertiesSet() throws java.lang.Exception
afterPropertiesSet
in interface org.springframework.beans.factory.InitializingBean
java.lang.Exception
public void setJobExplorer(JobExplorer jobExplorer)
public void setSystemProcessExitCodeMapper(SystemProcessExitCodeMapper systemProcessExitCodeMapper)
systemProcessExitCodeMapper
- maps system process return value to
ExitStatus
returned by Tasklet.
SimpleSystemProcessExitCodeMapper
is used by default.public void setTimeout(long timeout)
timeout
- upper limit for how long the execution of the external
program is allowed to last.public void setTerminationCheckInterval(long checkInterval)
checkInterval
- time interval in milliseconds (1 second by default).public void beforeStep(StepExecution stepExecution)
StepExecution
for interrupt checks during
system command execution.beforeStep
in interface StepExecutionListener
beforeStep
in class StepExecutionListenerSupport
stepExecution
- instance of StepExecution
.public void setTaskExecutor(org.springframework.core.task.TaskExecutor taskExecutor)
taskExecutor
- instance of TaskExecutor
.public void setInterruptOnCancel(boolean interruptOnCancel)
true
tasklet will attempt to interrupt the thread
executing the system command if setTimeout(long)
has been
exceeded or user interrupts the job. false
by defaultinterruptOnCancel
- boolean determines if process should be interruptedpublic void stop()
setInterruptOnCancel(boolean)
has been set to true. Otherwise
the underlying command will be allowed to finish before the tasklet
ends.stop
in interface StoppableTasklet
StoppableTasklet.stop()