I wanted to skip some record on process.
what i have tried is, i have created custom exception and throw the exception when i want to skip the record and its calling the Skip listener onSkipInProcess method.Its working fine.
please find the configuration.
<batch:chunk reader="masterFileItemReader" writer="masterFileWriter" processor="itemProcessor" commit-interval="5000" skip-limit="100000" >
<batch:skippable-exception-classes>
<batch:include class="org.springframework.batch.item.file.FlatFileParseException"/>
<batch:include class="com.exception.SkipException"/>
</batch:skippable-exception-classes>
<batch:listeners>
<batch:listener ref="recordSkipListener"/>
</batch:listeners>
But i would like to know is there any other way to skip the record on process?
Regards, Shankar
There are indeed two ways to do this, one like you mention with skip mechanism and the other with returning null
which will filter out item and not write it. Here is documentation link - 6.3.2. Filtering records where it is nicely explained what is difference between two approaches. Also this blog post explains skip in details and transactions in batch.
When you i.e. parse csv file and you expect 5 items per line but one line holds 6 items that is invalid item, and you can opt out to skip it (by marking reader exception as skippable and defining condition in policy as you gave example). However if each line holds name and your use case is to not write items that start with letter N
that is better implemented with returning null
(filtering item) since it is valid item but not according to your business case.
Please note also that if you return null
number of those items will be in StepContext
in getFilterCount()
and if you use skip approach they will be in getReadSkipCount()
, getProcessorSkipCount
and getWriteSkipCount
respectfully.