我读了相关的问题,但解决方案对我来说并不奏效.
我得到了org.springframework.batch.item.ReaderNotOpenException:读者必须打开才能读取异常.
以下是我的配置:
@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(); }
inputZipFile变量设置正确,文件存在于驱动器上.
我检查了FlatFileItemReader代码,并且ReaderNotOpenException发生在读者类的读者成员未设置时.读者成员设置为doOpen方法.
看起来doOpen不被调用.问题是为什么?
解决方法
当我将读取器bean的返回类型从Item更改为FlatFileItemReader时,问题消失了.我仍然不清楚为什么这是一个问题,因为chunk().reader()接受ItemReader作为输入.我假设有一些AOP魔术在引擎盖下,FlatFileReader init和返回类型匹配.