Implementing Custom Skip Policy

In my previous posts under Spring Batch, I explained with example how to use built in out of the box skip policies.

In this post I will explain how to create our own custom skip policy and use it.

To create our own custom skip policy we need to implement the interface org.springframework.batch.core.step.skip.SkipPolicy as shown below


1  package package27;
3  import org.springframework.batch.core.step.skip.SkipLimitExceededException;
4  import org.springframework.batch.core.step.skip.SkipPolicy;
6  public class MySkipPolicy implements SkipPolicy {
7   @Override
8   public boolean shouldSkip(Throwable throwable, int skipCount) throws SkipLimitExceededException {
9       System.out.println("SkipCount: " + skipCount);
10      if(skipCount == 5) {
11          return false;
12      }
13      return true;
14  }
15 }

In the above code, we implement SkipPolicy interface and provide implementation for shouldSkip method.

The shouldSkip method takes two arguments
1) Throwable –> the exception thrown when processing a record
2) skipCount –> a number representing the number of exception (i.e., argument 1) thrown

The method returns a boolean, if true is returned, the processing continues otherwise the processing stops.

In our code, if the skipCount reaches 5 we are returning false and stopping the processing midway.

Next i will show you how to integrate this skip policy with the batch job.

Below is the xml configuration

1   <bean id="mySkipPolicy" class="package27.MySkipPolicy"/>
3   <batch:job id="importEmployees">
4       <batch:step id="readWriteEmployees">
5           <batch:tasklet>
6               <batch:chunk reader="reader" writer="writer" commit-interval="50" skip-policy="mySkipPolicy"/>
7           </batch:tasklet>
8       </batch:step>
9   </batch:job>

In the above xml code, at line 1, I create a bean definition for MySkipPolicy class with id “mySkipPolicy”.

Next I refer this bean at line 6 and set it to attribute “skip-policy”.

In this way we can integrate the custom skip policy with the batch job.

Below is the complete xml configuration for your reference

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="" xmlns:xsi=""
    <bean id="employee" class="package27.Employee" scope="prototype"/>
    <bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader">
        <property name="resource" value="file:FileInput8.txt"/>
        <property name="lineMapper">
            <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
                <property name="lineTokenizer">
                    <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                        <property name="names" value="id,name,status,salary"/>
                <property name="fieldSetMapper">
                    <bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
                        <property name="prototypeBeanName" value="employee"/>
    <bean id="writer" class="org.springframework.batch.item.file.FlatFileItemWriter">
        <property name="resource" value="file:FileOutput.txt"/>
        <property name="lineAggregator">
            <bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
                <property name="fieldExtractor">
                    <bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
                        <property name="names" value="id,name,status,salary"/>
    <bean id="mySkipPolicy" class="package27.MySkipPolicy"/>
    <batch:job id="importEmployees">
        <batch:step id="readWriteEmployees">
                <batch:chunk reader="reader" writer="writer" commit-interval="50" skip-policy="mySkipPolicy"/>
    <bean id="transactionManager" class="" />
    <bean id="jobRepository" class="">
        <property name="transactionManager" ref="transactionManager"/>
    <bean id="jobLauncher" class="">
        <property name="jobRepository" ref="jobRepository"/>

Check out these books on Amazon

Leave a Reply