You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've recently encountered the need for an adapter to use an existing ItemWriter<SimpleType> in a step that works with ComplexType items, where a ComplexType instance encapsulates a SimpleType one (imagine a ComplexType::getSimpleType getter available).
While PropertyExtractingDelegatingItemWriter seems to be a solution, I didn't find it type-safe due to its underlying dependency on the MethodInvoker. I also looked at #4672, but they don't seem to help my use case.
Taking inspiration from java.util.stream.Collectors.mapping(Function, Collector), I came up with a custom ItemWriter adapter for this purpose, statically imported as mapping(Function, ItemWriter), which allows writing a step definition like the following:
factory.get("step")
.<ComplexType, ComplexType>chunk(10)
.reader(/* an ItemReader<ComplexType> */)
.writer(mapping(ComplexType::getSimpleType, newSimpleTypeItemWriter()))
.build()
I heavily simplified the example to give a feeling about the core idea. Arguably, that example can also be solved with a processor:
factory.get("step")
.<ComplexType, SimpleType>chunk(10)
.reader(/* an ItemReader<ComplexType> */)
.processor(/* an ItemProcess<ComplexType, SimpleType> */)
.writer(newSimpleTypeItemWriter())
.build()
However, my real-life use case also involves a CompositeItemWriter where each delegate expects items of a different type, all encapsulated by ComplexType. Something like the following:
I've recently encountered the need for an adapter to use an existing
ItemWriter<SimpleType>
in a step that works withComplexType
items, where aComplexType
instance encapsulates aSimpleType
one (imagine aComplexType::getSimpleType
getter available).While
PropertyExtractingDelegatingItemWriter
seems to be a solution, I didn't find it type-safe due to its underlying dependency on theMethodInvoker
. I also looked at #4672, but they don't seem to help my use case.Taking inspiration from
java.util.stream.Collectors.mapping(Function, Collector)
, I came up with a customItemWriter
adapter for this purpose, statically imported asmapping(Function, ItemWriter)
, which allows writing a step definition like the following:I heavily simplified the example to give a feeling about the core idea. Arguably, that example can also be solved with a processor:
However, my real-life use case also involves a
CompositeItemWriter
where each delegate expects items of a different type, all encapsulated byComplexType
. Something like the following:Would Spring Batch be interested in introducing such an adapter, maybe as a static factory method under
ItemWriter
for better discoverability?If yes, I would be happy to provide a PR for it.
The text was updated successfully, but these errors were encountered: