具有基于Java的配置的Spring Batch的可跳过异常类

时间:2014-06-15 20:27:58

标签: java spring spring-batch spring-java-config

我在XML中配置一个步骤:

<batch:step id="slaveStep">
        <batch:tasklet>
            <batch:chunk
                    reader="reader"
                    processor="processor"
                    writer="writer"
                    commit-interval="10"
                    skip-limit="100000">
                <batch:skippable-exception-classes>
                    <batch:include class="MyException"/>
                </batch:skippable-exception-classes>
            </batch:chunk>
        </batch:tasklet>
    </batch:step>

在java配置中,我使用这样的StepBuilder:

@Bean
public StepBuilder stepBuilder(String stepName)
{
    return new StepBuilder(stepName);
}

@Bean
Step slaveStep()
{
    return stepBuilder("slaveStep")
            .<Movie, Movie>chunk(10)
            .reader(reader(new HashMap<>()))
            .processor(processor())
            .writer(writer())
            .build();
}

但我找不到配置可跳过的异常类的方法

2 个答案:

答案 0 :(得分:22)

您需要使用FaultTolerantStepBuilder方法构建StepBuilder.faultTolerant

return stepBuilder()
  .chunk()
  .faultTolerant()
  .skip(MyException.class)
  .skipLimit(100000)
.build()

答案 1 :(得分:2)

@Configuration
@EnableBatchProcessing
@Import(DataConfig.class)
    public class SpringBatchConfig {
    ..................
    ..................
    @Autowired
    private StepBuilderFactory stepBuilders;

    @Bean
    public Step loadSlaveStep()
        return stepBuilders.get("slaveStep")()
       .chunk()
       .faultTolerant()
       .skip(MyException.class)
       .skipLimit(100000)
       .build() 
}