Book Image


Book Image


Overview of this book

Table of Contents (19 chapters)
Spring Cookbook
About the Author
About the Reviewers

Creating a job

We'll create a job that will simply execute some Java code. It will be a job with only one step. The step will be a Tasklet object (a single task, as opposed to a read-process-write step, which we'll cover later). We will execute this job in two different ways in the next two recipes.

How to do it…

Create a Tasklet class, which you will use to define a step and the job:

  1. Create the Task1 class implementing Tasklet:

    public class Task1 implements Tasklet {    
  2. In the Task1 class, add an execute() method with the code to be executed for the job:

    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext)
            throws Exception {
        System.out.println("Starting job..");
        // ... your code
        System.out.println("Job done..");
        return RepeatStatus.FINISHED;
  3. In the configuration class, add an autowired JobBuilderFactory attribute and an autowired StepBuilderFactory attribute:

    private JobBuilderFactory jobs;
    private StepBuilderFactory...