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 ...

Get Spring Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.