Executing a system command
A step can consist of just an execution of a system command. Spring Batch provides a convenient class for this, SystemCommandTasklet
.
Getting ready
We'll use the job defined in the Creating a job recipe.
How to do it…
In Spring Batch's configuration file, add a SystemCommandTasklet
bean. Declare the system command to be executed (here, we used the touch Unix command to create an empty file), the directory to execute it from, and the maximum time allowed for its execution:
@Bean public SystemCommandTasklet task1() { SystemCommandTasklet tasklet = new SystemCommandTasklet(); tasklet.setCommand("touch test.txt"); tasklet.setWorkingDirectory("/home/merlin"); tasklet.setTimeout(5000); return tasklet; }
How it works…
The SystemCommandTasklet
class will execute a command from the working directory and kill the process if it exceeds the timeout value.
There's more…
For a more advanced use of system commands (for example, to get the output of the system command) extend...