当前位置: 首页 > news >正文

spring batch处理数据模板(Reader-Processor-Writer模式)

步骤监听器

@Component
public class StepListener implements StepExecutionListener {private StepExecution stepExecution;public StepExecution getStepExecution() {return this.stepExecution;}@Overridepublic void beforeStep(StepExecution stepExecution) {this.stepExecution = stepExecution;}@Overridepublic ExitStatus afterStep(StepExecution stepExecution) {return null;}
}

@Component
@StepScope
public class ProductItemReader implements ItemReader<Product> {private Iterator<Product> iterator;private final StepListener stepListener;private final XxxRepository xxxRepository;public ProductItemReader(StepListener stepListener,XxxRepository xxxRepository) {this.stepListener = stepListener;this.xxxRepository = xxxRepository;}@Overridepublic Product read() {// 利用repository读取数据this.iterator = xxxRepository.xxx();// StepExecution 在 Step 执行时才可用,必须懒初始化StepExecution stepExecution = stepListener.getStepExecution();ExecutionContext executionContext = stepExecution.getExecutionContext();// 可设置STEP内上下文return iterator.hasNext() ? iterator.next() : null;}
}

处理

@Slf4j
@StepScope
@Component("productProcessor")
public class ProductProcessor implements ItemProcessor<Product, Product> {private final StepListener stepListener;public ProductProcessor(@Qualifier("stepListener") StepListener stepListener) {this.stepListener = stepListener;}@Overridepublic Product process(Product product) {// 从步骤域内读取数据或设置数据StepExecution stepExecution = productStepListener.getStepExecution();ExecutionContext executionContext = stepExecution.getExecutionContext();return product;}}

@Slf4j
@Component("productWriter")
public class ProductWriter implements ItemWriter<Product> {private final XxxRepository repository;private final StepListener stepListener;public ProductWriter(@Qualifier(value = "productRepository") XxxRepository xxxRepository,@Qualifier(value = "stepListener") StepListener stepListener) {this.repository = xxxRepository;this.stepListener = stepListener;}@Overridepublic void write(List<? extends Product> items){StepExecution stepExecution = this.stepListener.getStepExecution();ExecutionContext executionContext = stepExecution.getExecutionContext();items.forEach( product ->  {...});}
}

定义步骤

@Bean
public Step printStep(){TaskletStep taskletStep = stepBuilderFactory.get("testStep").listener(stepListener).<Product, Product>chunk(10).reader(productItemReader).processor(productProcessor).writer(productWriter).build();return taskletStep;
}

使用步骤

@Bean
public Job testJob(){return jobBuilderFactory.get("productJob").start(printStep())....build();
}
http://www.lryc.cn/news/609337.html

相关文章:

  • Timer实现定时调度的原理是什么?
  • PPT 转高精度 PDF API 接口
  • 使用DrissionPage实现xhs笔记自动翻页并爬取笔记视频、图片
  • Coin Combinations I(Dynamic Programming)
  • Docker环境离线安装指南
  • 解剖 .NET 经典:从 Component 到 BackgroundWorker
  • node.js常用函数
  • GaussDB case when的用法
  • SpringBoot AI自动化测试实战案例
  • GitCode疑难问题诊疗
  • Linux命令基础(下)
  • 1.内核模块
  • 14.Redis 哨兵 Sentinel
  • 2. 字符设备驱动
  • IO流-对象流
  • 克罗均线策略思路
  • `npm error code CERT_HAS_EXPIRED‘ 问题
  • Java Stream API 编程实战
  • 2025年渗透测试面试题总结-2025年HW(护网面试) 77-1(题目+回答)
  • 《测试驱动的React开发:从单元验证到集成协同的深度实践》
  • 【2025ICCV-目标检测方向】WaveMamba:用于 RGB-红外目标检测的小波驱动曼巴融合
  • 百度招黑产溯源安全工程师
  • SQL注入SQLi-LABS 靶场less31-38详细通关攻略
  • Maxscript在选择的可编辑多边形每个面上绘制一个内部圆形
  • 【高等数学】第七章 微分方程——第十节 常系数线性微分方程组解法举例
  • [硬件电路-140]:模拟电路 - 信号处理电路 - 锁定放大器概述、工作原理、常见芯片、管脚定义
  • 类与对象(中),咕咕咕
  • Zama的使命
  • 零确认双花攻击
  • 8月3日星期日今日早报简报微语报早读