Spring Boot wholesale services

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> 
XML

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/resourcesadding 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) ); 
SQL

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; } } 
Java

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; } } 
Java

Create a Batch configuration file, read data from and write SQL CSV file, as shown below. We need to add the class configuration file @EnableBatchProcessingcomments. @EnableBatchProcessingComments 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() { JdbcBatchItemWriter<User> writer = new JdbcBatchItemWriter<User>(); writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<User>()); writer.setSql("INSERT INTO USERS (first_name, last_name) VALUES (:firstName, :lastName)"); writer.setDataSource(dataSource); return writer; } @Bean public Job importUserJob(JobCompletionNotificationListener listener) { return jobBuilderFactory.get("importUserJob").incrementer( new RunIdIncrementer()).listener(listener).flow(step1()).end().build(); } @Bean public Step step1() { return stepBuilderFactory.get("step1").<User, User>chunk(10).reader(reader()).processor(processor()).writer(writer()).build(); } } 
Java

reader()A method for reading data from the CSV file, and writer()a method for writing data to SQL.

Next, write a job completion notification listener class - for notification when the job is completed.

package com.yiibai.batchservicedemo;

import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.batch.core.BatchStatus; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.listener.JobExecutionListenerSupport; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Component; @Component public class JobCompletionNotificationListener extends JobExecutionListenerSupport { private static final Logger log = LoggerFactory.getLogger(JobCompletionNotificationListener.class); private final JdbcTemplate jdbcTemplate; @Autowired public JobCompletionNotificationListener(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @Override public void afterJob(JobExecution jobExecution) { if (jobExecution.getStatus() == BatchStatus.COMPLETED) { log.info("!!! JOB FINISHED !! It's time to verify the results!!"); List<User> results = jdbcTemplate.query( "SELECT first_name, last_name FROM USERS", new RowMapper<User>() { @Override public User mapRow(ResultSet rs, int row) throws SQLException { return new User(rs.getString(1), rs.getString(2)); } }); for (User person : results) { log.info("Found <" + person + "> in the database."); } } } } 
Java

Now, create an executable JAR file, and use the following command to run Gradle or Maven Spring Boot application.

For Maven, using a command such as -

mvn clean install
Shell

After "BUILD SUCCESS", in targetlocate the JAR file directory.

For Gradle, you can use the command shown in the following -

gradle clean build
Shell

After the "BUILD SUCCESSFUL", can build/libsfind the JAR file directory.

Use the command given here to run JAR files -

java –jar <JARFILE>
Shell

See job execution process output in the console window.


The following is the correct / supplement content:

It should be less in the database configuration in application.resources   Submitted: 2019-08-29

Guess you like

Origin www.cnblogs.com/borter/p/12423875.html