0

My itemReader in spring batch transforms a csv record into Consumer object. This object contains a property recordType which is either insert or update.The file which my itemReader transforms may contain mix of update and insert records. Now while writing in my itemWriter I need to transform the Consumer record either to InsertConsumerRecordDTO or UpdateConsumerRecordDTO based on the recordType property. This means I need either UpdateCustomerItemWriter or InsertCustomerItemWriter at run time .In short, the requirement I have is

if(record has insert)
 Transform `Consumer` object to `InsertCustomerDTO`
 use `InsertCustomerItemWriter` and make a REST call;
else
   Transform `Consumer` object to `UpdateCustomerDTO`
   use `UpdateCustomerItemWriter` and make a REST call

Is it possible to achieve this functionality with spring batch?

  • You can use a `ClassifierCompositeItemWriter` for that. The idea is to classify items based on a given criteria (the record type in your case) and to delegate the the write operation to the corresponding item writer, see https://stackoverflow.com/questions/53501152/how-to-use-classifier-with-classifiercompositeitemwriter. – Mahmoud Ben Hassine Feb 28 '23 at 09:00

1 Answers1

0

Each chunk-oriented step in Spring Batch can only have single ItemWriter.

You can implement a custom ItemWriter that accepts a mixed List (Spring Batch 4) or Chunk (Spring Batch 5) of InsertConsumerRecordDTO and UpdateConsumerRecordDTO that performs the right action for each, respectively.

If the two DTO classes should have a common base class, then you can use that as type for List/Chunk. Otherwise, you can use Object.

Henning
  • 3,055
  • 6
  • 30