Class MongoItemWriter<T>
java.lang.Object
org.springframework.batch.infrastructure.item.data.MongoItemWriter<T>
- All Implemented Interfaces:
ItemWriter<T>
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.
- Author:
- Michael Minella, Parikshit Dutta, Mahmoud Ben Hassine, Stefano Cordio
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumOperation mode of the item writer. -
Constructor Summary
ConstructorsConstructorDescriptionMongoItemWriter(org.springframework.data.mongodb.core.MongoOperations template) Create a new instance ofMongoItemWriterwith the providedMongoOperationstemplate. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidPerforms the actual write to the store via the template.@Nullable StringGet the Mongo collection name.getMode()Get the operatingMongoItemWriter.Modeof the item writer.Get the list of primary keys associated with the document being written.protected org.springframework.data.mongodb.core.MongoOperationsGet theMongoOperationsto be used to save items to be written.voidsetCollection(@Nullable String collection) Set the name of the Mongo collection to be written to.voidsetMode(MongoItemWriter.Mode mode) Set the operatingMongoItemWriter.Modeto be applied by this writer.voidsetPrimaryKeys(List<String> primaryKeys) Set the primary keys to associate with the document being written.voidsetTemplate(org.springframework.data.mongodb.core.MongoOperations template) Set theMongoOperationsto be used to save items to be written.voidIf a transaction is active, buffer items to be written just before commit.
-
Constructor Details
-
MongoItemWriter
public MongoItemWriter(org.springframework.data.mongodb.core.MongoOperations template) Create a new instance ofMongoItemWriterwith the providedMongoOperationstemplate. The template is required.- Parameters:
template- the template implementation to be used. Must not be null.- Since:
- 6.0
-
-
Method Details
-
setMode
Set the operatingMongoItemWriter.Modeto be applied by this writer. Defaults toMongoItemWriter.Mode.UPSERT.- Parameters:
mode- the mode to be used.- Since:
- 5.1
-
getMode
Get the operatingMongoItemWriter.Modeof the item writer.- Returns:
- the operating mode
- Since:
- 5.1
-
setTemplate
public void setTemplate(org.springframework.data.mongodb.core.MongoOperations template) Set theMongoOperationsto be used to save items to be written.- Parameters:
template- the template implementation to be used.
-
getTemplate
protected org.springframework.data.mongodb.core.MongoOperations getTemplate()Get theMongoOperationsto be used to save items to be written. This can be called by a subclass if necessary.- Returns:
- template the template implementation to be used.
-
setCollection
Set the name of the Mongo collection to be written to.- Parameters:
collection- the name of the collection.
-
getCollection
Get the Mongo collection name.- Returns:
- the collection name
- Since:
- 5.1
-
setPrimaryKeys
-
getPrimaryKeys
-
write
If a transaction is active, buffer items to be written just before commit. Otherwise write items using the provided template.- 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.- See Also:
-
doWrite
-