Reputation: 121
I'm working with Spring batch for the first time and I need some help about validation step.
Before realy starting my batch Job, I need some validation on the file to be handled like :
After that I can really start my batch job.
I have in mind multiple step chaining each others, the initial step doing the validation, if the file is not valid the process go in error step.
How can I doing this validation? All reader found give me only one line of the file, I have to create my own reader? Which approach will you take to manage this behaviour?
Upvotes: 2
Views: 8216
Reputation: 18413
For first point use a JobExecutionDecider
; if test is not passed end your job with error(s).
For remaining points you can approach the problem writing your own reader or
FlatFileItemReader.setLinesToSkip()
and FlatFileItemReader.setSkippedLinesCallback()
PeekableItemReader
in order to check if you are on last line and perform different validation in your processor phase.This kind of approach could requires you to write different domain objects for first line,last line and other lines.
Upvotes: 1
Reputation: 2749
You can use single step and make a listener and you can validate whatever you want in beforeStep or you can have two steps and first step is for validation and return appropriate return status in afterstep method of StepExecutionListener .
In short- use listeners to validate , refer a very nice example here
To validate input file, you can refer this example
Upvotes: 0