Spring Batch tutorial – JavaDream

Home / Spring Batch tutorial – JavaDream

In this post we will see about spring batch. So the first question will come in our mind is

What is Spring Boot Batch ?

Spring boot batch is framework that is designed to execute large and robust batch application.

Batch application Example.

As we know we use spring boot batch to execute large task. So here are some application scenario where we use Batch framework.

1- Suppose we have a big CSV file which contains billions rerecords and i want to store that data in the MySQL database.

So if you try yo insert this billions record in one go your application takes a very very long time to execute this task.

And it may also be possible that your database not support this huge amount of insertion.

So in this case we will use Spring boot Batch framework and execute this task in batch chunks means we divide this task of insertion in chunks.

The above scenario we will cover in another application first we see some easy example to learn spring boot batch framework.

In this example we take a list of names and try to process this list data in batch.

Project Structure:

spring batch

There are some keyword in spring boot batch framework. That we use to process our application.

  1. ItemReader
  2. ItemProcessor
  3. ItemWriter
  4. Step
  5. Job
  6. Job Listener
  7. JobBuilderFactory
  8. StepBuilderFactory
  9. JobLauncher
ItemReader :

ItemReader is like entry point it will read your data from file or from the source you define.

ItemProcessor :

ItemProcessor is process the data that you read using ItemReader.

For Example you read a List of names using ItemReader and you want to convert this name in uppercase. So this task is done by ItemProcessor.

ItemWriter :

After Process the data by ItemProcessor we have to write this data. So for this we use ItemWriter.

Step :

After process all above steps ItemReader, ItemProcessor, ItemWriter we will get the steps.

Job :

Now we have to create a Job that executes the Step. Job is an interface and resides in org.springframework.batch.core package.

Job Listener :

This class deines two methods beforeJob and afterJob. As the name suggest this function is used to perform task before and after job execution.

JobBuilderFactory :

JobBuilderFactory is a class that used to create the Job.

StepBuilderFactory :

StepBuilderFactory is a class that used to create the Step.

JobLauncher :

Till now we have create the Job. Now we have to excute this Job for this we will use JobLauncher.

Now Lets do a coding example. We Just create a List of names and print this List name in batch.

To create above Project we have to follow below steps:

1- Add required dependency in your pom.xml file or add from Maven Repositry.

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-batch</artifactId>
</dependency>

2- Now create a class that implements ItemReader interface.

package com.vasu.springbatch;

import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;

public class Reader implements ItemReader<String> {

	private String[] names = {"Vasu","Vishu", "Ayush","Sushmita","konika"};
	private int count;
	
	@Override
	public String read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
		System.out.println("Inside Read Method");
		if(count < names.length) {
			return names[count++];
		}else {
			count = 0;
		}
		return null;
	}

}

3- Create a class that process above list and convert List names into UpperCase.

package com.vasu.springbatch;

import org.springframework.batch.item.ItemProcessor;

public class Processor implements ItemProcessor<String, String> {

	@Override
	public String process(String item) throws Exception {
		System.out.println("Inside Process method");
		return "PROCESSED "+ item.toUpperCase();
	}
}

4- Create Writer class that implement ItemWriter.

package com.vasu.springbatch;

import java.util.List;

import org.springframework.batch.item.ItemWriter;

public class Writer implements ItemWriter<String> {

	@Override
	public void write(List<? extends String> items) throws Exception {
		System.out.println("Inside Write method");
		System.out.println("Data In UpperCase is " + items);
	}
}

5- Create a Job Listener class .

package com.vasu.springbatch;

import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;

public class MyJobListener implements JobExecutionListener {

	@Override
	public void beforeJob(JobExecution jobExecution) {
		System.out.println("Before Job Started");
	}

	@Override
	public void afterJob(JobExecution jobExecution) {
		System.out.println("After Job Ended " + jobExecution.getStatus().toString());
	}
}

6- Now Create a Config class and initialize all the above beans.

package com.vasu.springbatch;

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.launch.support.RunIdIncrementer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BatchConfig {
	
	@Autowired
	private StepBuilderFactory sdf;
	
	@Autowired
	private JobBuilderFactory jbf;
	
	@Bean
	public Job job() {
		return jbf.get("myFirstBatchJob") 	
				.incrementer(new RunIdIncrementer())
				.listener(listener())
				.start(step())
				.build();
	}
	
	@Bean
	public Step step() {
		return sdf.get("myFirstBatchStep")
				.<String,String>chunk(3)
				.reader(reader())
				.processor(processor())
				.writer(writer())
				.build();
		
	}

	@Bean
	public Reader reader() {
		return new Reader();
	}

	@Bean
	public Writer writer() {
		return new Writer();
	}

	@Bean
	public Processor processor() {
		return new Processor();
	}

	@Bean
	public MyJobListener listener() {
		return new MyJobListener();
	}
}

7- Use @EnableBatchProcessing annotation in your main class.

package com.vasu.springbatch;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableBatchProcessing
public class SpringbatchApplication {

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

8- Create a Test Class to test the batch process. In this class autowired JobLauncher.

package com.vasu.springbatch;

import org.junit.jupiter.api.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringbatchApplicationTests {

	@Autowired
	JobLauncher launcher;

	@Autowired
	Job job;

	@Test
	void testBatch() throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException {
		JobParameters jobParameters = new JobParametersBuilder()
				.addLong("todayTime", System.currentTimeMillis())
				.toJobParameters();
		launcher.run(job, jobParameters);
	}

}

Now run this class as JUnit Test.

Output:

spring batch

Complete code on Github- Spring batch Github

You may also like

Spring boot security using JWT ( JSON WEB TOKENS ).

HTTPS in Spring Boot

Spring Boot with Apache kafka.

Profiles in Spring boot application

Help Others, Please Share

About Author

Leave a Reply

Your email address will not be published. Required fields are marked *