Class MongoItemWriter<T>

All Implemented Interfaces:
ItemWriter<T>, org.springframework.beans.factory.InitializingBean

public class MongoItemWriter<T> extends Object implements ItemWriter<T>, org.springframework.beans.factory.InitializingBean

A ItemWriter implementation that writes to a MongoDB store using an implementation of Spring Data's MongoOperations. Since MongoDB is not a transactional store, a best effort is made to persist written data at the last moment, yet still honor job status contracts. No attempt to roll back is made if an error occurs during writing.

This writer is thread-safe once all properties are set (normal singleton behavior) so it can be used in multiple concurrent transactions.

Michael Minella, Parikshit Dutta, Mahmoud Ben Hassine
  • Constructor Details

    • MongoItemWriter

      public MongoItemWriter()
  • Method Details

    • setDelete

      @Deprecated(since="5.1", forRemoval=true) public void setDelete(boolean delete)
      Deprecated, for removal: This API element is subject to removal in a future version.
      use setMode(Mode) instead. Scheduled for removal in v5.3 or later.
      Indicates if the items being passed to the writer are to be saved or removed from the data store. If set to false (default), the items will be saved or update using MongoItemWriter.Mode.UPSERT. If set to true, then items will be removed.
      delete - removal indicator
    • setMode

      public void setMode(MongoItemWriter.Mode mode)
      Set the operating MongoItemWriter.Mode to be applied by this writer. Defaults to MongoItemWriter.Mode.UPSERT.
      mode - the mode to be used.
    • getMode

      public MongoItemWriter.Mode getMode()
      Get the operating MongoItemWriter.Mode of the item writer.
      the operating mode
    • setTemplate

      public void setTemplate( template)
      Set the MongoOperations to be used to save items to be written.
      template - the template implementation to be used.
    • getTemplate

      protected getTemplate()
      Get the MongoOperations to be used to save items to be written. This can be called by a subclass if necessary.
      template the template implementation to be used.
    • setCollection

      public void setCollection(String collection)
      Set the name of the Mongo collection to be written to.
      collection - the name of the collection.
    • getCollection

      public String getCollection()
      Get the Mongo collection name.
      the collection name
    • write

      public void write(Chunk<? extends T> chunk) throws Exception
      If a transaction is active, buffer items to be written just before commit. Otherwise write items using the provided template.
      Specified by:
      write in interface ItemWriter<T>
      chunk - of items to be written. Must not be null.
      Exception - if there are errors. The framework will catch the exception and convert or rethrow it as appropriate.
      See Also:
    • doWrite

      protected void doWrite(Chunk<? extends T> chunk)
      Performs the actual write to the store via the template. This can be overridden by a subclass if necessary.
      chunk - the chunk of items to be persisted.
    • afterPropertiesSet

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