服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - SpringBatch跳过异常和限制方式

SpringBatch跳过异常和限制方式

2021-12-23 12:46篮球场上旧少年 Java教程

这篇文章主要介绍了SpringBatch跳过异常和限制方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

SpringBatch容错处理

1. 案例说明

从DB中reader出1000条数据,chunk = 100,当第二个chunk出现NullPointerException或者StringIndexOutOfBoundsException异常。业务要求batch不终了,程序继续执行。

2. 跳过异常限制

下记有两种实现方法。

2.1 skip 和 skipLimit配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@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代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
@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 接口代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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还不存在的情况下,就会报错

?
1
2
3
<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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_52914285/article/details/110188852

延伸 · 阅读

精彩推荐