SpringBatch跳过异常和限制方式
作者:篮球场上旧少年
SpringBatch容错处理
1. 案例说明
从DB中reader出1000条数据,chunk = 100,当第二个chunk出现NullPointerException或者StringIndexOutOfBoundsException异常。业务要求batch不终了,程序继续执行。
2. 跳过异常限制
下记有两种实现方法。
2.1 skip 和 skipLimit配置
@Bean public Step job1step1() throws Throwable { return stepBuilderFactory .get(_JOB_STEP_NAME) .listener(_stepListener) .<Model1, Model2>chunk(_CHUNK_SIZE) .reader(reader()) .processor(processor()) .writer(writer()) .faultTolerant() .skipLimit(10) .skip(NullPointerException.class) .skip(StringIndexOutOfBoundsException.class) .build(); }
上记代码示例中的skipLimit方法限制最大跳过数,skip方法限制跳过的异常类型。
这样某条数据出现异常时,并不会终了step,而是跳过本条错误数据,继续处理下一条。
2.2 自定义跳过配置SkipPolicy 接口
SkipPolicy相对于skip更加灵活。
例:业务要求,userId = 110的用户出现上记两个异常时,程序终了。
step代码
@Bean public Step job1step1() throws Throwable { return stepBuilderFactory .get(_JOB_STEP_NAME) .listener(_stepListener) .<Model1, Model2>chunk(_CHUNK_SIZE) .reader(reader()) .processor(processor()) .writer(writer()) .faultTolerant() .skipPolicy(new SkipPolicyTask()) .build(); }
自定义SkipPolicy 接口代码
public class SkipPolicyTask implements SkipPolicy { private static final int MAX_SKIP_COUNT = 10; private static final int USER_ID= 110; @Override public boolean isSkipFlg(Throwable throwable, int skipCount) throws SkipLimitExceededException { if (throwable instanceof NullPointerException && skipCount < MAX_SKIP_COUNT) { return true; } if (throwable instanceof StringIndexOutOfBoundsException && skipCount < MAX_SKIP_COUNT ) { if(common.getUserId == INVALID_TX_AMOUNT_LIMIT) { return false; } else { return true; } } return false; } }
return flase 程序终了,retuen true 跳过异常。
SpringBatch 错误积累
1.如果nextStep在该JOB中还没有配置
也就是说nextStep还不存在的情况下,就会报错
<end on="EIXT WITH IMBALANCE" /> <next on="BALANCED" to="nextStep" /> <fail on="*" />
Caused by: java.lang.IllegalArgumentException: Missing state for [StateTransition: [state=bain_Job.bainToTableStep, pattern=BALANCED, next=bain_Job.trustAcctBatPayStep]]
at org.springframework.batch.core.job.flow.support.SimpleFlow.initializeTransitions(SimpleFlow.java:283) ~[spring-batch-core-3.0.0.RELEASE.jar:3.0.0.RELEASE]
at org.springframework.batch.core.job.flow.support.SimpleFlow.afterPropertiesSet(SimpleFlow.java:128) ~[spring-batch-core-3.0.0.RELEASE.jar:3.0.0.RELEASE]
at org.springframework.batch.core.configuration.xml.SimpleFlowFactoryBean.getObject(SimpleFlowFactoryBean.java:125) ~[spring-batch-core-3.0.0.RELEASE.jar:3.0.0.RELEASE]
at org.springframework.batch.core.configuration.xml.SimpleFlowFactoryBean.getObject(SimpleFlowFactoryBean.java:46) ~[spring-batch-core-3.0.0.RELEASE.jar:3.0.0.RELEASE]
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE]
... 42 common frames omitted
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。