user3379502
user3379502

Reputation: 321

writing multiple files (different content) using spring batch

I have a requirement to write multiple files using Spring Batch. The first file will be written based on the data from the database table. The second file will contain just the number of records written to the first file. How can I create the second file? I am not sure whether org.springframework.batch.item.file.MultiResourceItemWriter is an option for me as I think it will write multiple files based on the data it will write chunks of data in the multiple files. Correct me if I am wrong here.

Please do suggest some options with sample code if possible.

Upvotes: 0

Views: 3439

Answers (1)

Nenad Bozic
Nenad Bozic

Reputation: 3784

You have couple of options:

  1. You can use CompositeItemWriter which calls collection of item writers in defined order so you can define one item writer which will write records based on data from DB and second will count records and write that to another file.

  2. You can write data to a file in first step, finish whole file and save it somewhere, you can save counter of records if that is all you need to StepContext (common batch patterns and scroll to 11.8 Passing Data to Future Steps) and read in new Taskletcounter and save to new file.

If you want to go with option 1 which I think is right choice you can check this example of batch job configuration with CompositeItemWriter

Upvotes: 4

Related Questions