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
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。