Batch service is a process to execute multiple commands in a single task. In this chapter, you will learn how to create a batch service in Spring Boot application.
Before starting, consider an example of how to save the CSV file contents to the HSQLDB.
To create a batch service program, you need to add Spring Boot Starter Batch HSQLDB dependencies and dependencies in build configuration file.
Maven users can pom.xml to add the following file dependencies.
<dependency>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-batch</artifactId> </dependency> <dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> </dependency>
Gradle users can build.gradle add the following file dependencies.
compile("org.springframework.boot:spring-boot-starter-batch")
compile("org.hsqldb:hsqldb")
Now, the resource directories - src/main/resources
adding the simple CSV data file, and the file name file.csv , as shown in FIG -
Max,Su
Da, Lee
Sanfeng, Zhang
Next, write a SQL script for the HSQLDB - in resource projects ( src/main/resources
) creates a file under the record: request_fail_hystrix_timeout
DROP TABLE USERS IF EXISTS; CREATE TABLE USERS ( user_id BIGINT IDENTITY NOT NULL PRIMARY KEY, first_name VARCHAR(20), last_name VARCHAR(20) );
Create a POJO class, as shown in FIG -
package com.yiibai.batchservicedemo;
public class User { private String lastName; private String firstName; public User() { } public User(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } @Override public String toString() { return "firstName: " + firstName + ", lastName: " + lastName; } }
Now, create an intermediate processor, and prior to writing data to the SQL execution operation after reading data from the CSV file.
package com.yiibai.batchservicedemo;
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.batch.item.ItemProcessor; public class UserItemProcessor implements ItemProcessor<User, User> { private static final Logger log = LoggerFactory.getLogger(UserItemProcessor.class); @Override public User process(final User user) throws Exception { final String firstName = user.getFirstName().toUpperCase(); final String lastName = user.getLastName().toUpperCase(); final User transformedPerson = new User(firstName, lastName); log.info("Converting (" + user + ") into (" + transformedPerson + ")"); return transformedPerson; } }
Create a Batch configuration file, read data from and write SQL CSV file, as shown below. We need to add the class configuration file @EnableBatchProcessing
comments. @EnableBatchProcessing
Comments are used to enable batch operations Spring Boot application.
package com.yiibai.batchservicedemo;
import javax.sql.DataSource; import org.springframework.batch.core.Job; import org.springframework.batch.core.Step; import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; 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.batch.item.database.BeanPropertyItemSqlParameterSourceProvider; import org.springframework.batch.item.database.JdbcBatchItemWriter; 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.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; @Configuration @EnableBatchProcessing public class BatchConfiguration { @Autowired public JobBuilderFactory jobBuilderFactory; @Autowired public StepBuilderFactory stepBuilderFactory; @Autowired public DataSource dataSource; @Bean public FlatFileItemReader<User> reader() { FlatFileItemReader<User> reader = new FlatFileItemReader<User>(); reader.setResource(new ClassPathResource("file.csv")); reader.setLineMapper(new DefaultLineMapper<User>() { { setLineTokenizer(new DelimitedLineTokenizer() { { setNames(new String[] { "firstName", "lastName" }); } }); setFieldSetMapper(new BeanWrapperFieldSetMapper<User>() { { setTargetType(User.class); } }); } }); return reader; } @Bean public UserItemProcessor processor() { return new UserItemProcessor(); } @Bean public JdbcBatchItemWriter<User> writer