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 aResource
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
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
AbstractFileItemWriter.OutputState
Encapsulates the runtime state of the writer.
Field Summary
Fields Modifier and Type Field Description protected boolean
append
static java.lang.String
DEFAULT_CHARSET
static java.lang.String
DEFAULT_LINE_SEPARATOR
static boolean
DEFAULT_TRANSACTIONAL
protected java.lang.String
lineSeparator
protected static org.apache.commons.logging.Log
logger
protected boolean
shouldDeleteIfExists
protected AbstractFileItemWriter.OutputState
state
Constructor Summary
Constructors Constructor Description AbstractFileItemWriter()
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
close()
No-op.protected abstract java.lang.String
doWrite(java.util.List<? extends T> items)
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.protected AbstractFileItemWriter.OutputState
getOutputState()
void
open(ExecutionContext executionContext)
Initialize the reader.void
setAppendAllowed(boolean append)
Flag to indicate that the target file should be appended if it already exists.void
setEncoding(java.lang.String newEncoding)
Sets encoding for output template.void
setFooterCallback(FlatFileFooterCallback footerCallback)
footerCallback will be called after writing the last item to file, but before the file is closed.void
setForceSync(boolean forceSync)
Flag to indicate that changes should be force-synced to disk on flush.void
setHeaderCallback(FlatFileHeaderCallback headerCallback)
headerCallback will be called before writing the first item to file.void
setLineSeparator(java.lang.String lineSeparator)
Public setter for the line separator.void
setResource(org.springframework.core.io.Resource resource)
Setter for resource.void
setSaveState(boolean saveState)
Set the flag indicating whether or not state should be saved in the providedExecutionContext
during theItemStream
call to update.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.void
setShouldDeleteIfExists(boolean shouldDeleteIfExists)
Flag to indicate that the target file should be deleted if it already exists, otherwise it will be created.void
setTransactional(boolean transactional)
Flag to indicate that writing to the buffer should be delayed if a transaction is active.void
update(ExecutionContext executionContext)
Return emptyExecutionContext
.void
write(java.util.List<? extends T> items)
Writes 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, setExecutionContextName, setName
Field Detail
DEFAULT_TRANSACTIONAL
public static final boolean DEFAULT_TRANSACTIONAL
- See Also:
- Constant Field Values
logger
protected static final org.apache.commons.logging.Log logger
DEFAULT_LINE_SEPARATOR
public static final java.lang.String DEFAULT_LINE_SEPARATOR
DEFAULT_CHARSET
public static final java.lang.String DEFAULT_CHARSET
- See Also:
- Constant Field Values
state
protected AbstractFileItemWriter.OutputState state
shouldDeleteIfExists
protected boolean shouldDeleteIfExists
lineSeparator
protected java.lang.String lineSeparator
append
protected boolean append
Method Detail
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 void setLineSeparator(java.lang.String lineSeparator)
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.Resource resource)
Setter for resource. Represents a file that can be written.- Specified by:
setResource
in interfaceResourceAwareItemWriterItemStream<T>
- Parameters:
resource
- the resource to be written to
setEncoding
public void setEncoding(java.lang.String newEncoding)
Sets encoding for output template.- Parameters:
newEncoding
-String
containing 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 andappendAllowed
is 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 flagshouldDeleteIfExists
is 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 providedExecutionContext
during theItemStream
call 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
public void setHeaderCallback(FlatFileHeaderCallback headerCallback)
headerCallback will be called before writing the first item to file. Newline will be automatically appended after the header is written.- Parameters:
headerCallback
-FlatFileHeaderCallback
to generate the header
setFooterCallback
public void setFooterCallback(FlatFileFooterCallback footerCallback)
footerCallback will be called after writing the last item to file, but before the file is closed.- Parameters:
footerCallback
-FlatFileFooterCallback
to generate the footer
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
public void write(java.util.List<? extends T> items) throws java.lang.Exception
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:
write
in interfaceItemWriter<T>
- Parameters:
items
- list of items to be written to output stream- Throws:
java.lang.Exception
- if an error occurs while writing items to the output stream
doWrite
protected abstract java.lang.String doWrite(java.util.List<? extends T> items)
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 class:ItemStreamSupport
No-op.- Specified by:
close
in interfaceItemStream
- Overrides:
close
in classItemStreamSupport
- See Also:
ItemStream.close()
open
public void open(ExecutionContext executionContext) throws ItemStreamException
Initialize the reader. This method may be called multiple times before close is called.- Specified by:
open
in interfaceItemStream
- Overrides:
open
in classItemStreamSupport
- Parameters:
executionContext
- current step'sExecutionContext
. Will be the executionContext from the last run of the step on a restart.- Throws:
ItemStreamException
- See Also:
ItemStream.open(ExecutionContext)
update
public void update(ExecutionContext executionContext)
Description copied from class:ItemStreamSupport
Return emptyExecutionContext
.- Specified by:
update
in interfaceItemStream
- Overrides:
update
in classItemStreamSupport
- Parameters:
executionContext
- to be updated- See Also:
ItemStream.update(ExecutionContext)
getOutputState
protected AbstractFileItemWriter.OutputState getOutputState()