springspring-batch

Spring batch dynamic reader


I have a requirement in Spring Batch, the file name will be passed as input via arguments, if the file is present we need to read the file in chunks of 1000 or if file name is not passed then need to connect to oracle database and read all the records from a particular table

I have gone through documentation but not sure how to implement this

Note: the file will only contains id, enriching will be done in processor

Could some one please suggest or provide some sample code on how to implement the above functionality


Solution

  • You can define a step scoped reader and return the required type based on the job parameter:

    @Bean
    @StepScope
    public ItemStreamReader<Person> itemReader(@Value("#{jobParameters['file']}") String file) {
        if (file != null) {
            //return a FlatFileItemReader<Person> configured with the `file` parameter
        } else {
            // return a JdbcCursorItemReader<Person> or a JdbcPagingItemReader<Person>
        }
    }
    

    With that in place, you can run your job like: