Class MappingItemWriter<T,U>
java.lang.Object
org.springframework.batch.infrastructure.item.support.MappingItemWriter<T,U>
- Type Parameters:
T- the type of the input itemsU- type of items accepted by downstream item writer
- All Implemented Interfaces:
ItemStream,ItemStreamWriter<T>,ItemWriter<T>
Adapts an
ItemWriter accepting items of type MappingItemWriter to one accepting items
of type MappingItemWriter by applying a mapping function to each item before writing.
Thread-safety is guaranteed as long as the downstream item writer is thread-safe, and
state management is honored with a downstream ItemStream item writer.
This adapter is most useful when used in combination with a
CompositeItemWriter,
where the mapping function in front of the downstream writer can be a getter of the
input item or a more complex transformation logic, effectively allowing deconstruction
patterns.
This adapter mimics the behavior of
Collectors.mapping(Function, java.util.stream.Collector).
- Since:
- 6.0
- Author:
- Stefano Cordio
-
Constructor Summary
ConstructorsConstructorDescriptionMappingItemWriter(Function<? super T, ? extends U> mapper, ItemWriter<? super U> downstream) Create a newMappingItemWriter. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()If any resources are needed for the stream to operate they need to be destroyed here.voidopen(ExecutionContext executionContext) Open the stream for the providedExecutionContext.voidupdate(ExecutionContext executionContext) Indicates that the execution context provided during open is about to be saved.voidProcess the supplied data element.
-
Constructor Details
-
MappingItemWriter
Create a newMappingItemWriter.- Parameters:
mapper- the mapping function to apply to the input itemsdownstream- the downstream item writer that accepts mapped items
-
-
Method Details
-
write
Description copied from interface:ItemWriterProcess the supplied data element. Will not be called with any null items in normal operation.- Specified by:
writein interfaceItemWriter<T>- Parameters:
chunk- of items to be written. Must not benull.- Throws:
Exception- if there are errors. The framework will catch the exception and convert or rethrow it as appropriate.
-
open
Description copied from interface:ItemStreamOpen the stream for the providedExecutionContext.- Specified by:
openin interfaceItemStream- Parameters:
executionContext- current step'sExecutionContext. Will be the executionContext from the last run of the step on a restart.- Throws:
ItemStreamException
-
update
Description copied from interface:ItemStreamIndicates that the execution context provided during open is about to be saved. If any state is remaining, but has not been put in the context, it should be added here.- Specified by:
updatein interfaceItemStream- Parameters:
executionContext- to be updated- Throws:
ItemStreamException
-
close
Description copied from interface:ItemStreamIf any resources are needed for the stream to operate they need to be destroyed here. Once this method has been called all other methods (except open) may throw an exception.- Specified by:
closein interfaceItemStream- Throws:
ItemStreamException
-