Python 官方文档:入门教程 => 点击学习
目录springBatch容错处理1. 案例说明2. 跳过异常限制SpringBatch 错误积累1.如果nextStep在该JOB中还没有配置SpringBatch容错处理 1.
从DB中reader出1000条数据,chunk = 100,当第二个chunk出现NullPointerException或者StringIndexOutOfBoundsException异常。业务要求batch不终了,程序继续执行。
下记有两种实现方法。
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 跳过异常。
也就是说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
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: SpringBatch跳过异常和限制方式
本文链接: https://lsjlt.com/news/134964.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0