spring 批次:Reader 必须 > 打开才能读取
spring batch: Reader must be > open before it can be read
我有下面的批次,它通过 bloc
从 csv 文件中读取
public class StreamAgentReader extends AbstractItemCountingItemStreamItemReader<StudentDTO> {
@Override
protected StudentDTO doRead() throws Exception {
LOGGER.info("Read Agent");
return new FlatFileItemReaderBuilder<StudentDTO>().name("agentReader")
.resource(new ClassPathResource("data/tables.CSV")).linesToSkip(1)
.build().read();
}
@Override
protected void doOpen() throws Exception {
// Nothing to do here
}
@Override
protected void doClose() throws Exception {
// Nothing to do here
}
}
@Configuration
public class BatchJobConfiguration {
@Autowired
private JobBuilderFactory jobBuilder;
@Autowired
private StepBuilderFactory stepBuilder;
@Autowired
private SingleJobInstanceListener singleJobInstanceListener;
@StepScope
@Bean
public ItemReader<StudentDTO> streamAgentReader() {
return new StreamAgentReader();
}
@StepScope
@Bean
public ItemWriter<StudentDTO> streamAgentWriter() {
return new StreamAgentWriter();
}
@Bean
public Step streamAgentChunkStep() {
return stepBuilder.get("streamAgentChunkStep").<StudentDTO, StudentDTO>chunk(10)
.reader(streamAgentReader()).writer(streamAgentWriter()).build();
}
@Bean
public Job streamAgentJob() {
return jobBuilder.get("StreamAgentJob15").listener(singleJobInstanceListener).start(streamAgentChunkStep())
.build();
}
}
执行批处理时出现此错误
org.springframework.batch.item.ReaderNotOpenException: Reader must be
open before it can be read. at
org.springframework.batch.item.file.FlatFileItemReader.readLine(FlatFileItemReader.java:201)
at
org.springframework.batch.item.file.FlatFileItemReader.doRead(FlatFileItemReader.java:178)
at
org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:93)
at
com.zp.digital.edgebb.batch.tasks.StreamAgentReader.doRead(StreamAgentReader.java:43)
我认为这是因为您将 reader 放在 StepScope
中,在这种情况下,bean return 类型应该是实现类型 FlatFileItemReader
。
所以改变这行代码:
@StepScope
@Bean
public ItemReader<StudentDTO> streamAgentReader() {
return new StreamAgentReader();
}
对此:
@Bean
@StepScope
public FlatFileItemReader<StudentDTO> streamAgentReader(){
FlatFileItemReader<StudentDTO> itemReader = new FlatFileItemReader<StudentDTO>();
itemReader.setLineMapper(lineMapper());
itemReader.setResource(new ClassPathResource("data/tables.CSV"));
return itemReader;
}
并在doRead()
方法中添加以下行:
FlatFileItemReader<StudentDTO> reader = new FlatFileItemReader<StudentDTO>();
...
reader.read();
我有下面的批次,它通过 bloc
从 csv 文件中读取public class StreamAgentReader extends AbstractItemCountingItemStreamItemReader<StudentDTO> {
@Override
protected StudentDTO doRead() throws Exception {
LOGGER.info("Read Agent");
return new FlatFileItemReaderBuilder<StudentDTO>().name("agentReader")
.resource(new ClassPathResource("data/tables.CSV")).linesToSkip(1)
.build().read();
}
@Override
protected void doOpen() throws Exception {
// Nothing to do here
}
@Override
protected void doClose() throws Exception {
// Nothing to do here
}
}
@Configuration
public class BatchJobConfiguration {
@Autowired
private JobBuilderFactory jobBuilder;
@Autowired
private StepBuilderFactory stepBuilder;
@Autowired
private SingleJobInstanceListener singleJobInstanceListener;
@StepScope
@Bean
public ItemReader<StudentDTO> streamAgentReader() {
return new StreamAgentReader();
}
@StepScope
@Bean
public ItemWriter<StudentDTO> streamAgentWriter() {
return new StreamAgentWriter();
}
@Bean
public Step streamAgentChunkStep() {
return stepBuilder.get("streamAgentChunkStep").<StudentDTO, StudentDTO>chunk(10)
.reader(streamAgentReader()).writer(streamAgentWriter()).build();
}
@Bean
public Job streamAgentJob() {
return jobBuilder.get("StreamAgentJob15").listener(singleJobInstanceListener).start(streamAgentChunkStep())
.build();
}
}
执行批处理时出现此错误
org.springframework.batch.item.ReaderNotOpenException: Reader must be open before it can be read. at org.springframework.batch.item.file.FlatFileItemReader.readLine(FlatFileItemReader.java:201) at org.springframework.batch.item.file.FlatFileItemReader.doRead(FlatFileItemReader.java:178) at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:93) at com.zp.digital.edgebb.batch.tasks.StreamAgentReader.doRead(StreamAgentReader.java:43)
我认为这是因为您将 reader 放在 StepScope
中,在这种情况下,bean return 类型应该是实现类型 FlatFileItemReader
。
所以改变这行代码:
@StepScope
@Bean
public ItemReader<StudentDTO> streamAgentReader() {
return new StreamAgentReader();
}
对此:
@Bean
@StepScope
public FlatFileItemReader<StudentDTO> streamAgentReader(){
FlatFileItemReader<StudentDTO> itemReader = new FlatFileItemReader<StudentDTO>();
itemReader.setLineMapper(lineMapper());
itemReader.setResource(new ClassPathResource("data/tables.CSV"));
return itemReader;
}
并在doRead()
方法中添加以下行:
FlatFileItemReader<StudentDTO> reader = new FlatFileItemReader<StudentDTO>();
...
reader.read();