public class SystemCommandTasklet extends StepExecutionListenerSupport implements Tasklet, 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(String command) |
void |
setEnvironmentParams(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 |
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(String dir) |
afterStep
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws 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.Exception
public void setCommand(String command)
command
- command to be executed in a separate system processpublic void setEnvironmentParams(String[] envp)
envp
- environment parameter values, inherited from parent process
when not set (or set to null).public void setWorkingDirectory(String dir)
dir
- working directory of the spawned process, inherited from
parent process when not set (or set to null).public void afterPropertiesSet() throws Exception
afterPropertiesSet
in interface org.springframework.beans.factory.InitializingBean
Exception
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
public void setTaskExecutor(org.springframework.core.task.TaskExecutor 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 defaultCopyright © 2014 Pivotal. All rights reserved.