title: Spring batch - write multiple files to multiple destinations
body: <p>I am new to Spring batch and currently working on a new batch job. My batch job:</p>
  
  <ol>
  <li>Reader: read records from database (i.e employees of 5 company)</li>
  <li>Processor: process the records</li>
  <li>5 Writers: filter and write the records into each file (i.e employee from company A will be written into company A file at Path A, employee from company B will be written into company B file at Path B), the last writer will write all the resigned employees from company A to company D. </li>
  <li>Tasklet: Save the files into database.</li>
  </ol>
  
  <p>Currently I am using CompositeItemWriter to write 5 files and it worked. </p>
  
  <pre><code>&lt;bean id="ComapanyWriter" 
      class="org.springframework.batch.item.support.CompositeItemWriter"&gt;
    &lt;property name="delegates"&gt;
      &lt;list&gt;
        &lt;ref bean="CompanyAWriter" /&gt;
        &lt;ref bean="CompanyBWriter" /&gt;
        &lt;ref bean="CompanyCWriter" /&gt;
        &lt;ref bean="CompanyDWriter" /&gt;
        &lt;ref bean="ResignedEmpWriter" /&gt;
      &lt;/list&gt;
    &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p>Some solution that I can think of:</p>
  
  <ol>
  <li>tried to remove the written records from the data pool (items) so the next writer will loop lesser data but seems like not allowed to do this? (I got this error <strong>java.lang.UnsupportedOperationException</strong>)</li>
  <li>split the records into 5 list (companyAList, companyBList, etc) for each writer to write but I am not sure how to achieve this</li>
  <li>split the records into 5 list in writer and write 5 files using 1 writer, but not sure it is feasible.</li>
  <li>Pass company ID as parameter so the job will process and write file dynamically based on the parameter. This method will need to trigger job 4 times for 4 company and it cannot process the resigned employees from all company. </li>
  <li>Now I am looking at ClassifierCompositeItemWriter, not sure whether it will help.</li>
  </ol>
  
  <p>Please advice me how can I achieve the above method or how to achieve the batch job goal.</p>