前言
这段时间上海名媛群成为了大家茶余饭后的谈资, 什么花了 500 元卧底上海名媛群, 本来以为能见识到纸醉金迷的上海人上人生活, 结果换来了一堆拼高端酒店, 拼下午茶, 甚至拼二手丝袜的...... 但是仍然瞧不起开宝马的一堆名媛, 今天用这个热门事件作为 Spring Batch 的入门例子
Spring Batch 介绍
Spring Batch 是一种批处理框架, 不是定时任务触发框架, 比如使用 @Scheduled, Quartz, ElasticJob 等框架都能定时触发任务, 但是任务的内容编写除了写普通的增删改查的 service, 还可以使用 Spring Batch 来编写, 主要优势如下:
1. 模型清晰, job 分为 step, step 分为 reader, processor, writer, 增加了不同的 job 之间输入/处理/输出能力的复用性
2. 任务运行情况和统计数据清楚, 每次 job 的入参, 运行时间, 读取(成功/失败)处理(失败/过滤)写入(成功/失败)持久化表中, 便于排查问题
3. 配置性强(跳过策略/重试策略,自定义监听器)容
错性强, 处理失败的数据可以通过观察者模式发送邮件/输出日志记录等
官方文档: https://spring.io/projects/spring-batch
Spring Batch 入门例子假设我们有一些拼单的商品(Commodity), 一些拼单的名媛(Girl), 我们需要批量读取这些商品, 把这些名媛和商品关联起来生成订单(GoodsOrder)并打印, 让我们看看这个过程应该怎么用 Spring Batch 实现吧!
定义 Reader@Data
publicclassListReader<T>implementsItemReader<T>{
privateList<T>sourceList;
privateintindex=0;
@Override
publicTread()throwsException{
returnsourceList.size()>index?sourceList.get(index ):null;
}
}
定义 Processor
@Component
publicclassGirlProcessorimplementsItemProcessor<Commodity,GoodsOrder>{
@Override
publicGoodsOrderprocess(Commoditycommodity)throwsException{
System.out.println(commodity);
finalArrayList<Girl>girlList=newArrayList<>();
for(inti=0;i<commodity.getBuyerCount();i ){
girlList.add(newGirl("girl" i));
}
returnnewGoodsOrder(commodity,girlList);
}
}
定义 Writer
@Component
publicclassPrintWriterimplementsItemWriter<Object>{
@Override
publicvoidwrite(List<?>list)throwsException{
list.stream().forEach(System.out::println);
}
}
拼装 Job
@Scheduled(cron="00/1***?")
publicvoiddemo1()throwsException{
System.out.println("jobstarting");
List<Commodity>commodityList=newArrayList<>();
for(inti=0;i<10;i ){
commodityList.add(newCommodity("酒店" i,i));
}
finalListReader<Commodity>reader=newListReader<>();
reader.setSourceList(commodityList);
finalJobgirlJob=jobBuilderFactory.get("demo1-girlsInShanghaiJob")
.flow(stepBuilderFactory.get("demo1-girlsInShanghaiStep")
.<Commodity,GoodsOrder>chunk(2).reader(reader)
.processor(girlProcessor).writer(printWriter).build())
.end().build();
jobLauncher.run(girlJob,newJobParametersBuilder()
.addDate("start_time",newDate()).toJobParameters());
}
输出:
Commodity(name=酒店0,buyerCount=0)
Commodity(name=酒店1,buyerCount=1)
GoodsOrder(commodity=Commodity(name=酒店0,buyerCount=0),buyerList=[])
GoodsOrder(commodity=Commodity(name=酒店1,buyerCount=1),buyerList=[Girl(name=girl0)])
Commodity(name=酒店2,buyerCount=2)
Commodity(name=酒店3,buyerCount=3)
GoodsOrder(commodity=Commodity(name=酒店2,buyerCount=2),buyerList=[Girl(name=girl0),Girl(name=girl1)])
GoodsOrder(commodity=Commodity(name=酒店3,buyerCount=3),buyerList=[Girl(name=girl0),Girl(name=girl1),Girl(name=girl2)])
Commodity(name=酒店4,buyerCount=4)
Commodity(name=酒店5,buyerCount=5)
GoodsOrder(commodity=Commodity(name=酒店4,buyerCount=4),buyerList=[Girl(name=girl0),Girl(name=girl1),Girl(name=girl2),Girl(name=girl3)])
GoodsOrder(commodity=Commodity(name=酒店5,buyerCount=5),buyerList=[Girl(name=girl0),Girl(name=girl1),Girl(name=girl2),Girl(name=girl3),Girl(name=girl4)])
Commodity(name=酒店6,buyerCount=6)
Commodity(name=酒店7,buyerCount=7)
GoodsOrder(commodity=Commodity(name=酒店6,buyerCount=6),buyerList=[Girl(name=girl0),Girl(name=girl1),Girl(name=girl2),Girl(name=girl3),Girl(name=girl4),Girl(name=girl5)])
GoodsOrder(commodity=Commodity(name=酒店7,buyerCount=7),buyerList=[Girl(name=girl0),Girl(name=girl1),Girl(name=girl2),Girl(name=girl3),Girl(name=girl4),Girl(name=girl5),Girl(name=girl6)])
Commodity(name=酒店8,buyerCount=8)
Commodity(name=酒店9,buyerCount=9)
GoodsOrder(commodity=Commodity(name=酒店8,buyerCount=8),buyerList=[Girl(name=girl0),Girl(name=girl1),Girl(name=girl2),Girl(name=girl3),Girl(name=girl4),Girl(name=girl5),Girl(name=girl6),Girl(name=girl7)])
GoodsOrder(commodity=Commodity(name=酒店9,buyerCount=9),buyerList=[Girl(name=girl0),Girl(name=girl1),Girl(name=girl2),Girl(name=girl3),Girl(name=girl4),Girl(name=girl5),Girl(name=girl6),Girl(name=girl7),Girl(name=girl8)])
附录
可以运行的项目代码地址: https://github.com/currentperson/spring-codog
本系列文章地址: https://github.com/currentperson/spring-study-notbook
设计模式系列文章地址: https://github.com/currentperson/codog-java-design-pattern
设计模式项目代码地址: https://github.com/currentperson/CodogDesignPattern
Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved