Spring Batch Course 11- Skip invalid Records


  • Skip policy is used to skip the records that are failed, otherwise this will cause the job to abort. The skip policy will allow the Spring Batch to complete the job for the records that are valid and skip the invalid records.
  • We can set the skipListener to handle the skipped records either to put them in a file or into a table in the database so that the support team can look into the failures and recover/reprocess or fix the invalid records.
  • To enable the skip we need to the following –
    • Add .faultTolerant() into the StepBuilder. This is to enable the fault tolerant policies like Skip, Retry and Restart.
    • Add .skip(Exception.class) after that to let the step know what are the exceptions that need to be skipped.
    • Add .skipLimit(<<limit>>) to restrict the number of skipped records. If the number of skipped records exceed this count then the step will abort.
    • We can also add .skipPolicy() to apply custom/out of box skip policies instead of .skipLimit
      • new AlwaysSkipItemSkipPolicy() will skip all the items that are in error.
    • To handle skip records add .listener(<<ListenerClassName>>) into the step Builder.
      • A custom skip listener can also be created where you can handle custom events to handle the Read, Write,Process errors like writing it to a file or a DB.
        • Create new class for a custom Skip policy implementing the SkipListener.
        • OnSkipInRead — Use this to specify how to handle read exceptions.
        • OnSkipInProcess — Use this to specify how to handle process exceptions.
        • OnSkipInWrite — Use this to specify how to handle write exceptions.