Tarun Sapra
Tarun Sapra

Reputation: 1901

Spring Batch output format of the file?

In spring batch samples, there are lot of examples of copying data from csv format to db table, db table to csv etc. I wanted to know is there a way in which the user can select a particular job for example copy data from a csv file and also specify the destination format i.e. excel, db , pdf. Thus the destination format of the job is dependent on the user input.?

Thanks! Tarun Sapra

Upvotes: 0

Views: 1698

Answers (1)

Michael Minella
Michael Minella

Reputation: 21503

Given that Spring Batch is a batch framework that does not deal with user input, I'm not sure you're working with the right scenario here. However, there are a few different options here:

  1. Use a job parameter and a decision step to direct to the correct combination of ItemReader/ItemWriter. The disadvantage of this is that you would need to define a step per implementation (one to handle PDF, one for Excel, etc), however since the choice would be done only once per run of the job it would have less of an impact on performance.

  2. A cleaner approach would still use a job parameter, however it would use the ClassifierCompositeItemWriter to delegate to the appropriate writer. This ItemWriter would be configured with one ItemWriter delegate per implementation type (PDF, Excel, etc) and would dictate to them based upon the implementation of the Classifier interface you configure. While this is a cleaner approach from a configuration standpoint, it has the disadvantage of evaluating each of the individual items for which ItemWriter to processs (it's a disadvantage assuming that the entire run is intended to send all of the items to the same ItemWriter per run).

  3. Finally, I would think that you should be able to, in theory, use Spring's bean factory concepts to inject an ItemWriter when the job starts. For the record, this is purely speculation and I have not actually tried this.

Michael T Minella
Author of Pro Spring Batch

Upvotes: 1

Related Questions