Class AbstractFileItemWriter<T>
java.lang.Object
org.springframework.batch.item.ItemStreamSupport
org.springframework.batch.item.support.AbstractItemStreamItemWriter<T>
org.springframework.batch.item.support.AbstractFileItemWriter<T>
- All Implemented Interfaces:
ResourceAwareItemWriterItemStream<T>,ItemStream,ItemStreamWriter<T>,ItemWriter<T>,org.springframework.beans.factory.InitializingBean
- Direct Known Subclasses:
FlatFileItemWriter,JsonFileItemWriter
public abstract class AbstractFileItemWriter<T>
extends AbstractItemStreamItemWriter<T>
implements ResourceAwareItemWriterItemStream<T>, org.springframework.beans.factory.InitializingBean
Base class for item writers that write data to a file or stream. This class provides
common features like restart, force sync, append etc. The location of the output file
is defined by a
Uses buffered writer to improve performance.
The implementation is not thread-safe.
WritableResource which must represent a writable file.Uses buffered writer to improve performance.
The implementation is not thread-safe.
- Since:
- 4.1
- Author:
- Waseem Malik, Tomas Slanina, Robert Kasanicky, Dave Syer, Michael Minella, Mahmoud Ben Hassine, Glenn Renfro, Remi Kaeffer, Elimelec Burghelea
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classEncapsulates the runtime state of the writer. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanstatic final Stringstatic final Stringstatic final booleanprotected Stringprotected static final org.apache.commons.logging.Logprotected @Nullable org.springframework.core.io.WritableResourceprotected booleanprotected AbstractFileItemWriter<T>.@Nullable OutputState -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()If any resources are needed for the stream to operate they need to be destroyed here.protected abstract StringWrite out a string of items followed by a "new line", where the format of the new line separator is determined by the underlying operating system.protected AbstractFileItemWriter<T>.OutputStatevoidopen(ExecutionContext executionContext) Initialize the reader.voidsetAppendAllowed(boolean append) Flag to indicate that the target file should be appended if it already exists.voidsetEncoding(String newEncoding) Sets encoding for output template.voidsetFooterCallback(FlatFileFooterCallback footerCallback) footerCallback will be called after writing the last item to file, but before the file is closed.voidsetForceSync(boolean forceSync) Flag to indicate that changes should be force-synced to disk on flush.voidsetHeaderCallback(FlatFileHeaderCallback headerCallback) headerCallback will be called before writing the first item to file.voidsetLineSeparator(String lineSeparator) Public setter for the line separator.voidsetResource(org.springframework.core.io.WritableResource resource) Setter for a writable resource.voidsetSaveState(boolean saveState) Set the flag indicating whether or not state should be saved in the providedExecutionContextduring theItemStreamcall to update.voidsetShouldDeleteIfEmpty(boolean shouldDeleteIfEmpty) Flag to indicate that the target file should be deleted if no lines have been written (other than header and footer) on close.voidsetShouldDeleteIfExists(boolean shouldDeleteIfExists) Flag to indicate that the target file should be deleted if it already exists, otherwise it will be created.voidsetTransactional(boolean transactional) Flag to indicate that writing to the buffer should be delayed if a transaction is active.voidupdate(ExecutionContext executionContext) Indicates that the execution context provided during open is about to be saved.voidWrites out a string followed by a "new line", where the format of the new line separator is determined by the underlying operating system.Methods inherited from class org.springframework.batch.item.ItemStreamSupport
getExecutionContextKey, getName, setExecutionContextName, setNameMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.beans.factory.InitializingBean
afterPropertiesSet
-
Field Details
-
DEFAULT_TRANSACTIONAL
public static final boolean DEFAULT_TRANSACTIONAL- See Also:
-
logger
protected static final org.apache.commons.logging.Log logger -
DEFAULT_LINE_SEPARATOR
-
DEFAULT_CHARSET
-
resource
protected @Nullable org.springframework.core.io.WritableResource resource -
state
-
shouldDeleteIfExists
protected boolean shouldDeleteIfExists -
lineSeparator
-
append
protected boolean append
-
-
Constructor Details
-
AbstractFileItemWriter
public AbstractFileItemWriter()
-
-
Method Details
-
setForceSync
public void setForceSync(boolean forceSync) Flag to indicate that changes should be force-synced to disk on flush. Defaults to false, which means that even with a local disk changes could be lost if the OS crashes in between a write and a cache flush. Setting to true may result in slower performance for usage patterns involving many frequent writes.- Parameters:
forceSync- the flag value to set
-
setLineSeparator
Public setter for the line separator. Defaults to the System property line.separator.- Parameters:
lineSeparator- the line separator to set
-
setResource
public void setResource(org.springframework.core.io.WritableResource resource) Setter for a writable resource. Represents a file that can be written.- Specified by:
setResourcein interfaceResourceAwareItemWriterItemStream<T>- Parameters:
resource- the resource to be written to
-
setEncoding
Sets encoding for output template.- Parameters:
newEncoding-Stringcontaining the encoding to be used for the writer.
-
setShouldDeleteIfExists
public void setShouldDeleteIfExists(boolean shouldDeleteIfExists) Flag to indicate that the target file should be deleted if it already exists, otherwise it will be created. Defaults to true, so no appending except on restart. If set to false andappendAllowedis also false then there will be an exception when the stream is opened to prevent existing data being potentially corrupted.- Parameters:
shouldDeleteIfExists- the flag value to set
-
setAppendAllowed
public void setAppendAllowed(boolean append) Flag to indicate that the target file should be appended if it already exists. If this flag is set then the flagshouldDeleteIfExistsis automatically set to false, so that flag should not be set explicitly. Defaults value is false.- Parameters:
append- the flag value to set
-
setShouldDeleteIfEmpty
public void setShouldDeleteIfEmpty(boolean shouldDeleteIfEmpty) Flag to indicate that the target file should be deleted if no lines have been written (other than header and footer) on close. Defaults to false.- Parameters:
shouldDeleteIfEmpty- the flag value to set
-
setSaveState
public void setSaveState(boolean saveState) Set the flag indicating whether or not state should be saved in the providedExecutionContextduring theItemStreamcall to update. Setting this to false means that it will always start at the beginning on a restart.- Parameters:
saveState- if true, state will be persisted
-
setHeaderCallback
headerCallback will be called before writing the first item to file. Newline will be automatically appended after the header is written.- Parameters:
headerCallback-FlatFileHeaderCallbackto generate the header
-
setTransactional
public void setTransactional(boolean transactional) Flag to indicate that writing to the buffer should be delayed if a transaction is active. Defaults to true.- Parameters:
transactional- true if writing to buffer should be delayed.
-
write
Writes out a string followed by a "new line", where the format of the new line separator is determined by the underlying operating system.- Specified by:
writein interfaceItemWriter<T>- Parameters:
items- list of items to be written to output stream- Throws:
Exception- if an error occurs while writing items to the output stream
-
doWrite
Write out a string of items followed by a "new line", where the format of the new line separator is determined by the underlying operating system.- Parameters:
items- to be written- Returns:
- written lines
-
close
public void 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.- Specified by:
closein interfaceItemStream- See Also:
-
open
Initialize the reader. This method may be called multiple times before close is called.- Specified by:
openin interfaceItemStream- Parameters:
executionContext- current step'sExecutionContext. Will be the executionContext from the last run of the step on a restart.- Throws:
ItemStreamException- See Also:
-
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.- Specified by:
updatein interfaceItemStream- Parameters:
executionContext- to be updated- See Also:
-
getOutputState
-