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.
Job
Step 1 - PolicyDetails
R -> P(pd) -> W(pd)
Step 2 - PolicyCharge
R -> P(pc) -> W(pc)
Step 3 - PolicyFund
R -> P(pf) -> W(pf)
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.
Option 1 Job
Step 1
R -> [P(pd) -> P(pc) -> P(pf)] -> Wn(pd,pc,pf)
I'm wondering is there an existing spring batch writer class that supports delegating based on different input types?
EDIT
I've defined this wrapper interface which each of the Processors in Option 1 which will return.
/**
* 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();
}
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.
EDIT 2
My first attempt at a custom Classifier implementation, which wraps the SubclassClassifier.
/**
* 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<HedgingDataSqlParameterSource, ItemWriter<HedgingDataSqlParameterSource>> {
private SubclassClassifier<HedgingTable, ItemWriter<HedgingDataSqlParameterSource>> subclassClassifier = null;
public ItemWriter<HedgingDataSqlParameterSource> classify(HedgingDataSqlParameterSource classifiable) {
HedgingTable table = classifiable.getHedgingTable();
return subclassClassifier.classify(table);
}
public SubclassClassifier<HedgingTable, ItemWriter<HedgingDataSqlParameterSource>> getSubclassClassifier() {
return subclassClassifier;
}
public void setSubclassClassifier(
SubclassClassifier<HedgingTable, ItemWriter<HedgingDataSqlParameterSource>> subclassClassifier) {
this.subclassClassifier = subclassClassifier;
}
}