Spring Batch批处理框架使用解析
作者:二刀 发布时间:2021-12-24 03:41:19
标签:Spring,Batch,批处理,框架
这篇文章主要介绍了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
0
投稿
猜你喜欢
- WebMvcConfigurer配置类其实是Spring内部的一种配置方式,采用JavaBean的形式来代替传统的xml配置文件形式进行针对
- 线程池execute与submit区别在使用线程池的时候,看到execute()与submit()方法。都可以使用线程池执行一个任务,但是两
- 本文介绍了Java实现动态获取图片验证码的示例代码,分享给大家,具体如下:import javax.imageio.ImageIO;impo
- mybatis的原身是ibatis,现在已经脱离了apache基金会,新官网是http://www.mybatis.org/。在研究Myba
- 本文介绍了 SpringBoot之Controller的使用,分享给大家,具体如下:1.@Controller:处理http请求 2.@Re
- 本文实例讲述了Java Socket实现单线程通信的方法。分享给大家供大家参考,具体如下:现在做Java直接使用Socket的情况是越来越少
- 常量池中各数据项类型详解常量池中的数据项是通过索引来引用的, 常量池中的各个数据项之间也会相互引用。在这11中常量池数据项类型中, 有两种比
- 简介mutable(可变)和immutable(不可变)对象是我们在java程序编写的过程中经常会使用到的。可变类型对象就是说,对象在创建之
- 情况简介spring项目,controller异步调用service的方法,产生大量并发。具体业务:前台同时传入大量待翻译的单词,后台业务接
- 今天在接手别人的一个项目的时候遇到一个坑,坑死我了;是一个打包的问题,好不容易我把代码写完了准备打包测试了,结果java -jar xxx.
- 本文实例讲述了Android中TextView显示插入的图片实现方法。分享给大家供大家参考,具体如下:Android系统默认给TextVie
- Java xml出现错误 javax.xml.transform.TransformerException: java.lang.NullP
- 一、引入pom<?xml version="1.0" encoding="UTF-8"?>
- 简单的页面分析在上一个文章简单的数据库连接测试,已经测试和数据库做简单的交互,也就是dao层的实现,接下来要说的却是action的简单实现,
- android线程消息机制主要由Handler,Looper,Message和MessageQuene四个部分组成。平常在开发中,我们常用来
- 前言通过此篇文章,你将了解到:Flutter windows和Android桌面应用屏幕适配的解决方案;屏幕适配的相关知识和原理;flutt
- 背景:Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了
- 前言java 10 引进一种新的闪闪发光的特性叫做局部变量类型推断。听起来很高大上吧?它是什么呢? 下面的两个情景是我们作为 Java 开发
- 今天下了个新浪微博的API研究研究,目前实现了发布微博功能,包括带图片的微博。为了安全,新浪微博的API中并没有提供用微博帐号密码登录的功能
- 前言最近有项目需要开发档案打包下载功能,其中包含很多大附件,项目使用minio存储且不在同一台服务器上,为了优化速度决定使用windows共