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
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.
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.
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).
You need to build up a FaultTolerantStepBuilder
using StepBuilder.faultTolerant
method.
return stepBuilder()
.chunk()
.faultTolerant()
.skip(MyException.class)
.skipLimit(100000)
.build()
@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()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With