Spring-Batch How skip exceptions works for composite writers
Asked Answered
P

1

0

I am using Spring Batch and my step configuration is as below:

 @Bean
  public Step testStep(
      JdbcCursorItemReader<TestStep> testStageDataReader,
      TestStepProcessor testStepProcessor,
      CompositeItemWriter<Writer> testWriter,
      PlatformTransactionManager transactionManager,
      JobRepository jobRepository) {
    return stepBuilderFactory
        .get("TESTING")
        .<>chunk(100)
        .reader(testStageDataReader)
        .processor(testStepProcessor)
        .writer(testWriter)
        .faultTolerant()
        .skip(DataIntegrityViolationException.class)
        .skipLimit(1)
        .listener(new SkipTestListener())
        .transactionManager(transactionManager)
        .repository(jobRepository)
        .build();
  }

My composite item writer


 @Bean
  public CompositeItemWriter<Writer> testWriter(
      Writer1 writer1,
      Writer2 writer2,
      Writer3 writer3)
      throws Exception {
    List<ItemWriter<? super Writer>> writers = new ArrayList<>();
    writers.add(writer1);
    writers.add(writer2);
    writers.add(writer3);
    CompositeItemWriter<Writer> writers = new CompositeItemWriter<>();
    workingWellDailyMemberAggWriter.setDelegates(writers);
    workingWellDailyMemberAggWriter.afterPropertiesSet();
    return writers;
  }


Now, If there is a DataIntegrityViolationException on writer1 my skip listener is invoked where I do my logging and then control goes to next step

What I am looking for a way that control goes to the next writer which are currently get skipped

Papyrus answered 11/6, 2020 at 8:21 Comment(3)
don't throw an Exception. catch it and throw an object. But, if an error occurs during the processing of an item, the batch should go to the next item to process.Ideal
I have 3 writers that get called in sequential order. If there is an exception happens at writer 1, I want my writer 2 and 3 to be called. Right now, If I have 1 record and exception occurs then writer 2 and 3 are not called.Papyrus
ok. so extract those writers into a separate process. or add all the writing business logic into one single writer.Ideal
S
0

This type of orchestration needs to be done via your own composite ItemWriter. Spring Batch doesn't have an out of the box component that will handle exceptions within the logic of a single component like that.

Salema answered 15/6, 2020 at 13:58 Comment(1)
Could you please share more code details here? Please guide here too: #63296565Pythian

© 2022 - 2024 — McMap. All rights reserved.