Class SynchronizedItemStreamReader<T>

java.lang.Object
org.springframework.batch.infrastructure.item.support.SynchronizedItemStreamReader<T>
Type Parameters:
T - type of object being read
All Implemented Interfaces:
ItemReader<T>, ItemStream, ItemStreamReader<T>

public class SynchronizedItemStreamReader<T> extends Object implements ItemStreamReader<T>
This is a simple ItemStreamReader decorator with a synchronized ItemReader.read() method - which makes a non-thread-safe ItemReader thread-safe.

However, if reprocessing an item is problematic, then using this will make a job not restartable.

Here is the motivation behind this class: https://stackoverflow.com/a/20002493/2910265

Since:
3.0.4
Author:
Matthew Ouyang, Mahmoud Ben Hassine
  • Constructor Details

    • SynchronizedItemStreamReader

      public SynchronizedItemStreamReader(ItemStreamReader<T> delegate)
      Create a new SynchronizedItemStreamReader with the given delegate.
      Parameters:
      delegate - the item reader to use as a delegate
      Since:
      6.0
  • Method Details

    • setDelegate

      public void setDelegate(ItemStreamReader<T> delegate)
    • read

      public @Nullable T read() throws Exception
      This delegates to the read method of the delegate
      Specified by:
      read in interface ItemReader<T>
      Returns:
      T the item to be processed or null if the data source is exhausted
      Throws:
      Exception - if an error occurs.
    • close

      public void close()
      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
    • open

      public void open(ExecutionContext executionContext)
      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.
    • update

      public void update(ExecutionContext executionContext)
      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