您好,登錄后才能下訂單哦!
這篇文章主要介紹“SpringBatch跳過異常限制和自定義跳過配置SkipPolicy接口的方法”,在日常操作中,相信很多人在SpringBatch跳過異常限制和自定義跳過配置SkipPolicy接口的方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”SpringBatch跳過異常限制和自定義跳過配置SkipPolicy接口的方法”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
SpringBatch容錯處理
1. 案例說明
2. 跳過異常限制
SpringBatch 錯誤積累
1.如果nextStep在該JOB中還沒有配置
從DB中reader出1000條數(shù)據(jù),chunk = 100,當(dāng)?shù)诙€chunk出現(xiàn)NullPointerException或者StringIndexOutOfBoundsException異常。業(yè)務(wù)要求batch不終了,程序繼續(xù)執(zhí)行。
下記有兩種實現(xiàn)方法。
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方法限制最大跳過數(shù),skip方法限制跳過的異常類型。
這樣某條數(shù)據(jù)出現(xiàn)異常時,并不會終了step,而是跳過本條錯誤數(shù)據(jù),繼續(xù)處理下一條。
2.2 自定義跳過配置SkipPolicy 接口
SkipPolicy相對于skip更加靈活。
例:業(yè)務(wù)要求,userId = 110的用戶出現(xiàn)上記兩個異常時,程序終了。
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 跳過異常。
也就是說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
到此,關(guān)于“SpringBatch跳過異常限制和自定義跳過配置SkipPolicy接口的方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。