如何使用 Spring Batch Annotation 编写 retryable-exception-类?

How to write retryable-exception-classes using Spring Batch Annotation?

我们目前正在从基于 Spring Batch XML 的应用程序迁移到最新的 Spring Boot 2.2.6.RELEASE 版本应用程序。

我有下面的 XML 片段,我想将其转换为基于注释的作业。当我浏览 https://docs.spring.io/spring-batch/docs/current/reference/html/step.html#taskletStep 时,我真的很难找到这些选项。

<batch:job id="myJob">
    <batch:step id="step1">
        <batch:tasklet>
            <batch:chunk reader="reader" writer="writer" commit-interval="100" retry-limit="3" skip-limit="3">
                <batch:retryable-exception-classes>
                    <batch:exclude class="org.springframework.dao.PessimisticLockingFailureException"/>
                </batch:retryable-exception-classes>
                <batch:skippable-exception-classes>
                    <batch:include class="org.springframework.dao.DeadlockLoserDataAccessException"/>
                </batch:skippable-exception-classes>
            </batch:chunk>
        </batch:tasklet>
    </batch:step>
</batch:job>

另一个片段:

<bean id="retryPolicy" class="org.springframework.retry.policy.ExceptionClassifierRetryPolicy">
    <property name="policyMap">
        <map>
            <entry key="org.springframework.dao.ConcurrencyFailureException">
                <bean class="org.springframework.batch.retry.policy.SimplreRetryPolicy">
                    <property name="maxAttempts" value="3" />
                </bean>
            </entry>
            <entry key="org.springframework.dao.DeadlockLoserDataAccessException">
                <bean class="org.springframework.batch.retry.policy.SimplreRetryPolicy">
                    <property name="maxAttempts" value="5" />
                </bean>
            </entry>
        </map>
    </property>
</bean>

每个页面的顶部都有一个切换开关,可让您选择要在代码示例中看到的配置样式(XML 或 Java):

现在 retryable exceptions section,这里是带有 XML 配置的代码:

Java 配置中的等价物:

如果要提供自定义重试策略,可以使用FaultTolerantStepBuilder#retryPolicy