如何在job.xml文件中为chunk配置Spring Batch RetryPolicy

我配置如下 . 但是获得例外(见下文),请帮助 . 这样做的目的是实现重启失败的步骤 .

======================================== retryPolicyJob.xml

<batch:job id="retryPolicyJob" job-repository="jobRepository">
    <batch:step id="retryPolicyStep">
        <batch:tasklet>
            <batch:chunk reader="ConnectionRetryReader" writer="CustomItemWriter" commit-interval="100"
                retry-policy="retryPolicy" />
        </batch:tasklet>
    </batch:step>
</batch:job>

<bean id="ConnectionRetryReader" class="spring.batch.examples.retry.ConnectionRetryReader"/>
<bean id="CustomItemWriter" class="spring.batch.examples.retry.CustomItemWriter"/>

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

============================================= Reader类代码:

public class ConnectionRetryReader<T> implements ItemReader<T>, ItemStream {

    private int retryCount = 0;
    private T t;
    int currentIndex = 0;
    private static final String CURRENT_INDEX = "current.index";

    public T read() throws Exception, UnexpectedInputException, ParseException,
            NonTransientResourceException, ConnectionFailureException,
            ResourceNotAvailableException {
        retryCount++;
        System.out.println("In Retry " + retryCount);
        if (retryCount == 2) {
            System.out.println("throwing ConcurrencyFailureException");
            throw new org.springframework.dao.ConcurrencyFailureException(
                    "ConcurrencyFailureException thrown", new Exception());
        }
        System.out.println("throwing DeadlockLoserDataAccessException");
        throw new org.springframework.dao.DeadlockLoserDataAccessException(
                "DeadlockLoserDataAccessException thrown", new Exception());
    }

    public void open(ExecutionContext executionContext)
            throws ItemStreamException {
        if (executionContext.containsKey(CURRENT_INDEX)) {
            currentIndex = new Long(executionContext.getLong(CURRENT_INDEX))
                    .intValue();
        } else {
            currentIndex = 0;
        }
    }

    public void update(ExecutionContext executionContext)
            throws ItemStreamException {
        executionContext.putLong(CURRENT_INDEX,
                new Long(currentIndex).longValue());

    }

    public void close() throws ItemStreamException {
        // TODO Auto-generated method stub

    }

}

================================================== ==异常消息:

17:22:40,140  INFO main SimpleJobLauncher:179 - No TaskExecutor has been set, defaulting to synchronous executor.
17:22:40,546  INFO main SimpleJobLauncher:118 - Job: [FlowJob: [name=retryPolicyJob]] launched with the following parameters: [{}]
17:22:40,577  INFO main SimpleStepHandler:133 - Executing step: [retryPolicyStep]
In Retry 1
throwing DeadlockLoserDataAccessException
17:22:40,640 ERROR main AbstractStep:212 - Encountered an error executing the step
org.springframework.batch.core.step.skip.NonSkippableReadException: Non-skippable exception during read
    at org.springframework.batch.core.step.item.FaultTolerantChunkProvider.read(FaultTolerantChunkProvider.java:104)
    at org.springframework.batch.core.step.item.SimpleChunkProvider$1.doInIteration(SimpleChunkProvider.java:108)
    at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:367)
    at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:214)
    at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:143)
    at org.springframework.batch.core.step.item.SimpleChunkProvider.provide(SimpleChunkProvider.java:103)
    at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:68)
    at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:386)
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:130)
    at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:264)
    at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:76)
    at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:367)
    at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:214)
    at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:143)
    at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:250)
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:195)
    at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:135)
    at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:61)
    at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:60)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:144)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:124)
    at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:135)
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:281)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:120)
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:48)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:114)
    at org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:349)
    at org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:574)
    at spring.batch.examples.retry.RetryTestCommandLine.main(RetryTestCommandLine.java:9)
Caused by: org.springframework.dao.DeadlockLoserDataAccessException: DeadlockLoserDataAccessException thrown; nested exception is java.lang.Exception
    at spring.batch.examples.retry.ConnectionRetryReader.read(ConnectionRetryReader.java:30)
    at org.springframework.batch.core.step.item.SimpleChunkProvider.doRead(SimpleChunkProvider.java:90)
    at org.springframework.batch.core.step.item.FaultTolerantChunkProvider.read(FaultTolerantChunkProvider.java:87)
    ... 28 more
Caused by: java.lang.Exception
    at spring.batch.examples.retry.ConnectionRetryReader.read(ConnectionRetryReader.java:31)
    ... 30 more
17:22:40,655  INFO main SimpleJobLauncher:121 - Job: [FlowJob: [name=retryPolicyJob]] completed with the following parameters: [{}] and the following status: [FAILED]

回答(1)

2 years ago

您的reader类是显式调用异常类 . 这应该由retryPolicy自动管理 . 如果要捕获异常详细信息,请使用侦听器“MyRetryListener extends RetryListenerSupport” .

您需要为reader类定义类型,而不是使用“T” . 这是一个字节[],字符串,???

问候,戈登狄更斯