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

MySkipPolicy

package xml.package25;
  
import org.springframework.batch.core.step.skip.SkipLimitExceededException;
import org.springframework.batch.core.step.skip.SkipPolicy;
  
public class MySkipPolicy implements SkipPolicy {
   @Override
   public boolean shouldSkip(Throwable throwable, int skipCount) throws SkipLimitExceededException {
       System.out.println("SkipCount: " + skipCount);
      if(skipCount == 5) {
          return false;
      }
      return true;
  }
}

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

   <bean id="mySkipPolicy" class="xml.package25.MySkipPolicy"/>
   
   <batch:job id="importEmployees">
       <batch:step id="readWriteEmployees">
           <batch:tasklet>
               <batch:chunk reader="reader" writer="writer" commit-interval="50" skip-policy="mySkipPolicy"/>
           </batch:tasklet>
       </batch:step>
   </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="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:batch="http://www.springframework.org/schema/batch"
	   xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd
						http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
	
	<bean id="employee" class="xml.package25.Employee" scope="prototype"/>
	
	<bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader">
		<property name="resource" value="file:EmployeeRecordsWithFileParseException.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"/>
					</bean>
				</property>
				<property name="fieldSetMapper">
					<bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
						<property name="prototypeBeanName" value="employee"/>
					</bean>
				</property>
			</bean>
		</property> 
	</bean>
	
	<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>
				</property>
			</bean>
		</property>
	</bean>
	
	<bean id="mySkipPolicy" class="xml.package25.MySkipPolicy"/>
	
	<batch:job id="importEmployees">
		<batch:step id="readWriteEmployees">
			<batch:tasklet>
				<batch:chunk reader="reader" writer="writer" commit-interval="50" skip-policy="mySkipPolicy"/>
			</batch:tasklet>
		</batch:step>
	</batch:job>

	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>

	<bean id="dataSource"
		  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="org.h2.Driver" />
		<property name="url" value="jdbc:h2:file:~/repository" />
		<property name="username" value="" />
		<property name="password" value="" />
	</bean>

	<!-- create job-meta tables automatically -->
	<jdbc:initialize-database data-source="dataSource">
		<jdbc:script
				location="org/springframework/batch/core/schema-drop-h2.sql" />
		<jdbc:script location="org/springframework/batch/core/schema-h2.sql" />
	</jdbc:initialize-database>

	<bean id="jobRepository"
		  class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<property name="transactionManager" ref="transactionManager"/>
		<property name="databaseType" value="h2"/>
	</bean>

	<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.TaskExecutorJobLauncher">
		<property name="jobRepository" ref="jobRepository"/>
	</bean>
</beans>

Leave a comment