Reputation: 11
i want to read multiple xml files in spring batch , and before reading i should validate the name of each file and put it in the context , how can i process ? is it possible to have this senario using tasklet and reader writer processor ?? :
folder : file1.xml file2.xml file3.xml
validate filename (file1.xml) -- read file1 -- process -- write
then
validate filename (file2.xml) -- read file2 -- write
validate filename (file3.xml) -- read file3 -- write
......
or any other way ??????
Upvotes: 0
Views: 3007
Reputation: 21503
There are three approaches you can take with this. Each has it's benefits and weaknesses.
Use a step to validate
You can set your job up with a validateFileName
step that preceedes the step that processes the files (say processFiles
). The validateFileName
step would do any validations on the file name needed then provide the files to process to the next step. How it communicates this could be a simple as moving the valid files to a new directory or as complex as using the job's ExecutionContext
to store the names of the files to process.
The advantage of this is that it decouples validation from processing. The disadvantage is that it makes the job slightly more complex given that you'd have an extra step.
Use a StepExecutionListener to do the validation
You could use a StepExecutionListener#beforeStep()
call to do the validation. Same concepts apply as before with regards to how to communicate what validates and what doesn't.
This may be a less complex option, but it more tightly couples (albeit marginally) the processing and validation.
Use an ItemReader
that validates before it reads
This last option is to write an ItemReader
implementation that is similar to the MultiResourceItemReader
but provides a hook into validating the file before reading it. If the file doesn't validate, you would skip it.
This option again couples validation with the processing, but may provide a nice reusable abstraction for this particular use case.
I hope this helps!
Upvotes: 3