Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skippable exception classes for Spring Batch with java based configuration

I configure a step in XML like this:

<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>

In the java configuration I use a StepBuilder like this:

@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();
}

But I could not find a way to configure the skippable exception classes

like image 882
Amer A. Avatar asked Jun 15 '14 20:06

Amer A.


People also ask

Can we skip processor in Spring Batch?

Using Custom SkipPolicy Sometimes we may need a more sophisticated skip-checking mechanism. For that purpose, Spring Batch framework provides the SkipPolicy interface. We can then provide our own implementation of skip logic and plug it into our step definition.

How does Spring Batch handle exceptions?

By default , if there's an uncaught exception when processing the job, spring batch will stop the job. If the job is restarted with the same job parameters, it will pick up where it left off. The way it knows where the job status is by checking the job repository where it saves all the spring batch job status.

What is skip limit in Spring Batch?

Define a skip-limit on your chunk element to tell Spring how many items can be skipped before the job fails (you might handle a few invalid records, but if you have too many then the input data might be invalid).


2 Answers

You need to build up a FaultTolerantStepBuilder using StepBuilder.faultTolerant method.

return stepBuilder()
  .chunk()
  .faultTolerant()
  .skip(MyException.class)
  .skipLimit(100000)
.build()
like image 118
Luca Basso Ricci Avatar answered Oct 02 '22 12:10

Luca Basso Ricci


@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() 
}
like image 39
Mahesh Avatar answered Oct 02 '22 13:10

Mahesh