OcterA
OcterA

Reputation: 121

Validation step in Spring batch

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

Answers (2)

Luca Basso Ricci
Luca Basso Ricci

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

  • 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.

Upvotes: 1

surya
surya

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

Related Questions