Spring Batch_Configuring Skip Logichtml
spring 官方文檔:http://docs.spring.io/spring-batch/trunk/reference/html/configureStep.html#configuringSkip java
當spring batch 處理過程當中,可能要發生錯誤,就會致使整個批處理過程的失敗,而有些錯誤不該該致使整個批處理的失敗,那麼經過配置 <skippable-exception-classes> 能夠忽略掉某些錯誤,從而使 批處理繼續運行。mysql
下面是我主要的處理skip邏輯的配置:spring
<!-- add people desc job begin --> <batch:job id="addPeopleDescJob"> <batch:step id="addDescStep" parent="abstractStep"> <batch:tasklet> <batch:chunk reader="peopleAddDescReader" processor="allowSkipProcessor" writer="addDescPeopleWriter" commit-interval="2" skip-limit="10"> <batch:skippable-exception-classes> <!--batch:include配置容許發生的異常 --> <batch:include class="com.lyx.batch.InvalidDataException" /> </batch:skippable-exception-classes> </batch:chunk> </batch:tasklet> </batch:step> </batch:job> <!-- add people desc job end -->
其中下面這段配置是配置的要忽略的異常:sql
<batch:skippable-exception-classes> <!--batch:include配置容許發生的異常 --> <batch:include class="com.lyx.batch.InvalidDataException" /> </batch:skippable-exception-classes>
下面是完整的配置:數據庫
<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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 包的掃描 --> <context:component-scan base-package="com.lyx.batch" /> <bean id="exceptionHandler" class="com.lyx.batch.ExceptionListener" /> <batch:step id="abstractStep" abstract="true"> <batch:listeners> <batch:listener ref="exceptionHandler" /> </batch:listeners> </batch:step> <bean id="abstractCursorReader" abstract="true" class="org.springframework.batch.item.database.JdbcCursorItemReader"> <property name="dataSource" ref="dataSource" /> </bean> <!-- add people desc job begin --> <batch:job id="addPeopleDescJob"> <batch:step id="addDescStep" parent="abstractStep"> <batch:tasklet> <batch:chunk reader="peopleAddDescReader" processor="allowSkipProcessor" writer="addDescPeopleWriter" commit-interval="2" skip-limit="10"> <batch:skippable-exception-classes> <!--batch:include配置容許發生的異常 --> <batch:include class="com.lyx.batch.InvalidDataException" /> </batch:skippable-exception-classes> </batch:chunk> </batch:tasklet> </batch:step> </batch:job> <!-- add people desc job end --> <bean id="peopleAddDescReader" parent="abstractCursorReader" scope="step"> <property name="sql"> <value><![CDATA[select first_name ,last_name from people where first_name like ? or last_name like ?]]></value> </property> <property name="rowMapper" ref="peopleRowMapper" /> <property name="preparedStatementSetter" ref="preparedStatementSetter" /> <property name="fetchSize" value="20" /> </bean> <bean id="peopleRowMapper" class="com.lyx.batch.PeopleRowMapper" /> <bean id="preparedStatementSetter" class="com.lyx.batch.PeoplePreparedStatementSetter" /> <bean id="allowSkipProcessor" class="com.lyx.batch.AllowSkipProcessor" /> <bean id="addDescPeopleWriter" class="com.lyx.batch.AddDescPeopleWriter" /> <!--tomcat jdbc pool數據源配置 --> <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close"> <property name="poolProperties"> <bean class="org.apache.tomcat.jdbc.pool.PoolProperties"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username" value="root" /> <property name="password" value="034039" /> </bean> </property> </bean> <!-- spring batch 配置jobRepository --> <batch:job-repository id="jobRepository" data-source="dataSource" transaction-manager="transactionManager" isolation-level-for-create="REPEATABLE_READ" table-prefix="BATCH_" max-varchar-length="1000" /> <!-- spring的事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- batch luncher --> <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"> <property name="jobRepository" ref="jobRepository" /> </bean> </beans>
定義的異常類:apache
InvalidDataException.javatomcat
package com.lyx.batch; public class InvalidDataException extends Exception { private static final long serialVersionUID = 1L; public InvalidDataException() { } public InvalidDataException(String message) { super(message); } }
拋出異常的processorapp
AllowSkipProcessor.javafetch
package com.lyx.batch; import org.springframework.batch.item.ItemProcessor; public class AllowSkipProcessor implements ItemProcessor<People, PeopleDESC> { public PeopleDESC process(People item) throws Exception { System.out.println("process people desc"); if ("lyx".equals(item.getFirstName())) { System.out.println("skip invalid data!!!!!!!!!!!!!!!!!!!!!!!!"); throw new InvalidDataException("skip this data!!!!!!!!!!!!"); } return new PeopleDESC(item.getLastName(), item.getFirstName(), Thread .currentThread().getName()); } }
運行結果:
process people desc
skip invalid data!!!!!!!!!!!!!!!!!!!!!!!!
process people desc
skip invalid data!!!!!!!!!!!!!!!!!!!!!!!!
任務正常完成
程序運行時間: 7526ms
我在數據庫中加了兩條數據,這兩條數據符合拋出異常的條件,當拋出異常後,沒有致使批處理的失敗,最終成功。
關於SkipPolicy的用法:http://www.cnblogs.com/gulvzhe/archive/2012/01/02/2309690.html
===================END===================