chastain
chastain

Reputation: 83

Spring Batch validating line and sending email if validation fail

I want to do some validation on line I process and if validation fail, I would like to send email that we can't process that line. How can I achieved that? I'm using FlatFileItemReader and processor after it.

Upvotes: 2

Views: 651

Answers (2)

Mahmoud Ben Hassine
Mahmoud Ben Hassine

Reputation: 31630

I'm using FlatFileItemReader and processor after it

That's how I would do it too. In your processor, you can implement the validation logic and throw an exception for invalid items. This is one of the typical use cases for an item processor BTW. The Validating input section of the reference documentation shows an example.

You can then configure the exception to be skippable and use a SkipListener to store invalid items (either in memory or to a file or a db table, etc). Finally, you would use an additional tasklet step to send an email with those invalid items.

HTH.

Upvotes: 2

chastain
chastain

Reputation: 83

I have problem after adding faultTolerant, skipLimit an skip into the step config. In this setup I'm unable to use build()

return stepBuilderFactory.get("job")
                .allowStartIfComplete(true)
                .<RegisterUserRequest, RegisterUserRequest>chunk(2000)
                .reader(registerUserFromFileReader)
                .processor(registerUserProcessor)
                .faultTolerant()
                .skipLimit(50000)
                .skip(RegisterUserLineException.class)
                .listener(registerUserSkipListener)
                .writer(userRegistration)
                .listener(registerUserFinalizer)
                .build() //doesn't work

Upvotes: 0

Related Questions