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

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: