Announcement Announcement Module
Collapse
No announcement yet.
Late Parameter Binding in ItemWriter using JavaConfig Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Late Parameter Binding in ItemWriter using JavaConfig

    I have a simple job having below steps.
    1. ItemReader: Read Item from a file using ItemReader
    2. ItemProcessor: Process single file using Item Processor and generate a dynamic file name at some other path.Save it in stepExecutionContext
    3. ItemWriter: fetch that dynamic generated file name and path from stepExecutionContext. Use it in 'resource' property of FlatFileItemWriter to create it and write data in file.

    I am stuck in passing late binding parameter to FlatFileItemWriter. Below is the code snippet of my Configration file

    Code:
          // JOB
        @Bean
        public Job migrationJob(@Qualifier("readConfigStep") Step readConfigStep) {
    
            Job job = jobBuilderFactory.get("migrationJob").incrementer(new RunIdIncrementer())
                    .listener(jobExecutionListener()).validator(migrationJobValidator())
                    .start(readConfigStep).build();
    
            return job;
        }
       
    
         //read write config step 
        @Bean
        public Step readConfigStep(
                @Qualifier("migrateFlatFileReader") ItemReader<SConfig> migrateFlatFileReader,
                @Qualifier("downloadingDProcessor") ItemProcessor<SConfig, DConfig> itemProcessor,
                @Qualifier("delimitItemWriter") ItemWriter<DConfig> delimitItemWriter) {
    
            Step step = stepBuilderFactory.get("readMigrationConfigStep")
                    .<SConfig, DConfig> chunk(1).reader(migrateFlatFileReader)
                    .processor(itemProcessor).writer(delimitItemWriter).build();
            return step;
        }
    
         //writer expecting runtime value from stepExecution
    
        @Bean
        @StepScope
        public FlatFileItemWriter<DConfig> delimitItemWriter(
                @Value("#{stepExecutionContext[pathToDFile]}") Resource pathToDFile) {
            FlatFileItemWriter<DConfig> dFileItemWriter = new FlatFileItemWriter<DConfig>();
            dFileItemWriter.setResource(pathToDFile);
            dFileItemWriter.setLineAggregator(lineAggregator());
            dFileItemWriter.setHeaderCallback(new DConfigFileHeader());
            return dFileItemWriter;
        }

    After a long search I have added this post. A little help needed will be appreciated.

  • #2
    I know this is an old post... but did you figure out the problem?
    What worked for me was to use @Scope(value = "step") and putting single quotes around 'pathToDFile'.
    Code:
    @Bean
    @Scope(value = "step")
    public FlatFileItemWriter<DConfig> delimitItemWriter( @Value("#{stepExecutionContext['pathToDFile']}") Resource pathToDFile) {
       FlatFileItemWriter<DConfig> dFileItemWriter = new FlatFileItemWriter<DConfig>();
       dFileItemWriter.setResource(pathToDFile);
       dFileItemWriter.setLineAggregator(lineAggregator());
       dFileItemWriter.setHeaderCallback(new DConfigFileHeader());
       return dFileItemWriter;
    }

    Comment

    Working...
    X