Skip to content
Advertisement

Spring Batch avoid launch Reader and Writer before tasklet

I’m working with spring batch and have a job with two steps the first step (tasklet) validation the header CSV and the second step reads an CSV file and Write to another CSV file like this:

@Bean
public ClassifierCompositeItemWriter<POJO> classifierCompositeItemWriter() throws Exception {
    Classifier<POJO, ItemWriter<? super POJO>> classifier = new ClassiItemWriter(ClassiItemWriter.itemWriter());
    return new ClassifierCompositeItemWriterBuilder<POJO>()
            .classifier(classifier)
            .build();
}



@Bean
public Step readAndWriteCsvFile() throws Exception {
    return stepBuilderFactory.get("readAndWriteCsvFile")
            .<POJO, POJO>chunk(10000)
            .reader(ClassitemReader.itemReader())
            .processor(processor())
            .writer(classifierCompositeItemWriter())
            .build();
}

I used a FlatFileItemReader (in ClassitemReader) and a FlatFileItemWriter (in ClassiItemWriter), before reading CSV. I checked if the header of CSV file is correct via tasklet like this :

@Bean
public Step fileValidatorStep() {
    return stepBuilderFactory
            .get("fileValidatorStep")
            .tasklet(fileValidator)
            .build();
}

And if so I process the transformation from CSV file recieved to another file CSV.

in the jobBuilderFactory i check if ExistStatus comes from tasklet fileValidatorStep is “COMPLETED” to forward the process to readAndWriteCsvFile(), if not “COMPLETED” and tasklet fileValidatorStep return ExistStatus “ERROR” the job end and exit processing.

@Bean
public Job job() throws Exception {
    return jobBuilderFactory.get("job")
            .incrementer(new RunIdIncrementer())
            .start(fileValidatorStep()).on("ERROR").end()
            .next(fileValidatorStep()).on("COMPLETED").to(readAndWriteCsvFile())
            .end().build();
}

The problem is that when I launch my Job the Bean readAndWriteCsvFile runs first of the tasklet, that means the standard Bean of reader and writer of spring batch always loaded in life cycle before i can validation the header and check the ExistStatus, the reader is still working and reades the file and puts data in another file without check because is loading Bean during launch of Job before all tasklet.

How i can launch readAndWriteCsvFile methode after fileValidatorStep ?

Advertisement

Answer

I solved my problem, i changed the bean FlatFileItemReader method inside the Job class configuration with the annotation @StepScope, now this bean has only loads when I need it also should avoid declaring the FlatFileItemReader bean out of scope of the job

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement