Class MultiResourceItemWriter<T>
java.lang.Object
org.springframework.batch.item.ItemStreamSupport
org.springframework.batch.item.support.AbstractItemStreamItemWriter<T>
org.springframework.batch.item.file.MultiResourceItemWriter<T>
- Type Parameters:
T- item type
- All Implemented Interfaces:
ItemStream,ItemStreamWriter<T>,ItemWriter<T>
Wraps a
ResourceAwareItemWriterItemStream and creates a new output resource
when the count of items written in current resource exceeds
setItemCountLimitPerResource(int). Suffix creation can be customized with
setResourceSuffixCreator(ResourceSuffixCreator).
This writer will create an output file only when there are items to write, which means there would be no empty file created if no items are passed (for example when all items are filtered or skipped during the processing phase).
- Author:
- Robert Kasanicky, Mahmoud Ben Hassine, Henning Pƶttker
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()If any resources are needed for the stream to operate they need to be destroyed here.voidopen(ExecutionContext executionContext) Open the stream for the providedExecutionContext.voidsetDelegate(ResourceAwareItemWriterItemStream<? super T> delegate) Delegate used for actual writing of the output.voidsetItemCountLimitPerResource(int itemCountLimitPerResource) After this limit is exceeded the next chunk will be written into newly created resource.voidsetResource(org.springframework.core.io.Resource resource) Prototype for output resources.voidsetResourceSuffixCreator(ResourceSuffixCreator suffixCreator) Allows customization of the suffix of the created resources based on the index.voidsetSaveState(boolean saveState) Indicates that the state of the reader will be saved after each commit.voidupdate(ExecutionContext executionContext) Indicates that the execution context provided during open is about to be saved.voidProcess the supplied data element.Methods inherited from class org.springframework.batch.item.ItemStreamSupport
getExecutionContextKey, getName, setExecutionContextName, setName
-
Constructor Details
-
MultiResourceItemWriter
public MultiResourceItemWriter()
-
-
Method Details
-
write
Description copied from interface:ItemWriterProcess the supplied data element. Will not be called with any null items in normal operation.- Parameters:
items- 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.
-
setResourceSuffixCreator
Allows customization of the suffix of the created resources based on the index.- Parameters:
suffixCreator-ResourceSuffixCreatorto be used by the writer.
-
setItemCountLimitPerResource
public void setItemCountLimitPerResource(int itemCountLimitPerResource) After this limit is exceeded the next chunk will be written into newly created resource.- Parameters:
itemCountLimitPerResource- int item threshold used to determine when a new resource should be created.
-
setDelegate
Delegate used for actual writing of the output.- Parameters:
delegate-ResourceAwareItemWriterItemStreamthat will be used to write the output.
-
setResource
public void setResource(org.springframework.core.io.Resource resource) Prototype for output resources. Actual output files will be created in the same directory and use the same name as this prototype with appended suffix (according tosetResourceSuffixCreator(ResourceSuffixCreator).- Parameters:
resource- The prototype resource.
-
setSaveState
public void setSaveState(boolean saveState) Indicates that the state of the reader will be saved after each commit.- Parameters:
saveState- true the state is saved.
-
close
Description copied from interface:ItemStreamIf 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.- Throws:
ItemStreamException
-
open
Description copied from interface:ItemStreamOpen the stream for the providedExecutionContext.- Parameters:
executionContext- current step'sExecutionContext. Will be the executionContext from the last run of the step on a restart.- Throws:
ItemStreamException
-
update
Description copied from interface:ItemStreamIndicates 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.- Parameters:
executionContext- to be updated- Throws:
ItemStreamException
-