Developing your first Spring Batch Job

Giuliana Bezerra
3 min readMar 18, 2020

--

The Spring Batch framework arose from the need to develop batch processing routines in a standardized way in the JVM. With that in mind, this article will help you develop your first Job Spring Batch with a step-by-step approach.

If you are not yet familiar with batch systems, I suggest reading this article.

Prerequisites

The following programs must be installed:

  1. JDK 8
  2. Maven
  3. Spring Tools Suite (STS)

Goals

At the end of this tutorial you will be able to create Jobs Spring Batch that perform simple tasks.

It is not in the scope of this post to implement more complex Jobs, with chunk-based steps.

Step 1: Project Creation

Open your STS and select the menu File > New > Spring Starter Project:

Fill in the project data as shown in the figure above and press Next>:

This is the stage for selecting the project’s dependencies. By default, a Job Spring Batch needs at least the Spring Batch dependency and some database driver to save its metadata. For simplicity, we will use the H2 Database dependency, which is an in-memory database. Select Finish to generate the project:

Step 2: Job Creation

Create a classe called BatchConfig:

In this class, we will add the job in fact. To do this, add the following anotations:

  1. @Configuration: This annotation will inform that the class will configure Beans for Spring (our job).
  2. @EnableBatchProcessing: This annotation allows us to use the Spring Batch framework components in the project.

To build our job and its step, we will use Spring Batch builders that will be injected into our configuration class:

The Job will be registered as a Spring Bean through a method that will use the job builder that we have injected:

In the builder we have informed the name of the job and which step is part of it. Let’s create the method that sets up the step using the step builder:

The step above is tasklet-based, which is used to implement simple tasks. The task is executed until the status returned by it is FINISHED. When this happens, the Job is terminated.

Step 3: Job Execution

Run the FirstSpringBatchJobApplication class as a Spring Boot Application (shortcut F11):

The message that we have configured in the tasklet was printed successfully.

Conclusion

In this article we have created a simple Job step by step. To go deeper, I suggest reading the official documentation, which has more complex components that can be incorporated into your Job.

I also have a Youtube channel where I talk about software development, be sure to check it out!

--

--

Giuliana Bezerra
Giuliana Bezerra

Written by Giuliana Bezerra

Solution Architect — Online Instructor — Youtuber at @giulianabezerra — Writer

No responses yet