Spring Batch: org.springframework.batch.item.ReaderNotOpenException: Reader must be open before it can be read

前端 未结 7 1136
独厮守ぢ
独厮守ぢ 2020-12-15 05:24

I read SO related questions but the solutions don\'t work for me.

I get the org.springframework.batch.item.ReaderNotOpenException: Reader must be open before i

相关标签:
7条回答
  • 2020-12-15 05:28

    I think you should increase your chunk size in processPlayerStats() step bean class, i.e from chunk(10) to chunk(100/more may be).

    0 讨论(0)
  • 2020-12-15 05:28

    Same problem here. Changing return type of my reader to the real implementation and adding to the reader

    implements ItemStream
    

    Did the trick for me

    0 讨论(0)
  • 2020-12-15 05:31

    The method I defined is like below:

        @Bean
        @StepScope
        public ItemReader<BP> BPReader(){
           
            JdbcCursorItemReader<BP> itemReader = new JdbcCursorItemReader<BP>();
            ...
            return itemReader;
        }

    The type I defined in the method is ItemReader which is an interface, the return type is JdbcCursorItemReader which is a sub-class of it. By changing the return type defination to JdbcCursorItemReader solved my problem

    0 讨论(0)
  • 2020-12-15 05:32

    It is because ItemReader don't have the open method, using hte StepScope will create a proxy class based on the return type. It is also ok to return ItemStreamReader

    0 讨论(0)
  • 2020-12-15 05:37

    I have fixed it by:

    reader.open(new ExecutionContext());
    
    0 讨论(0)
  • 2020-12-15 05:42

    Since you put the reader in StepScope, the bean return type should be the implementing type FlatFileItemReader:

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

    If you specify the interface, the Spring proxy only has access to the methods and annotations specified on the interface ItemReader and is missing important annotations. There is also a warning (with a typo) in the logs:

    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. 
    

    Currently the Spring Boot Batch example is also returning the ItemReader, so I guess other people will struggle with the same issues.

    0 讨论(0)
提交回复
热议问题