Reputation: 1
I am new to Spring batch and currently working on a new batch job. My batch job:
Currently I am using CompositeItemWriter to write 5 files and it worked.
<bean id="ComapanyWriter"
class="org.springframework.batch.item.support.CompositeItemWriter">
<property name="delegates">
<list>
<ref bean="CompanyAWriter" />
<ref bean="CompanyBWriter" />
<ref bean="CompanyCWriter" />
<ref bean="CompanyDWriter" />
<ref bean="ResignedEmpWriter" />
</list>
</property>
</bean>
However, I realized that if the reader retrieved 50k records from database, each writer will loop all the records, search for their respective company employee and write into file, which total is 5 x 50k times and redundant. So, I worried about the performance.
Some solution that I can think of:
Please advice me how can I achieve the above method or how to achieve the batch job goal.
Upvotes: 0
Views: 3881
Reputation: 31590
ClassifierCompositeItemWriter
is the way to go. The Classifier
will classify items and pass them to the assigned writer. This way, each writer will receive only items of the class it is assigned to and not loop over all the items of the chunk.
You can find an example of how to use the ClassifierCompositeItemWriter
here: https://github.com/spring-projects/spring-batch/blob/master/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/ClassifierCompositeItemWriterTests.java
There is a similar question to this one, I'm adding it here for reference: Read flat file and write to multiple writers which will write different objects.
Hope this helps.
Upvotes: 1