Spring Batch应用简介
内容分为三部分:内容介绍,技术特点,开发示例
Spring Batch Job\Step\Flow示例:
package com.itlyf.springbatch.config;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
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.job.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableBatchProcessing
public class JobFlowDemo2 {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
//创建JOB
@Bean
public Job flowDemoJob() {
return jobBuilderFactory.get("flowDemoJob")
.start(flowDemoflow())
.next(jobFlowstep3())
.end()
.build();
}
//创建FLOW
@Bean
public Flow flowDemoflow() {
return new FlowBuilder<Flow>("flowDemoflow")
.start(jobFlowstep1())
.next(jobFlowstep2())
.build();
}
@Bean
public Step jobFlowstep1() {
return stepBuilderFactory.get("jobFlowstep1")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution arg0, ChunkContext arg1) throws Exception {
System.out.println("jobFlowstep1");
return RepeatStatus.FINISHED;
}
}).build();
}
public Step jobFlowstep2() {
return stepBuilderFactory.get("jobFlowstep2")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution arg0, ChunkContext arg1) throws Exception {
System.out.println("jobFlowstep2");
return RepeatStatus.FINISHED;
}
}).build();
}
public Step jobFlowstep3() {
return stepBuilderFactory.get("jobFlowstep3")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution arg0, ChunkContext arg1) throws Exception {
System.out.println("step3");
return RepeatStatus.FINISHED;
}
}).build();
}
}
Spring Batch读取数据示例:
package com.itlyf.springbatch.itermdb;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
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.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.JdbcPagingItemReader;
import org.springframework.batch.item.database.Order;
import org.springframework.batch.item.database.support.MySqlPagingQueryProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.RowMapper;
@Configuration
@EnableBatchProcessing
public class ItermReaderDbDemo {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private DataSource datasource;
@Autowired
@Qualifier("itermdbwrite")
private ItemWriter<? super User> itermdbwrite;
//创建JOB
@Bean
public Job itermDbDemoJob() {
return jobBuilderFactory.get("itermDbDemoJob")
.start(itermDbDemoStep())
.build();
}
//创建STEP
@Bean
public Step itermDbDemoStep() {
return stepBuilderFactory.get("listenerstep1")
.<User,User>chunk(2) //read process write
.faultTolerant()
.reader(itermdbread())
.writer(itermdbwrite)
.build();
}
@Bean
@StepScope
public JdbcPagingItemReader<User> itermdbread() {
JdbcPagingItemReader<User> reader = new JdbcPagingItemReader<User>();
reader.setDataSource(datasource);
reader.setFetchSize(2);
//读取倒的记录转换User对象
reader.setRowMapper(new RowMapper<User>() {
@Override
public User mapRow(ResultSet dbresult, int rownum) throws SQLException {
User user = new User();
user.setId(dbresult.getInt(1));
user.setUsername(dbresult.getString(2));
user.setPassword(dbresult.getString(3));
return user;
}
});
//SQL
MySqlPagingQueryProvider provider = new MySqlPagingQueryProvider();
provider.setSelectClause("id,username,password");
provider.setFromClause("from user");
//指定字段排序
Map<String,Order> lyfsort = new HashMap<>(1);
lyfsort.put("id",Order.DESCENDING);
provider.setSortKeys(lyfsort);
reader.setQueryProvider(provider);
return reader;
}
}
Spring Batch写数据示例:
package com.itlyf.springbatch.itemwriter;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.batch.item.file.transform.LineAggregator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@Configuration
public class ItemWriterFileConfig {
@Bean
public FlatFileItemWriter<Cust> itemfilewirter() throws Exception{
FlatFileItemWriter<Cust> fileItemWriter = new FlatFileItemWriter<>();
String path = "C:\\custfile.txt";
fileItemWriter.setResource(new FileSystemResource(path));
//把cust对象转换成字符串
fileItemWriter.setLineAggregator(new LineAggregator<Cust>() {
@Override
public String aggregate(Cust item) {
ObjectMapper mapper = new ObjectMapper();
String str = null;
//转换为JSON字符串
try {
str=mapper.writeValueAsString(item);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return str;
}
});
fileItemWriter.afterPropertiesSet();
return fileItemWriter;
}
}
推荐博文: https://blog.csdn.net/topdeveloperr/article/details/84337956
https://blog.csdn.net/cxy_chh/article/details/92062259
Spring Batch API查询: https://docs.spring.io/spring-batch/docs/4.2.x/api/index.html