In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "SpringBatch skips exception restrictions and custom skips configuration SkipPolicy interface methods". In daily operation, I believe many people have doubts about SpringBatch skips exception restrictions and custom skips configuration SkipPolicy interface methods. I have consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer your doubts about "SpringBatch skips exception restrictions and custom skips configuration SkipPolicy interface methods"! Next, please follow the small series to learn together!
directory
SpringBatch fault tolerant processing
1. case illustrates
2. Skip exception limits
SpringBatch error accumulation
1. If nextStep is not already configured in the JOB
SpringBatch Fault-tolerant Processing 1. case illustrates
1000 pieces of data from the DB reader, chunk = 100, when the second chunk has NullPointerException or StringIndexOutOfBoundsException. The business request batch is not terminated and the program continues to execute.
2. Skip exception limits
There are two ways to do this.
2.1 skip and skip limit configuration
@Bean public Step job1step1() throws Throwable { return stepBuilderFactory .get(_JOB_STEP_NAME) .listener(_stepListener) .chunk(_CHUNK_SIZE) .reader(reader()) .processor(processor()) .writer(writer()) .faultTolerant() .skipLimit(10) .skip(NullPointerException.class) .skip(StringIndexOutOfBoundsException.class) .build(); }
The skipLimit method in the code example above 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 certain piece of data, it will not end the step, but skip this piece of error data and continue to process the next piece.
2.2 Custom Skip Configuration SkipPolicy Interface
SkipPolicy is more flexible than skip.
For example: Business requirements, when the user with userId = 110 has two exceptions recorded above, the program ends.
Step code
@Bean public Step job1step1() throws Throwable { return stepBuilderFactory .get(_JOB_STEP_NAME) .listener(_stepListener) .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; } }
return flase program ends, retuen true skips exception.
SpringBatch error accumulation 1. If nextStep has not been configured in this JOB
In other words, if nextStep does not exist, it will report an error.
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
At this point, the study of "SpringBatch skipping exception restrictions and custom skipping methods for configuring SkipPolicy interfaces" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.