org.springframework.batch.core.step.tasklet
Class SystemCommandTasklet

java.lang.Object
  extended by org.springframework.batch.core.listener.StepExecutionListenerSupport
      extended by org.springframework.batch.core.step.tasklet.SystemCommandTasklet
All Implemented Interfaces:
Tasklet, StepExecutionListener, StepListener, InitializingBean

public class SystemCommandTasklet
extends StepExecutionListenerSupport
implements Tasklet, 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

Field Summary
protected static Log logger
           
 
Constructor Summary
SystemCommandTasklet()
           
 
Method Summary
 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(TaskExecutor taskExecutor)
          Sets the task executor that will be used to execute the system command NB!
 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)
           
 
Methods inherited from class org.springframework.batch.core.listener.StepExecutionListenerSupport
afterStep
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected static final Log logger
Constructor Detail

SystemCommandTasklet

public SystemCommandTasklet()
Method Detail

execute

public RepeatStatus execute(StepContribution contribution,
                            ChunkContext chunkContext)
                     throws Exception
Execute system command and map its exit code to ExitStatus using SystemProcessExitCodeMapper.

Specified by:
execute in interface Tasklet
Parameters:
contribution - mutable state to be passed back to update the current step execution
chunkContext - attributes shared between invocations but not between restarts
Returns:
an RepeatStatus indicating whether processing is continuable.
Throws:
Exception

setCommand

public void setCommand(String command)
Parameters:
command - command to be executed in a separate system process

setEnvironmentParams

public void setEnvironmentParams(String[] envp)
Parameters:
envp - environment parameter values, inherited from parent process when not set (or set to null).

setWorkingDirectory

public void setWorkingDirectory(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 Exception
Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
Exception

setSystemProcessExitCodeMapper

public void setSystemProcessExitCodeMapper(SystemProcessExitCodeMapper systemProcessExitCodeMapper)
Parameters:
systemProcessExitCodeMapper - maps system process return value to ExitStatus 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 to StepExecution for interrupt checks during system command execution.

Specified by:
beforeStep in interface StepExecutionListener
Overrides:
beforeStep in class StepExecutionListenerSupport

setTaskExecutor

public void setTaskExecutor(TaskExecutor taskExecutor)
Sets the task executor that will be used to execute the system command NB! Avoid using a synchronous task executor


setInterruptOnCancel

public 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. false by default



Copyright © 2013 SpringSource. All Rights Reserved.