【发布时间】:2021-06-21 09:56:42
【问题描述】:
在项目阅读器实现中尝试读取 JdbcCursorItemReader 时,我得到阅读器未打开异常。我检查了堆栈溢出,但无法讨论 jdbc 项目阅读器。
这里是批量配置和项目阅读器实现的代码。仅添加了必需的代码。
public class BatchConfig extends DefaultBatchConfigurer {
@Bean
public ItemReader<Allocation> allocationReader() {
return new AllocationReader(dataSource);
}
@Bean
public Step step() {
return stepBuilderFactory.get("step").<Allocation, Allocation>chunk (1).reader(allocationReader()).processor(allocationProcessor()).writer(allocationWriter()).build();
}
}
public class AllocationReader implements ItemReader<Allocation> {
private DataSource ds;
private string block;
public AllocationReader(DataSource ds) {
this.ds = ds;
}
@BeforeStep
public void readStep(StepExecution StepExecution) {
this.stepExecution = StepExecution;
block = StepExecution.getJobExecution().get ExecutionContext().get("blocks");
}
@Override
public Allocation read() {
JdbcCursorItemReader<Allocation> reader = new JdbcCursorItemReader<Allocation>();
reader.setSql("select * from blocks where block_ref = + block);
reader.setDataSource(this.ds);
reader.setRowMapper(new AllocationMapper());
return reader.read();
}}
我无法在批处理配置中将项目阅读器编写为 bean,因为我需要在进入项目阅读器以访问 stepexecution 之前调用。
如果Item reader read()函数返回类型更改为jdbccursorItemReader类型,会在Step reader()中抛出类型异常。
让我知道我缺少什么或需要任何其他代码 sn-p。
【问题讨论】:
标签: spring-boot spring-batch spring-jdbc itemrenderer