Today, I show you how to create a Spring Batch Hello World Example. This post introduces very clear to help you understand then you can create your own example. You know what, Spring Batch provides reusable functions that are essential in processing large volumes of records, including logging/tracing, transaction management, job processing statistics, job restart, skip, and resource management. Let’s dive into
Spring Batch Hello World Example
Project Setup
Tools and framework that we use:
Spring Boot 2.2.2 RELEASE
Spring Batch 4.2.1
Maven 3.6
Java 8
Project Directory
Our project will have a structure like below
Project Dependencies
We’re using the dependencies like below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
... <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <java.version>8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-batch</artifactId> </dependency> </dependencies> ... |
Creating the Model
Here, we assume that we need to read employee information from CSV file named employees.csv, its format looks like below:
1 2 |
David, Pham, Java deparment James, Bon, PHP deparment |
This file is located in the project folder resources /csv/employees.csv
Then, we create a POJO class that its object map with data in the above CSV file
1 2 3 4 5 6 7 8 9 10 11 |
package com.javabycode.model; public class Employee { private String firstName; private String lastName; private String department; public Employee() { } // getter and setter } |
Configuring Spring Batch Jobs
We assume that we have a data CSV file that contains a list of employees, its format looks like below.
Create BatchConfig class and add the @EnableBatchProcessing annotation for this class to get the support of Spring Batch features.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.javabycode.batch; import javax.sql.DataSource; import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer; import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.context.annotation.Configuration; @Configuration @EnableBatchProcessing public class BatchConfig extends DefaultBatchConfigurer { @Override public void setDataSource(DataSource dataSource) { // initialize a Map based JobRepository by default } } |
Create Spring Batch job that named myJob
for our own Hello World application
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
package com.javabycode.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.item.file.FlatFileItemReader; import org.springframework.batch.item.file.FlatFileItemWriter; import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder; import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder; import org.springframework.batch.item.file.transform.PassThroughLineAggregator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.FileSystemResource; import com.javabycode.model.Employee; @Configuration public class HelloWorldJobConfig { @Bean public Job helloWorlJob(JobBuilderFactory jobBuilders, StepBuilderFactory stepBuilders) { return jobBuilders.get("myJob") .start(helloWorldStep(stepBuilders)).build(); } @Bean public Step helloWorldStep(StepBuilderFactory stepBuilders) { return stepBuilders.get("myStep") .<Employee, String>chunk(10).reader(reader()) .processor(processor()).writer(writer()).build(); } @Bean public FlatFileItemReader<Employee> reader() { return new FlatFileItemReaderBuilder<Employee>() .name("employeeItemReader") .resource(new ClassPathResource("csv/employees.csv")) .delimited().names(new String[] {"firstName", "lastName","department"}) .targetType(Employee.class).build(); } @Bean public EmployeeItemProcessor processor() { return new EmployeeItemProcessor(); } @Bean public FlatFileItemWriter<String> writer() { return new FlatFileItemWriterBuilder<String>() .name("greetingItemWriter") .resource(new FileSystemResource( "target/output.txt")) .lineAggregator(new PassThroughLineAggregator<>()).build(); } } |
Let’s dive into the above code:
The method helloWorldJob
returns an instance of Job
bean. That Job
bean may be likely understood is a container of steps and other configuration options.
The methodhelloWorldStep
returns an instance of Step
bean. This bean encapsulates phases of a batch job. Here, we have a step named myStep
that takes care of reading and writing the CSV file.
We use the FlatFileItemReader to read the CSV file, its instance is created by the FlatFileItemReaderBuilder implementation. In order to FlatFileItemReader processes your CSV file, you must provide the information of file format, e.g: java mapping class, matched fields.
The EmployeeItemProcessor
handles the processing of the data.
To write file we use FlatFileItemWriter that its instance created by a FlatFileItemWriterBuilder
builder implementation.
Processing the Data
In most cases, we need to make some data processing from our original data. So we can create EmployeeItemProcessor
that implements ItemProcessor
interface to take care of that task. Let’s have a look at below implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.javabycode.batch; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.batch.item.ItemProcessor; import com.javabycode.model.Employee; public class EmployeeItemProcessor implements ItemProcessor<Employee, String> { private static final Logger LOGGER = LoggerFactory.getLogger(EmployeeItemProcessor.class); @Override public String process(Employee employee) throws Exception { String greeting = "Hello " + employee.getFirstName() + " " + employee.getLastName() + " from " + employee.getDepartment()+"!"; LOGGER.info("converting '{}' into '{}'", employee, greeting); return greeting; } } |
Configuring SpringBootApplication class
To configure Spring Boot application we create simple class below. If you haven’t had knowledge about Spring Boot yet. Let’s checkout the post Spring Boot Tutorial for Beginners
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.javabycode; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; @SpringBootApplication( exclude = {DataSourceAutoConfiguration.class}) public class MySpringBootApplication { public static void main(String[] args) { SpringApplication.run(MySpringBootApplication.class, args); } } |
Running Application
To demo our Spring Batch Hello World Example, we run the below command in the console.
1 |
mvn spring-boot:run |
Note: You must be in your project directory before run that command.
While running that command, you will see the output like below
That’s all about Spring Batch Hello World Example.
If you find this post useful, don't hesitate to share it with your friends or other people for growing knowledge together and is an effort to contribute us.
References
Spring Boot Tutorial for Beginners
Spring Batch Frameworks