SpringBatch Skipping Exceptions and Restrictions
- 2021-11-10 09:36:50
- OfStack
SpringBatch Fault Tolerant Handling
1. Case description
From DB, reader gets 1000 pieces of data, chunk = 100, when NullPointerException or StringIndexOutOfBoundsException exception occurs in the second chunk. Business requirements batch will not end, and the program will continue to be executed.
2. Skip exception restrictions
There are two ways to realize it.
2.1 skip and skipLimit Configuration
@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();
}
The skipLimit method in the above code example limits the maximum number of skips, and the skip method limits the types of exceptions skipped.
In this way, when an exception occurs in a piece of data, it will not end step, but skip this wrong data and continue to process the next one.
2.2 Custom Skip Configuration of SkipPolicy Interface
SkipPolicy is more flexible than skip.
Example: Business requirements, userId = 110 users appear above two exceptions, the end of the program.
step code
@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();
}
Custom SkipPolicy interface code
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;
}
}
End of return flase program, retuen true skip exception.
SpringBatch error accumulation
1. If nextStep is not already configured in this JOB
That is to say, if nextStep does not exist, an error will be reported
<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