重试不使用FaultTolerantStepBuilder

时间:2016-12-06 13:15:57

标签: spring-batch

我正在尝试在我容易出错的项目阅读器中构建重试。我已经设置了一个小POC来测试一切是否正常,但是我无法重试。这是我做的:

@Configuration
@EnableBatchProcessing
public static class TestConfiguration {

    // other beans

    @Bean
    @Qualifier("importFullJob")
    public Job importFullJob(ItemReader itemReader) {
        TaskletStep mockStep = stepBuilderFactory.get("mockStep")
                .chunk(1)
                .faultTolerant()
                .retry(RestClientException.class)
                .retryLimit(10)
                .reader(itemReader)
                .processor(item -> "processed")
                .writer(items -> {
                })
                .build();

        return jobBuilderFactory.get("importFullJob").start(mockStep).build();
    }

    @Bean
    @JobScope
    public ItemReader itemReader() {
        return new ItemReader() {

            @Override
            public Object read() throws Exception {
                System.out.println("try read");

                throw new RestClientException("booom");
            }
        };
    }
}

我原本期待对ItemReader.read()进行10次调用,但我只得到一次调用和此异常:

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-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.step.item.SimpleChunkProvider$1.doInIteration(SimpleChunkProvider.java:116) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:374) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.step.item.SimpleChunkProvider.provide(SimpleChunkProvider.java:110) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:69) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133) ~[spring-tx-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:271) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:81) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:374) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:257) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:200) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:392) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.job.SimpleJob.doExecute(SimpleJob.java:135) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_72]
Caused by: org.springframework.web.client.RestClientException: buuuum
    at org.woezelmann.batch.starter.FullImportStarterIT$TestConfiguration$1.read(FullImportStarterIT.java:256) ~[test-classes/:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_72]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_72]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_72]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_72]
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302) ~[spring-aop-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) ~[spring-aop-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:133) ~[spring-aop-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:121) ~[spring-aop-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) ~[spring-aop-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at com.sun.proxy.$Proxy68.read(Unknown Source) ~[?:?]
    at org.springframework.batch.core.step.item.SimpleChunkProvider.doRead(SimpleChunkProvider.java:91) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.step.item.FaultTolerantChunkProvider.read(FaultTolerantChunkProvider.java:87) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    ... 22 more

我也试过

.retryPolicy(new AlwaysRetryPolicy())

但我也不工作。

我做错了什么还是这个错误?

编辑:

我试过了

.faultTolerant()
.skipPolicy(new AlwaysSkipItemSkipPolicy())

按预期工作。

1 个答案:

答案 0 :(得分:1)

< p>"我做错了什么或这是一个错误?"好像是个bug。奇怪的是,在写入时抛出异常似乎没问题。 < / p为H. <预><代码> @Bean public Job retrySample(){     return jobBuilderFactory.get(" retrySample")。start(mockStep())。build(); } @豆 public Step mockStep(){     return stepBuilderFactory.get(" mockStep")                 。< String,String> chunk(1)                 .reader(itemReader())                 .writer(itemWriter())                 .faultTolerant()                 .retry(Exception.class)                 .retryLimit(10)                 。建立(); } @豆 @工作范围 public ItemReader< String> itemReader(){     返回新的ItemReader< String>(){         @覆盖         public String read()throws Exception {             返回" dkafj&#34 ;;         }     }; } @豆 @工作范围 public ItemWriter< String> itemWriter(){     返回新的ItemWriter< String>(){         @覆盖         public void write(List<?extends String> items)throws Exception {             items.forEach(item - > {System.out.println(" got" + item);});             抛出新的例外(" booom");         }     }; } < /代码>< /预> < p为H.日志:其中/ p为H. <预><代码> 2016-12-08 16:04:09.618 INFO 9040 --- [main] o.s.b.c.l.support.SimpleJobLauncher:Job:[SimpleJob:[name = retrySample]]使用以下参数启动:[{runId = Job}] 2016-12-08 16:04:09.651 INFO 9040 --- [main] o.s.batch.core.job.SimpleStepHandler:执行步骤:[mockStep] gotdkafj gotdkafj gotdkafj gotdkafj gotdkafj gotdkafj gotdkafj gotdkafj gotdkafj gotdkafj 2016-12-08 16:04:09.707 ERROR 9040 --- [main] o.s.batch.core.step.AbstractStep:在job retrySample中遇到执行步骤mockStep的错误 org.springframework.retry.ExhaustedRetryException:在最后一次尝试恢复路径后重试耗尽,但异常不可跳过。嵌套异常是java.lang.Exception:booom     在org.springframework.batch.core.step.item.FaultTolerantChunkProcessor $ 5.recover(FaultTolerantChunkProcessor.java:403)〜[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] < /代码>< /预> < p>以下是Spring Batch的链接< a href =" https://jira.spring.io/browse/BATCH/?selectedTab = com.atlassian.jira.jira-projects-plugin:摘要面板" rel =" nofollow noreferrer"> JIRA< / a>< / p>