Java Batch Processing Tutorial 🎯

beginner
11 min

Java Batch Processing Tutorial 🎯

Welcome to our comprehensive guide on Java Batch Processing! In this lesson, we'll dive deep into the world of batch processing, a crucial aspect of Java programming that helps automate repetitive tasks. This lesson is perfect for beginners and intermediates looking to expand their Java skills.

Understanding Batch Processing 📝

Batch processing is a method of handling jobs in computer systems. It involves a system that processes a set of jobs, one at a time, without requiring user intervention during the processing of each job.

In Java, we can use the java.batch package to create batch applications.

Setting Up a Batch Application 💡

To start, let's set up a simple batch application:

  1. Create a new Maven project with the following archetype: maven-archetype-quickstart
  2. Add the following dependencies to your pom.xml file:
xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-batch</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
  1. Replace the content of your src/main/java/com.example.app.App.java file with the following code:
java
package com.example.app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }

Creating a Batch Job 💡

Now, let's create our first batch job.

  1. Create a new package named batch under src/main/java.
  2. Inside the batch package, create a new class called HelloWorldJob:
java
package com.example.app.batch; import org.springframework.batch.core.Job; import org.springframework.batch.core.Step; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; import org.springframework.batch.core.configuration.annotation.StepScope; import org.springframework.batch.core.launch.support.RunIdIncrementer; import org.springframework.batch.item.file.FlatFileItemReader; import org.springframework.batch.item.file.LineMapper; import org.springframework.batch.item.file.mapping.DefaultLineMapper; import org.springframework.batch.item.file.transform.DelimitedLineTokenizer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.nio.charset.StandardCharsets; @Configuration public class HelloWorldJobConfig { @Autowired private JobBuilderFactory jobBuilderFactory; @Autowired private StepBuilderFactory stepBuilderFactory; @Bean public Job helloWorldJob() { return jobBuilderFactory.get("helloWorldJob") .incrementer(new RunIdIncrementer()) .start(step1()) .next(step2()) .build(); } @Bean public Step step1() { return stepBuilderFactory.get("step1") .<String, String> chunk(10) .reader(reader()) .processor(processor()) .writer(writer()) .build(); } @Bean public Step step2() { return stepBuilderFactory.get("step2").build(); } @Bean @StepScope public FlatFileItemReader<String> reader() { FlatFileItemReader<String> reader = new FlatFileItemReader<>(); reader.setResource(org.springframework.core.io.ClassPathResource .getClassPathResource("input.csv")); reader.setLinesToSkip(1); reader.setLineMapper(lineMapper()); return reader; } @Bean public LineMapper<String> lineMapper() { DefaultLineMapper<String> lineMapper = new DefaultLineMapper<>(); DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer(); lineTokenizer.setDelimiter(","); lineMapper.setLineTokenizer(lineTokenizer); lineMapper.setFieldSetMapper(fieldSet -> new String(fieldSet.get("input").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8)); return lineMapper; } @Bean public StringWriter writer() { return new StringWriter(); } @Bean public StringProcessor processor() { return new StringProcessor(); } } class StringProcessor implements ItemProcessor<String, String> { @Override public String process(String item) throws Exception { // Process the item here and return the processed result return item.toUpperCase(); } } class StringWriter extends StringWriterBase implements ListProcessor<String> { @Override public void write(List<? extends String> items) throws IOException { for (String item : items) { super.write(item); super.write("\n"); } } }

This code creates a simple batch job that reads a CSV file, processes each line, and writes the results to another file.

Running the Batch Job 💡

To run the job, simply execute your main application:

bash
mvn spring-boot:run

Quiz

With this lesson, you now have a solid understanding of Java batch processing! Practice by creating more batch jobs and exploring the powerful capabilities of this feature. Happy coding! 🥳