Home > Blockchain >  spring batch: Reader must be > open before it can be read
spring batch: Reader must be > open before it can be read

Time:05-29

I have the batch below which reads from a csv file by bloc

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();
    }
}

When the batch execute i get this error

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)

CodePudding user response:

I think this is beacause you put the reader in StepScope, and in that case the bean return type should be the implementing type FlatFileItemReader. So change this line of code:

@StepScope
        @Bean
        public ItemReader<StudentDTO> streamAgentReader() {
            return new StreamAgentReader();
        }

to this:

@Bean
@StepScope
public FlatFileItemReader<StudentDTO> streamAgentReader(){
    FlatFileItemReader<StudentDTO> itemReader = new FlatFileItemReader<StudentDTO>();
    itemReader.setLineMapper(lineMapper());
    itemReader.setResource(new ClassPathResource("data/tables.CSV"));
    return itemReader;
}

And indoRead() method add following lines:

 FlatFileItemReader<StudentDTO> reader = new FlatFileItemReader<StudentDTO>();
    ...
    reader.read();
  • Related