2
votes

I have a Spring Batch job that needs to do the below

  • Check a directory on local file system that may contain more than one file
  • Process each of the files, save data from those files to database
  • Rename files by adding a suffix to include PROCESSED or ERROR

I have used the below

  • A MultiResourceItemReader that reads files and delegates to a FlatFileItemReader
  • The FlatFileItemReader reads data using LineMapper, FieldSetMapper
  • An ItemProcessor manipulates data read
  • An ItemWriter writes to the database

What I want to do

  • Rename each file to either PROCESSED / ERROR at the end of Step based on execution status
  • How do I pass the resource file name that the FlatFileItemReader processes to the StepExecutionListener?
  • How do I pass the resource file name to the ItemProcessor as it also needs to save the name of the file the data was read from

Below is my relevant config

<batch:job id="myJob" job-repository="jobRepository">
    <batch:step id="processFiles">
        <batch:tasklet>
            <batch:chunk reader="multiResourceReader" processor="myItemProcessor" writer="myItemWriter" commit-interval="100" />
        </batch:tasklet>
        <batch:listeners>
            <batch:listener ref="myStepListener"/>
        </batch:listeners>                          
    </batch:step>
</batch:job>
1
Currently reviewing Luca's suggestions and other ways of implementing this. Found that I can make my data object that needs to be mapped ResourceAware and the MultiResourceItemReader will inject the resource. That solves one of the issues.mekatoka

1 Answers

0
votes

This scenario is similar to this one.
I'm not a fan of first solution because is too messy and file rename is "hidden" into java code and can't be easily detected from job's xml definition.
I prefer solution #2: IMO success/error conditions are easy to check and step flow is more cleaner and well separated.
Get your consideration, I hope my advices will help.