当前位置: 首页 > 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.dtcms.com/a/313028.html

相关文章:

  • 【Mysql】日志--错误日志、二进制日志、查询日志、慢查询日志
  • Timer实现定时调度的原理是什么?
  • Python开发环境PyCharm下载与安装
  • RSA 解密逻辑
  • Spring lookup-method实现原理深度解析
  • 悬挂的绳子,它的函数方程是什么样子的?
  • 嵌入式学习日志——数据结构(一)
  • RAG与智能体技术全景解析:架构革新、场景落地与未来趋势
  • 【前端:Html】--1.2.基础语法
  • Redis面试精讲 Day 10:Redis数据结构底层实现原理
  • RK3568 AB分区+OTA升级(Linux)
  • 在微信小程序中使用本地存储的方法
  • 《volatile 与 synchronized 底层实现与性能比较》
  • ubuntu syslog中appindicator报错解决
  • 深入理解C++缺省参数:从基础用法到最佳实践
  • 8-verilog-串口接收与发送模块
  • Python切片命名技术详解:提升代码可读性与维护性的专业实践
  • linux下jvm之jstack的使用
  • 洛谷——P1048 [NOIP 2005 普及组] 采药
  • 【openlayers框架学习】九:openlayers中的交互类(select和draw)
  • GaussDB SQL执行计划详解
  • Rust: 获取 MAC 地址方法大全
  • Zama的使命
  • 【读论文】KAG-Thinker:升级版RAG 框架
  • 推荐系统学习笔记(九)曝光过滤 Bloom Filter
  • 【DL学习笔记】感受野(Receptive Field)
  • 映射网络驱动器后,重启映射就没了
  • 王之凝视 免安中文 离线运行版
  • 【Bluetooth】【Transport层篇】第四章 基于基础UART的蓝牙硬件发送协议 UART H4 Transport详解
  • wordpress登陆前登陆后显示不同的顶部菜单