1
votes

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 :

  • Check the name with information from database
  • Check first and last line (wich are specific) with information from system and database
  • Check total line number with a data in the first line

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?

2

2 Answers

1
votes

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

  • for first line use FlatFileItemReader.setLinesToSkip() and FlatFileItemReader.setSkippedLinesCallback()
  • use a 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.

0
votes

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