Class MappingItemWriter<T,U>

java.lang.Object
org.springframework.batch.infrastructure.item.support.MappingItemWriter<T,U>
Type Parameters:
T - the type of the input items
U - type of items accepted by downstream item writer
All Implemented Interfaces:
ItemStream, ItemStreamWriter<T>, ItemWriter<T>

public class MappingItemWriter<T,U> extends Object implements ItemStreamWriter<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 Details

    • MappingItemWriter

      public MappingItemWriter(Function<? super T,? extends U> mapper, ItemWriter<? super U> downstream)
      Create a new MappingItemWriter.
      Parameters:
      mapper - the mapping function to apply to the input items
      downstream - the downstream item writer that accepts mapped items
  • Method Details

    • write

      public void write(Chunk<? extends T> chunk) throws Exception
      Description copied from interface: ItemWriter
      Process the supplied data element. Will not be called with any null items in normal operation.
      Specified by:
      write in interface ItemWriter<T>
      Parameters:
      chunk - of items to be written. Must not be null.
      Throws:
      Exception - if there are errors. The framework will catch the exception and convert or rethrow it as appropriate.
    • open

      public void open(ExecutionContext executionContext) throws ItemStreamException
      Description copied from interface: ItemStream
      Open the stream for the provided ExecutionContext.
      Specified by:
      open in interface ItemStream
      Parameters:
      executionContext - current step's ExecutionContext. Will be the executionContext from the last run of the step on a restart.
      Throws:
      ItemStreamException
    • update

      public void update(ExecutionContext executionContext) throws ItemStreamException
      Description copied from interface: ItemStream
      Indicates 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:
      update in interface ItemStream
      Parameters:
      executionContext - to be updated
      Throws:
      ItemStreamException
    • close

      public void close() throws ItemStreamException
      Description copied from interface: ItemStream
      If 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:
      close in interface ItemStream
      Throws:
      ItemStreamException