sachin
sachin

Reputation: 23

Spring Batch: How to combine multiple task or get 2 task in single

I am bit puzzled here, I need to do a task similar to the following scenario with Spring Batch

  1. Read Person from repository ==> I can use RepositoryItemReader
  2. (a) Generate CSV file (FlatFileItemWriter) and (b) save CSV file in DB with the generated date (I can use RepositoryItemWriter)

But here I am struggling to understand how I can give generated CSV file output of 2a to save in DB 2b. Consider CSV File has approx 1000+ Person Data which are processed for a single day.

is it possible to merge 2a & 2b? I thought about CompositeItemWriter but as here we are combining 1000+ employee in CSV file so it won't work.

Upvotes: 2

Views: 2140

Answers (1)

Mahmoud Ben Hassine
Mahmoud Ben Hassine

Reputation: 31600

Using a CompositeItemWriter won't work as you will be trying to write an incomplete file to the database for each chunk..

I would not merge 2a and 2b. Make each step do one thing (and do it well):

  • Step 1 (chunk-oriented tasklet): read persons and generate the file
  • Step 2 (simple tasklet) : save the file in the database

Step 1 can use the job execution context to pass the name of the generated file to step 2. You can find an example in the Passing Data To Future Steps section. Moreover, with this setup, step 2 will not run if step 1 fails (which makes sense to me).

Upvotes: 2

Related Questions