In this post under Spring Batch, I will explain how to change skip policy to one of out of the box provided skip policies.
Whenever we use the “skip-limit” attribute and “skippable-exception-classes” element as shown below at line 4 and 5
1 <batch:job id="importEmployees">
2 <batch:step id="readWriteEmployees">
3 <batch:tasklet>
4 <batch:chunk reader="reader" writer="writer" commit-interval="50" skip-limit="200">
5 <batch:skippable-exception-classes>
6 <batch:include class="org.springframework.batch.item.file.FlatFileParseException"/>
7 </batch:skippable-exception-classes>
8 </batch:chunk>
9 <batch:listeners>
10 <batch:listener ref="mySkipListener"/>
11 </batch:listeners>
12 </batch:tasklet>
13 </batch:step>
14 </batch:job>
Spring Batch internally uses “org.springframework.batch.core.step.skip.LimitCheckingItemSkipPolicy” implementation. This class is one of the out of the box provided skip policies. The others are
1) AlwaysSkipItemSkipPolicy
2) ExceptionClassifierSkipPolicy
3) NeverSkipItemSkipPolicy
4) CompositeSkipPolicy
As part of this example I will show you how to use “AlwaysSkipItemSkipPolicy”.
AlwaysSkipItemSkipPolicy class just skips records which throws exceptions (regardless of type of exception). There is no limit check and a way to specify for which specific exception to skip.
Below is the xml configuration
XML Snippet
1 <bean id="alwaysSkipItemSkipPolicy" class="org.springframework.batch.core.step.skip.AlwaysSkipItemSkipPolicy"/>
2
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="alwaysSkipItemSkipPolicy"/>
7 </batch:tasklet>
8 </batch:step>
9 </batch:job>
In the above xml snippet, at line 1 I create a bean definition for class “AlwaysSkipItemSkipPolicy” with id “alwaysSkipItemSkipPolicy”.
Then I refer to the bean “alwaysSkipItemSkipPolicy” using “skip-policy” attribute at line 6.
In this way, we can use skip policies other than LimitCheckingItemSkipPolicy.
Below is the complete xml code for your reference.
XML code
<?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"
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">
<bean id="employee" class="package26.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"/>
</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="alwaysSkipItemSkipPolicy" class="org.springframework.batch.core.step.skip.AlwaysSkipItemSkipPolicy"/>
<batch:job id="importEmployees">
<batch:step id="readWriteEmployees">
<batch:tasklet>
<batch:chunk reader="reader" writer="writer" commit-interval="50" skip-policy="alwaysSkipItemSkipPolicy"/>
</batch:tasklet>
</batch:step>
</batch:job>
<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
</bean>
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository"/>
</bean>
</beans>