Posted in

How to create a Spring Batch job?

Hey there! As a supplier of Spring-related tech stuff, I get asked a lot about how to create a Spring Batch job. It’s a pretty cool feature in the Spring ecosystem that can handle large volumes of data processing, and today, I’m gonna walk you through the process. Spring

What’s Spring Batch Anyway?

Before we jump into creating a job, let me quickly explain what Spring Batch is. Spring Batch is an open-source framework that helps you build batch processing applications. Batch processing means handling a large number of records in one go, like processing thousands of transactions at the end of the day. It’s super useful in industries like finance, e-commerce, and healthcare where you gotta deal with tons of data regularly.

Setting Up the Project

First things first, you need to set up a project. The easiest way to do this is by using Spring Initializr. It’s an online tool that helps you generate a basic Spring Boot project with all the necessary dependencies.

Go to the Spring Initializr website. You’ll see a form where you can configure your project. Choose the project type, like Maven or Gradle. I usually go with Maven because it’s easy to manage dependencies. Then, select the Spring Boot version. For most cases, the latest stable version works just fine.

Under the "Dependencies" section, you gotta add a few important ones. Search for "Spring Batch" and add it to your project. You’ll also need "Spring Boot DevTools" which helps with development by automatically restarting your application when you make changes. And if you’re gonna work with databases, add the appropriate database driver, like "MySQL Driver" if you’re using MySQL.

Once you’ve configured everything, click the "Generate" button. It’ll download a ZIP file with your project structure. Extract it to a folder on your computer, and open it in your favorite IDE, like IntelliJ IDEA or Eclipse.

Defining the Job

Now, let’s start creating the Spring Batch job. In Spring Batch, a job is made up of steps, and each step has a reader, a processor, and a writer.

Creating the Job Configuration

First, you need to create a configuration class for your job. In your Java project, create a new class, let’s say BatchJobConfig. Annotate it with @Configuration and @EnableBatchProcessing. The @Configuration annotation tells Spring that this is a configuration class, and @EnableBatchProcessing enables the Spring Batch features.

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableBatchProcessing
public class BatchJobConfig {
    // Job configuration code will go here
}

Defining the Job

Inside the BatchJobConfig class, you need to define the job. You can do this by creating a method that returns a Job object. Use the JobBuilderFactory to build the job.

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobBuilderFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

@Autowired
private JobBuilderFactory jobBuilderFactory;

@Bean
public Job myJob() {
    return jobBuilderFactory.get("myJob")
           .start(myStep())
           .build();
}

Here, myJob is the name of the job, and myStep is the step that the job will execute. We’ll define the step next.

Creating the Step

A step is the building block of a job. It consists of a reader, a processor, and a writer.

Creating the Reader

The reader is responsible for reading data from a source. It could be a database, a file, or an API. Let’s say we’re reading data from a CSV file. You can use the FlatFileItemReader to read the CSV file.

import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.core.io.ClassPathResource;

@Bean
public FlatFileItemReader<MyData> reader() {
    FlatFileItemReader<MyData> reader = new FlatFileItemReader<>();
    reader.setResource(new ClassPathResource("data.csv"));
    reader.setLineMapper(new DefaultLineMapper<MyData>() {{
        setLineTokenizer(new DelimitedLineTokenizer() {{
            setNames(new String[]{"field1", "field2", "field3"});
        }});
        setFieldSetMapper(new BeanWrapperFieldSetMapper<MyData>() {{
            setTargetType(MyData.class);
        }});
    }});
    return reader;
}

Here, MyData is a Java class that represents the data in the CSV file. You need to create a class with fields corresponding to the columns in the CSV file.

Creating the Processor

The processor is used to process the data read by the reader. It can perform operations like data validation, transformation, or enrichment. Let’s say we’re just adding a new field to the data.

import org.springframework.batch.item.ItemProcessor;

public class MyProcessor implements ItemProcessor<MyData, MyData> {
    @Override
    public MyData process(MyData item) throws Exception {
        item.setNewField("Processed");
        return item;
    }
}

Creating the Writer

The writer is responsible for writing the processed data to a destination. It could be a database, a file, or an API. Let’s say we’re writing the data to a database. You can use the JdbcBatchItemWriter to write the data to a database.

import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

@Autowired
private NamedParameterJdbcTemplate jdbcTemplate;

@Bean
public JdbcBatchItemWriter<MyData> writer() {
    JdbcBatchItemWriter<MyData> writer = new JdbcBatchItemWriter<>();
    writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
    writer.setSql("INSERT INTO my_table (field1, field2, field3, new_field) VALUES (:field1, :field2, :field3, :newField)");
    writer.setJdbcTemplate(jdbcTemplate);
    return writer;
}

Defining the Step

Now that we have the reader, processor, and writer, we can define the step.

import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepBuilderFactory;
import org.springframework.beans.factory.annotation.Autowired;

@Autowired
private StepBuilderFactory stepBuilderFactory;

@Bean
public Step myStep() {
    return stepBuilderFactory.get("myStep")
           .<MyData, MyData>chunk(10)
           .reader(reader())
           .processor(new MyProcessor())
           .writer(writer())
           .build();
}

Here, chunk(10) means that the step will process 10 items at a time.

Running the Job

To run the job, you can create a main class and use the JobLauncher to launch the job.

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BatchApplication implements CommandLineRunner {

    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    private Job myJob;

    public static void main(String[] args) {
        SpringApplication.run(BatchApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        JobParameters jobParameters = new JobParameters();
        JobExecution execution = jobLauncher.run(myJob, jobParameters);
        System.out.println("Job finished with status: " + execution.getStatus());
    }
}

Monitoring and Error Handling

Spring Batch provides built-in monitoring and error handling features. You can use the JobExecution object to get information about the job execution, like the start time, end time, and status.

For error handling, you can use the RetryTemplate to retry failed operations. You can also use the SkipListener to handle skipped items.

Conclusion

Creating a Spring Batch job isn’t too hard once you get the hang of it. It’s a powerful tool for handling large volumes of data processing. As a Spring supplier, we’re here to help you implement Spring Batch in your projects. Whether you need help with the initial setup, optimizing your jobs, or integrating with other systems, we’ve got you covered.

Spring If you’re interested in using our Spring Batch solutions or have any questions about the process, don’t hesitate to reach out. We’re always happy to have a chat about how we can work together and make your batch processing tasks a breeze. Let’s start this exciting journey of efficient data processing with Spring Batch!

References

  • Spring Batch Documentation
  • Spring Boot Documentation
  • Java 8 Documentation

Xinxiang Fengda Machinery Co., Ltd.
We’re well-known as one of the leading spring manufacturers and suppliers in China, specialized in providing high quality customized service for global clients. We warmly welcome you to buy high-grade spring made in China here from our factory.
Address: No.16 Wangguanying Village, Kangcun Town, Huojia County, Xinxiang City, Henan Province, China
E-mail: xxfdjx@163.com
WebSite: https://www.flipflowscreen.com/