org.springframework.batch.item.file
Class FlatFileItemWriter<T>

java.lang.Object
  extended by org.springframework.batch.item.util.ExecutionContextUserSupport
      extended by org.springframework.batch.item.file.FlatFileItemWriter<T>
All Implemented Interfaces:
ResourceAwareItemWriterItemStream<T>, ItemStream, ItemWriter<T>, InitializingBean

public class FlatFileItemWriter<T>
extends ExecutionContextUserSupport
implements ResourceAwareItemWriterItemStream<T>, InitializingBean

This class is an item writer that writes data to a file or stream. The writer also provides restart. The location of the output file is defined by a Resource and must represent a writable file.
Uses buffered writer to improve performance.
The implementation is *not* thread-safe.

Author:
Waseem Malik, Tomas Slanina, Robert Kasanicky, Dave Syer

Field Summary
protected static Log logger
           
 
Constructor Summary
FlatFileItemWriter()
           
 
Method Summary
 void afterPropertiesSet()
          Assert that mandatory properties (lineAggregator) are set.
 void close()
          If any resources are needed for the stream to operate they need to be destroyed here.
 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(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 setLineAggregator(LineAggregator<T> lineAggregator)
          Public setter for the LineAggregator.
 void setLineSeparator(String lineSeparator)
          Public setter for the line separator.
 void setResource(Resource resource)
          Setter for resource.
 void setSaveState(boolean saveState)
          Set the flag indicating whether or not state should be saved in the provided ExecutionContext during the ItemStream 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)
          Indicates that the execution context provided during open is about to be saved.
 void write(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.util.ExecutionContextUserSupport
getKey, getName, setName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected static final Log logger
Constructor Detail

FlatFileItemWriter

public FlatFileItemWriter()
Method Detail

afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Assert that mandatory properties (lineAggregator) are set.

Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
Exception
See Also:
InitializingBean.afterPropertiesSet()

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(String lineSeparator)
Public setter for the line separator. Defaults to the System property line.separator.

Parameters:
lineSeparator - the line separator to set

setLineAggregator

public void setLineAggregator(LineAggregator<T> lineAggregator)
Public setter for the LineAggregator. This will be used to translate the item into a line for output.

Parameters:
lineAggregator - the LineAggregator to set

setResource

public void setResource(Resource resource)
Setter for resource. Represents a file that can be written.

Specified by:
setResource in interface ResourceAwareItemWriterItemStream<T>
Parameters:
resource -

setEncoding

public void setEncoding(String newEncoding)
Sets encoding for output template.


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 and appendAllowed 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 flag shouldDeleteIfExists 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 provided ExecutionContext during the ItemStream call to update. Setting this to false means that it will always start at the beginning on a restart.

Parameters:
saveState -

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.


setFooterCallback

public void setFooterCallback(FlatFileFooterCallback footerCallback)
footerCallback will be called after writing the last item to file, but before the file is closed.


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.


write

public void write(List<? extends T> items)
           throws 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. If the input is not a String and a converter is available the converter will be applied and then this method recursively called with the result. If the input is an array or collection each value will be written to a separate line (recursively calling this method for each value). If no converter is supplied the input object's toString method will be used.

Specified by:
write in interface ItemWriter<T>
Parameters:
items - list of items to be written to output stream
Throws:
Exception - if the transformer or file output fail, WriterNotOpenException if the writer has not been initialized.

close

public void close()
Description copied from interface: ItemStream
If 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:
close in interface ItemStream
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 interface ItemStream
Throws:
ItemStreamException
See Also:
ItemStream.open(ExecutionContext)

update

public void update(ExecutionContext executionContext)
Description copied from interface: ItemStream
Indicates 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:
update in interface ItemStream
Parameters:
executionContext - to be updated
See Also:
ItemStream.update(ExecutionContext)


Copyright © 2013 SpringSource. All Rights Reserved.