Class SystemCommandTasklet
- java.lang.Object
- org.springframework.batch.core.listener.StepExecutionListenerSupport
- org.springframework.batch.core.step.tasklet.SystemCommandTasklet
- All Implemented Interfaces:
StoppableTasklet
,Tasklet
,StepExecutionListener
,StepListener
,org.springframework.beans.factory.InitializingBean
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 injectedsetTaskExecutor(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 orsetTimeout(long)
expired or job was interrupted). The check interval is given bysetTerminationCheckInterval(long)
. When job interrupt is detected tasklet's execution is terminated immediately by throwingJobInterruptedException
.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
Field Summary
Fields Modifier and Type Field Description protected static org.apache.commons.logging.Log
logger
Constructor Summary
Constructors Constructor Description SystemCommandTasklet()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
afterPropertiesSet()
void
beforeStep(StepExecution stepExecution)
Get a reference toStepExecution
for interrupt checks during system command execution.RepeatStatus
execute(StepContribution contribution, ChunkContext chunkContext)
Execute system command and map its exit code toExitStatus
usingSystemProcessExitCodeMapper
.void
setCommand(java.lang.String command)
void
setEnvironmentParams(java.lang.String[] envp)
void
setInterruptOnCancel(boolean interruptOnCancel)
Iftrue
tasklet will attempt to interrupt the thread executing the system command ifsetTimeout(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 executorvoid
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 ifsetInterruptOnCancel(boolean)
has been set to true.Methods inherited from class org.springframework.batch.core.listener.StepExecutionListenerSupport
afterStep
Method Detail
execute
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws java.lang.Exception
Execute system command and map its exit code toExitStatus
usingSystemProcessExitCodeMapper
.- Specified by:
execute
in 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
RepeatStatus
indicating whether processing is continuable. Returningnull
is interpreted asRepeatStatus.FINISHED
- Throws:
java.lang.Exception
- thrown if error occurs during execution.
setCommand
public void setCommand(java.lang.String command)
- Parameters:
command
- command to be executed in a separate system process
setEnvironmentParams
public void setEnvironmentParams(java.lang.String[] envp)
- Parameters:
envp
- environment parameter values, inherited from parent process when not set (or set to null).
setWorkingDirectory
public void setWorkingDirectory(java.lang.String dir)
- Parameters:
dir
- working directory of the spawned process, inherited from parent process when not set (or set to null).
afterPropertiesSet
public void afterPropertiesSet() throws java.lang.Exception
- Specified by:
afterPropertiesSet
in interfaceorg.springframework.beans.factory.InitializingBean
- Throws:
java.lang.Exception
setJobExplorer
public void setJobExplorer(JobExplorer jobExplorer)
setSystemProcessExitCodeMapper
public void setSystemProcessExitCodeMapper(SystemProcessExitCodeMapper systemProcessExitCodeMapper)
- Parameters:
systemProcessExitCodeMapper
- maps system process return value toExitStatus
returned by Tasklet.SimpleSystemProcessExitCodeMapper
is 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
public void beforeStep(StepExecution stepExecution)
Get a reference toStepExecution
for interrupt checks during system command execution.- Specified by:
beforeStep
in interfaceStepExecutionListener
- Overrides:
beforeStep
in classStepExecutionListenerSupport
- 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)
Iftrue
tasklet will attempt to interrupt the thread executing the system command ifsetTimeout(long)
has been exceeded or user interrupts the job.false
by 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:
stop
in interfaceStoppableTasklet
- Since:
- 3.0
- See Also:
StoppableTasklet.stop()