I have found many examples to use multiple writers in this forum. Most, if not all, of the answers focus on CompositeItemWriter and ClassifierItemWriter.
Business Need: Read a single line from an input file. This line will contain multiple fields (over 50) which need to be written to their own database tables (in theory represent different classes).
----- claimwriter(write to claim table)
/
/
claimlineitemprocessor -----
\
\
----- pharmacywriter(write to pharmacy table)
I have used a fieldset mapper to create the object representing the claim line (ClaimLine). Most of the fields are a simple mapping to the data in the file, but a few need to have their format changed or related field mapping logic.
Basic item writer code looks like this:
@SuppressWarnings({ "unchecked", "rawtypes" })
@Bean
public ItemWriter<ClaimLine> writer() {
CompositeItemWriter<ClaimLine> cWriter = new CompositeItemWriter<ClaimLine>();
JdbcBatchItemWriter claimWriter = new JdbcBatchItemWriter();
claimWriter.setItemSqlParameterSourceProvider(new ClaimItemSqlParameterSourceProvider());
claimWriter.setSql( // would like to insert into pharmacy table);
claimWriter.setDataSource(dataSource);
claimWriter.afterPropertiesSet();
JdbcBatchItemWriter pharmacyWriter = new JdbcBatchItemWriter();
pharmacyWriter.setItemSqlParameterSourceProvider(new PharmacyItemSqlParameterSourceProvider());
pharmacyWriter.setSql( // would like to insert into pharmacy table);
pharmacyWriter.setDataSource(dataSource);
pharmacyWriter.afterPropertiesSet();
List<ItemWriter<? super ClaimLine>> mWriter = new ArrayList<ItemWriter<? super ClaimLine>>();
mWriter.add(claimWriter);
mWriter.add(pharmacyWriter);
cWriter.setDelegates(mWriter);
// other code
return cWriter;
};
When creating the custom source providers, each of them seem to expect because that is the class that has already been mapped to the input line and contain the values I would like to send to the respective tables.
This is basically where I am now stuck thinking I cannot use a CompositeItemWriter because I am trying to transform a single object into two different ones. And ClassifierCompositeItemWriter works like a router and sends it down a path specific to a condition, which is not what I want to do.
For reference, I tried doing something similar with Spring Integration and also hit a similar roadblock.
Any help is appreciated.