重试不使用 Spring 批处理 Java 配置
Retry not working with Spring Batch with Java Config
我有 Spring 批处理作业,配置如下:
@Bean
public Job myJob(Step step1, Step step2, Step step3) {
return jobs.get("myJob").start(step1).next(step2).next(step3).build();
}
@Bean
public Step step1(ItemReader<String> myReader,
ItemProcessor<String, String> myProcessor,
ItemWriter<String> myWriter) {
return steps.get("step1").<String, String>chunk(1)
.reader(myReader)
.faultTolerant().retryLimit(3).retry(MyException.class)
.processor(myProcessor)
.writer(myWriter)
.build();
}
@Bean
@StepScope
public MyReader myReader() {
return new MyReader();
}
@Bean
public MyProcessor myProcessor() {
return new MyProcessor();
}
@Bean
public MyWriter myWriter() {
return new MyWriter();
}
当 MyReader class 抛出 MyException 时,它将停止作业的执行而不重试以下堆栈跟踪:
2019-05-16 14:45:09.460 ERROR 22485 --- [ main] o.s.batch.core.step.AbstractStep : Encountered an error executing step step1 in job myJob
org.springframework.batch.core.step.skip.NonSkippableReadException: Non-skippable exception during read
at org.springframework.batch.core.step.item.FaultTolerantChunkProvider.read(FaultTolerantChunkProvider.java:105) ~[spring-batch-core-4.0.1.RELEASE.jar:4.0.1.RELEASE]
at org.springframework.batch.core.step.item.SimpleChunkProvider.doInIteration(SimpleChunkProvider.java:116) ~[spring-batch-core-4.0.1.RELEASE.jar:4.0.1.RELEASE]
从 2.2.0 开始,重试功能似乎已从 Spring 批处理中移除。它现在是新库的一部分,Spring 重试。
https://docs-stage.spring.io/spring-batch/docs/current/reference/html/retry.html#retry
使用声明性重试处理此问题的步骤:
第 1 步:
在申请中包含 @EnableRetry
Spet 2:
基于https://github.com/spring-projects/spring-retry#additional-dependencies
添加aop starter
Gradle
运行时间('org.springframework.boot:spring-boot-starter-aop')
Maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<scope>runtime</scope>
</dependency>
Spet 3:
在 MyReader
中包含 @Retryable
@Override
@Retryable(include = { MyException.class }, maxAttempts = 5)
public String read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
示例代码签入github:https://github.com/atulkulkarni18/spring-batch-reader-retry
示例输出:
MyReader : 0
MyProcessor : 0
MyWriter : [0]
MyReader : 1
MyProcessor : 1
MyWriter : [1]
MyReader : 2
MyProcessor : 2
MyWriter : [2]
MyReader : 3
****
MyReader : 3
****
MyReader : 3
****
MyReader : 3
MyProcessor : 3
MyWriter : [3]
MyReader : 4
MyProcessor : 4
MyWriter : [4]
MyReader : 5
When MyReader class throws MyException it is stopping the execution of the job without retrying
重试策略未应用于项目 reader。因此,即使您将异常声明为可重试并且该异常是从 reader 中抛出的,也不会调用重试策略。
重试策略仅适用于处理器和写入器。
我有 Spring 批处理作业,配置如下:
@Bean
public Job myJob(Step step1, Step step2, Step step3) {
return jobs.get("myJob").start(step1).next(step2).next(step3).build();
}
@Bean
public Step step1(ItemReader<String> myReader,
ItemProcessor<String, String> myProcessor,
ItemWriter<String> myWriter) {
return steps.get("step1").<String, String>chunk(1)
.reader(myReader)
.faultTolerant().retryLimit(3).retry(MyException.class)
.processor(myProcessor)
.writer(myWriter)
.build();
}
@Bean
@StepScope
public MyReader myReader() {
return new MyReader();
}
@Bean
public MyProcessor myProcessor() {
return new MyProcessor();
}
@Bean
public MyWriter myWriter() {
return new MyWriter();
}
当 MyReader class 抛出 MyException 时,它将停止作业的执行而不重试以下堆栈跟踪:
2019-05-16 14:45:09.460 ERROR 22485 --- [ main] o.s.batch.core.step.AbstractStep : Encountered an error executing step step1 in job myJob
org.springframework.batch.core.step.skip.NonSkippableReadException: Non-skippable exception during read
at org.springframework.batch.core.step.item.FaultTolerantChunkProvider.read(FaultTolerantChunkProvider.java:105) ~[spring-batch-core-4.0.1.RELEASE.jar:4.0.1.RELEASE]
at org.springframework.batch.core.step.item.SimpleChunkProvider.doInIteration(SimpleChunkProvider.java:116) ~[spring-batch-core-4.0.1.RELEASE.jar:4.0.1.RELEASE]
从 2.2.0 开始,重试功能似乎已从 Spring 批处理中移除。它现在是新库的一部分,Spring 重试。 https://docs-stage.spring.io/spring-batch/docs/current/reference/html/retry.html#retry
使用声明性重试处理此问题的步骤:
第 1 步:
在申请中包含 @EnableRetry
Spet 2: 基于https://github.com/spring-projects/spring-retry#additional-dependencies
添加aop starterGradle 运行时间('org.springframework.boot:spring-boot-starter-aop')
Maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<scope>runtime</scope>
</dependency>
Spet 3:
在 MyReader
@Retryable
@Override
@Retryable(include = { MyException.class }, maxAttempts = 5)
public String read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
示例代码签入github:https://github.com/atulkulkarni18/spring-batch-reader-retry
示例输出:
MyReader : 0
MyProcessor : 0
MyWriter : [0]
MyReader : 1
MyProcessor : 1
MyWriter : [1]
MyReader : 2
MyProcessor : 2
MyWriter : [2]
MyReader : 3
****
MyReader : 3
****
MyReader : 3
****
MyReader : 3
MyProcessor : 3
MyWriter : [3]
MyReader : 4
MyProcessor : 4
MyWriter : [4]
MyReader : 5
When MyReader class throws MyException it is stopping the execution of the job without retrying
重试策略未应用于项目 reader。因此,即使您将异常声明为可重试并且该异常是从 reader 中抛出的,也不会调用重试策略。
重试策略仅适用于处理器和写入器。