背景:并发抽取接口数据。通过ai生成后调试了老半天才能使,记录一下
主要是处理器Processor, 读取器Reader和写库用的Writer

使用了框架的Shell功能,挺强大的。如果要对接口读取的数据做过滤,可以增加执行参数,如:--skip-filter

- 这个参数我是用在Processor中的,因为要对已经获取的数据进行过滤。但其中要注意的点就是需要再处理器中获取这个参数
java
public class ExamReportItemProcessor implements ItemProcessor<ExamReportDto, ExamReportEntity> {
@Autowired
private InpatientValidationService inpatientValidationService;
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private boolean skipFilter = false;
@BeforeStep
public void beforeStep(StepExecution stepExecution) {
JobParameters jobParameters = stepExecution.getJobParameters();
String skipFilterStr = jobParameters.getString("skipFilter");
this.skipFilter = "true".equalsIgnoreCase(skipFilterStr);
}
还要注意:并发执行命令需要用到注解 @StepScope,否则会陷入分批次拉数据却只调用一次的尴尬, 调了许久
java
# 这是在BatchConfig.java中的配置
@Bean
@StepScope
public ExamReportItemReader examReportItemReader() {
return new ExamReportItemReader();
}
@Bean
@StepScope
public ExamReportItemProcessor examReportItemProcessor() {
return new ExamReportItemProcessor();
}
另外我是用cursor的auto模式调试的,出现问题就将问题和输入的命令进行反馈,经历了4-5次才能跑通了
放个私有的gitee地址
过程记录
bash
# 使用curl检测接口,使用的POST格式,(接口每次只返回一页数据和总页数.为了方便,直接全部保存)
curl -X POST "http:xxxx" -H "Content-Type: application/json" -d '{"StartTime": "2025-01-01 00:00:00"....}'
# 使用json_pp美化json语句,挺方便,没有额外安装工具
echo '{"name":"john","age":30}' | json_pp!74