For your use case, I would encapsulate A and B in a wrapper type, such AB:
class AB {
private A originalItem;
private B transformedItem;
}
With that, you would have: ItemReader<A>
, ItemProcessor<A, AB>
and ItemWriter<AB>
. The processor creates instances of AB in which it keeps a reference to the original item. The writer can then get access to both types and delegate to the JdbcBatchItemReader<B>
and SimpleMailMessageItemWriter<A>
as needed, something like:
class ABItemWriter implements ItemWriter<AB> {
private JdbcBatchItemWriter<B> jdbcBatchItemWriter;
private SimpleMailMessageItemWriter mailMessageItemWriter;
// constructor with delegates
@Override
public void write(List<? extends AB> items) throws Exception {
jdbcBatchItemWriter.write(getBs(items));
mailMessageItemWriter.write(getAs(items)); // this would not be called if the jdbc writer fails
}
}
The methods getAs
and getBs
would extract items of type A/B from AB. Encapsulation for the win! BTW, a Java record is a good option for type AB.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…