Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Batch FlatFileItemReader continue on incorrect number of tokens

I'm using Spring Batch FlatFileItemReader to parse csv files. Every now and then I get an ill-formated line and the application completely crashes with:

Caused by: org.springframework.batch.item.file.transform.IncorrectTokenCountException: Incorrect number of tokens found in record: expected 11 actual 18

Is there any way to tell the FlatFileItemReader to continue (throw exception and continue or ignore and continue) without it completely exiting the application.

I'm guessing I may need to extend the FlatFileItemReader to make this happen as there does not appear to be any setting for this. Any suggestions on how best to proceed and make this happen?

like image 983
DavidR Avatar asked Oct 28 '25 01:10

DavidR


1 Answers

You can configure SkipLogic for your batch jobs Here's a link to doc

Basically, if you are using Java Config to manage your Batch Job you can do something like this

stepBuilderFactory.get("step1")
                .<Person, Person>chunk(10)
                .reader(reader)
                .writer(writer)
                .processor(processor)
                .faultTolerant()
                .skipLimit(10)
                .skip(RuntimeException.class)
                .listener(skipListener) // if you want to add
                .build();
like image 71
A0__oN Avatar answered Oct 30 '25 22:10

A0__oN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!