title: Spring Batch - Delegated Item Writer
body: <p>I currently have the following spring batch (v2.2.4) job which reads from a single datasource and then creates three distinct output formats. I have three steps each with a standard reader, processsor and writer bean. I'm using brackets below to denote which format each processor or writer is using. Each of the processors in this example return a SqlParameterSource object to the writer bean.</p>
  
  <pre><code>Job
      Step 1 - PolicyDetails
          R -&gt; P(pd) -&gt; W(pd)
      Step 2 - PolicyCharge
          R -&gt; P(pc) -&gt; W(pc)
      Step 3 - PolicyFund
          R -&gt; P(pf) -&gt; W(pf)
  </code></pre>
  
  <p>I don't like the fact that i'm reading the same data three times so I'm planning on using a Composite Processor in a new job. The square brackets denote the composite processors. I'm unsure what interface my new Writer (Wn) should implement since it will have to handle or delegate the writing of three different object types. </p>
  
  <pre><code>Option 1 Job
      Step 1
          R -&gt; [P(pd) -&gt; P(pc) -&gt; P(pf)] -&gt; Wn(pd,pc,pf)
  </code></pre>
  
  <p>I'm wondering is there an existing spring batch writer class that supports delegating based on different input types? </p>
  
  <p>EDIT</p>
  
  <p>I've defined this wrapper interface which each of the Processors in Option 1 which will return. </p>
  
  <pre><code>/**
   * This interface allows us to wrap a SqlParameterSource and link it to a specific HedgingTable. 
   * The ClassifierCompositeItemWriter will use the HedgingTable to route the SqlParameterSource to the correct writer.
   */
  public interface HedgingDataSqlParameterSource {
  
      /**
       * The specific table that the SqlParameterSource data should be written to.
       */
      HedgingTable getHedgingTable();
  
      /**
       * The name value data for the insertion to the database table.
       */
      SqlParameterSource getSQLParameterSource();
  }
  </code></pre>
  
  <p>I've read up on the ClassifierCompositeItemWriter, but i'm still unsure how i can filter on the getHedgingTable() value. Do i reuse the existing SubclassClassifier class or define my own custom classifier.</p>
  
  <p>EDIT 2</p>
  
  <p>My first attempt at a custom Classifier implementation, which wraps the SubclassClassifier.</p>
  
  <pre><code>/**
   * Return an ItemWriter for the provided HedgingDataSqlParameterSource based on reusing a SubclassClassifier
   * which maps the specific HedgingTable type to a ItemWriter.
   */
  public class HedgingTableClassifier implements Classifier&lt;HedgingDataSqlParameterSource, ItemWriter&lt;HedgingDataSqlParameterSource&gt;&gt; {
  
      private SubclassClassifier&lt;HedgingTable, ItemWriter&lt;HedgingDataSqlParameterSource&gt;&gt; subclassClassifier = null;
  
      public ItemWriter&lt;HedgingDataSqlParameterSource&gt; classify(HedgingDataSqlParameterSource classifiable) {
          HedgingTable table = classifiable.getHedgingTable();
          return subclassClassifier.classify(table);
      }
  
      public SubclassClassifier&lt;HedgingTable, ItemWriter&lt;HedgingDataSqlParameterSource&gt;&gt; getSubclassClassifier() {
          return subclassClassifier;
      }
  
      public void setSubclassClassifier(
              SubclassClassifier&lt;HedgingTable, ItemWriter&lt;HedgingDataSqlParameterSource&gt;&gt; subclassClassifier) {
          this.subclassClassifier = subclassClassifier;
      }
  }
  </code></pre>