Class SynchronizedItemStreamWriter<T>

java.lang.Object
org.springframework.batch.item.support.SynchronizedItemStreamWriter<T>
Type Parameters:
T - type of object being written
All Implemented Interfaces:
ItemStream, ItemStreamWriter<T>, ItemWriter<T>, org.springframework.beans.factory.InitializingBean

public class SynchronizedItemStreamWriter<T> extends Object implements ItemStreamWriter<T>, org.springframework.beans.factory.InitializingBean
An ItemStreamWriter decorator with a synchronized write() method.

This decorator is useful when using a non thread-safe item writer in a multi-threaded step. Typical delegate examples are the JsonFileItemWriter and StaxEventItemWriter.

It should be noted that synchronizing writes might introduce some performance degradation, so this decorator should be used wisely and only when necessary. For example, using a FlatFileItemWriter in a multi-threaded step does NOT require synchronizing writes, so using this decorator in such use case might be counter-productive.

Author:
Dimitrios Liapis, Mahmoud Ben Hassine
  • Constructor Details

    • SynchronizedItemStreamWriter

      public SynchronizedItemStreamWriter()
  • Method Details

    • setDelegate

      public void setDelegate(ItemStreamWriter<T> delegate)
      Set the delegate ItemStreamWriter.
      Parameters:
      delegate - the delegate to set
    • write

      public void write(Chunk<? extends T> items) throws Exception
      This method delegates to the write method of the delegate.
      Specified by:
      write in interface ItemWriter<T>
      Parameters:
      items - 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
    • afterPropertiesSet

      public void afterPropertiesSet() throws Exception
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
      Throws:
      Exception