Spring Batch批处理框架使用解析
作者:二刀 时间:2021-12-24 03:41:19
这篇文章主要介绍了Spring Batch批处理框架使用解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
使用Spring Batch做为批处理框架,可以完成常规的数据量不是特别大的离线计算。
现在写一个简单的入门版示例。
这里默认大家已经掌握了Spring Batch的基本知识,示例只是为了快速上手实践
目标1:程序随机生成字符串,经过Spring Batch后,统一在字符串后加入“----PROCESSED”,并输出
目标2:程序读取txt文件,经过Spring Batch后,统一加入如上字段,并输出
Spring Batch的流程
读取数据----itemReader
处理数据----itemProcess
数据写入----itemWrite
分析目标可知,两个目标的输入数据源不同,处理方式基本一致,数据完成后的写入规则一致
由此可以分段完成代码
itemReader
目标一
这里没有使用Spring Batch自带的集中reader,所以自定义了随机生成字符串的reader
这里代码并不完善,reader会无线循环生成随机字符串,但不影响本次学习的目的
public class MyItemReader implements ItemReader<String> {
@Override
public String read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
return RandomStringUtils.randomAlphabetic(10);
}
}
目标二
由于是读取文件中的内容,所以不用自定义reader实现,可直接使用FlatFileItemReader,在Batch的config中配置即可
@Bean
public ItemReader<String> textReader(){
FlatFileItemReader<String> reader=new FlatFileItemReader<>();
File file = new File("D:\\FTP\\ttest.txt");
reader.setResource(new FileSystemResource(file));
reader.setLineMapper(new LineMapper<String>() {
@Override
public String mapLine(String line, int lineNumber) throws Exception {
return line;
}
});
return reader;
}
itemProcess
这里采用同一种处理方式即可
public class MyItemProcessor implements ItemProcessor<String,String> {
@Override
public String process(String s) throws Exception {
return s+"---------PROCESSED";
}
}
itemWriter
也采用同一种即可
public class MyItemWriter implements ItemWriter<String> {
@Override
public void write(List<? extends String> items) throws Exception {
for (String item : items) {
System.out.println(item);
}
}
}
配置完成Batch Config
@Configuration
@EnableBatchProcessing
public class BatchConfiguration extends DefaultBatchConfigurer {
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Bean
public MyItemProcessor processor(){
return new MyItemProcessor();
}
@Bean
public ItemWriter<String> writer(){
return new MyItemWriter();
}
@Bean
public ItemReader<String> textReader(){
FlatFileItemReader<String> reader=new FlatFileItemReader<>();
File file = new File("D:\\FTP\\ttest.txt");
reader.setResource(new FileSystemResource(file));
reader.setLineMapper(new LineMapper<String>() {
@Override
public String mapLine(String line, int lineNumber) throws Exception {
return line;
}
});
return reader;
}
@Bean
public ItemReader<String> stringReader(){
return new MyItemReader();
}
@Override
public void setDataSource(DataSource dataSource) {
super.setDataSource(dataSource);
}
@Bean
public Step myStep(){
return stepBuilderFactory
.get("step1")
//这个chunk size是最后调用写入的时候,一次性写入多少条已处理的数据
.<String,String>chunk(10)
// .reader(textReader())
.reader(stringReader())
.processor(processor())
.writer(writer())
.build();
}
@Bean
public Job MyJob(){
return jobBuilderFactory
.get("MyJOB")
.listener(new JobExecutionListenerSupport(){
//所有处理结束后调用
@Override
public void afterJob(JobExecution jobExecution) {
if(jobExecution.getStatus() == BatchStatus.COMPLETED){
System.out.println("OK");
}
}
})
.flow(myStep())
.end()
.build();
}
}
结束
最后直接运行spring boot程序即可
来源:https://www.cnblogs.com/tilv37/p/12019652.html
标签:Spring,Batch,批处理,框架
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
spring cloud将spring boot服务注册到Eureka Server上的方法
2023-12-08 19:42:09
![](https://img.aspxhome.com/file/2023/7/60837_0s.png)
Intellij搭建springmvc常见问题解决方案
2023-07-23 12:53:29
![](https://img.aspxhome.com/file/2023/9/57419_0s.png)
IntelliJ IDEA 2017 汉化包及图文教程
2023-11-25 12:17:58
![](https://img.aspxhome.com/file/2023/0/59570_0s.jpg)
CentOS 7系统下配置自定义JDK的教程
2022-02-27 13:46:26
Windows下Java环境变量配置详解
2022-02-04 05:56:02
SpringBoot执行定时任务@Scheduled的方法
2022-08-13 03:43:31
Java集合Map的clear与new Map区别详解
2022-03-21 15:41:39
![](https://img.aspxhome.com/file/2023/1/63321_0s.png)
Java编程实现帕斯卡三角形代码示例
2023-11-02 08:08:24
![](https://img.aspxhome.com/file/2023/3/58303_0s.gif)
Spring创建Bean的生命周期详析
2022-01-27 06:33:26
![](https://img.aspxhome.com/file/2023/8/71178_0s.png)
C++实现的链表类实例
2023-07-04 08:36:36
java编程之基于SpringBoot框架实现扫码登录
2023-02-14 02:39:28
![](https://img.aspxhome.com/file/2023/9/62999_0s.png)
Android消息机制Handler的工作过程详解
2023-07-31 13:49:03
![](https://img.aspxhome.com/file/2023/5/85965_0s.jpg)
SpringBoot过滤器与拦截 器深入分析实现方法
2023-11-28 23:04:15
SpringBoot常用数据库开发技术汇总介绍
2023-11-11 09:39:22
Java httpClient介绍以及使用示例
2023-03-15 23:51:15
Java实现map转换成json的方法详解
2022-09-09 12:26:03
Servlet3.0学习总结之基于Servlet3.0的文件上传实例
2023-09-02 01:51:33
![](https://img.aspxhome.com/file/2023/1/58141_0s.gif)
C# Winform消息通知之系统本地通知local toast notification
2023-02-01 04:14:02
![](https://img.aspxhome.com/file/2023/0/68610_0s.jpg)
SpringBoot实现本地存储文件上传及提供HTTP访问服务的方法
2022-09-14 19:09:12
![](https://img.aspxhome.com/file/2023/9/63749_0s.png)
Spring自动配置之condition条件判断下篇
2023-10-15 10:04:59
![](https://img.aspxhome.com/file/2023/0/62060_0s.png)