Class MongoItemWriter<T>
java.lang.Object
org.springframework.batch.item.data.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.
- Author:
- Michael Minella, Parikshit Dutta, Mahmoud Ben Hassine
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
protected void
Performs the actual write to the store via the template.protected org.springframework.data.mongodb.core.MongoOperations
Get theMongoOperations
to be used to save items to be written.void
setCollection
(String collection) Set the name of the Mongo collection to be written to.void
setDelete
(boolean delete) Indicates if the items being passed to the writer are to be saved or removed from the data store.void
setTemplate
(org.springframework.data.mongodb.core.MongoOperations template) Set theMongoOperations
to be used to save items to be written.void
If a transaction is active, buffer items to be written just before commit.
-
Constructor Details
-
MongoItemWriter
public MongoItemWriter()
-
-
Method Details
-
setDelete
public void setDelete(boolean delete) 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. If set to true, the items will be removed.- Parameters:
delete
- removal indicator
-
setTemplate
public void setTemplate(org.springframework.data.mongodb.core.MongoOperations template) Set theMongoOperations
to 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 theMongoOperations
to 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.
-
write
If a transaction is active, buffer items to be written just before commit. Otherwise write items using the provided template.- Specified by:
write
in 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
Performs the actual write to the store via the template. This can be overridden by a subclass if necessary.- Parameters:
chunk
- the chunk of items to be persisted.
-
afterPropertiesSet
- Specified by:
afterPropertiesSet
in interfaceorg.springframework.beans.factory.InitializingBean
- Throws:
Exception
-