Spring Batch: org.springframework.batch.item.ReaderNotOpenException: Reader 必須先開啟才能閱讀

2022-09-01 13:41:26

我阅读了与SO相关的问题,但解决方案不适合我。

我得到例外。org.springframework.batch.item.ReaderNotOpenException: Reader must be open before it can be read

以下是我的配置:

@Bean
@StepScope
public ItemReader<Player> reader(@Value("#{jobParameters[inputZipfile]}") String inputZipfile) {
                final String [] header = { .. this part omitted for brevity ... };
                FlatFileItemReader<Player> reader = new FlatFileItemReader<Player>();


                System.out.println("\t\t\t\t\t"+inputZipfile);

                reader.setResource(new ClassPathResource(inputZipfile));
                reader.setLineMapper(new DefaultLineMapper<Player>() {{
                    setLineTokenizer(new DelimitedLineTokenizer() {{
                        setNames( header );
                    }});
                    setFieldSetMapper(new BeanWrapperFieldSetMapper<Player>() {{
                        setTargetType(Player.class);
                    }});
                }});
                reader.setComments( header );
                return reader;
}

@Bean
@StepScope
public ItemProcessor<Player, PlayersStats> processor(@Value("#{jobParameters[statType]}") String statType,
                                                                 @Value("#{jobParameters[season]}") String season){
                PlayersStatsProcessor psp = new PlayersStatsProcessor();
                psp.setStatisticType( StatisticType.valueOf(statType) );
                psp.setSeason( season );
                return psp;
}


@Bean
@StepScope
public ItemWriter<PlayersStats> writer(){
            return new CustomWriter();
}


@Bean
public Job generateStatisticsJob() {

        return this.jobs.get("generateStatisticsJob")
                .incrementer(new RunIdIncrementer())
                .start(processPlayerStats())
                //.end()
                .build();
}

@Bean
public Step processPlayerStats() {
           return this.steps.get("processPlayerStats")        
                        .<Player, PlayersStats> chunk(10)
                        .reader(reader(null))
                        .processor(processor(null,null))
                        .writer(writer())
                        .build();
}

输入ZipFile变量设置正确,并且该文件存在于驱动器上。我签入了 FlatFileItemReader 代码,当未设置 reader 类的 reader 成员时,会发生 ReaderNotOpenException。读取器成员在 doOpen 方法中设置。看起来 doOpen 没有调用。问题是为什么?


答案 1

当我将读者 Bean 的返回类型从 Item 更改为 FlatFileItemReader 时,问题消失了。我仍然不清楚为什么这是一个问题,因为 chunk().reader() 接受 ItemReader 作为输入。我假设引擎盖下有一些AOP魔术,它做FlatFileReader init并通过返回类型匹配。


答案 2

既然你把阅读器放进去,豆子返回类型应该是实现类型:StepScopeFlatFileItemReader

@Bean
@StepScope
public FlatFileItemReader<Player> reader(@Value("#{jobParameters[inputZipfile]}") String inputZipfile) {
            ...
            return reader;
}

如果指定接口,则 Spring 代理只能访问接口上指定的方法和注释,并且缺少重要的注释。日志中还有一个警告(带有拼写错误):ItemReader

2015-05-07 10:40:22,733 WARN  [main] org.springframework.batch.item.ItemReader is an interface.  The implementing class will not be queried for annotation based listener configurations.  If using @StepScope on a @Bean method, be sure to return the implementing class so listner annotations can be used.
2015-05-07 10:40:22,748 WARN  [main] org.springframework.batch.item.ItemReader is an interface.  The implementing class will not be queried for annotation based listener configurations.  If using @StepScope on a @Bean method, be sure to return the implementing class so listner annotations can be used. 

目前,Spring Boot Batch示例也返回了ItemReader,所以我想其他人也会遇到同样的问题。


推荐