org.springframework.batch.item.file
Class FlatFileItemWriter

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

public class FlatFileItemWriter
extends ExecutionContextUserSupport
implements ItemWriter, ItemStream, 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.

Output lines are buffered until flush() is called and only then the actual writing to file occurs.

The implementation is *not* thread-safe.

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

Constructor Summary
FlatFileItemWriter()
           
 
Method Summary
 void afterPropertiesSet()
          Assert that mandatory properties (resource) are set.
 void clear()
          Clear any buffers that are being held.
 void close(ExecutionContext executionContext)
          If any resources are needed for the stream to operate they need to be destroyed here.
 void flush()
          Flush any buffers that are being held.
 void open(ExecutionContext executionContext)
          Initialize the reader.
 void setBufferSize(int newSize)
          Sets buffer size for output template
 void setEncoding(String newEncoding)
          Sets encoding for output template.
 void setFieldSetCreator(FieldSetCreator fieldSetCreator)
          Public setter for the FieldSetCreator.
 void setHeaderLines(String[] headerLines)
          Public setter for the header lines.
 void setLineAggregator(LineAggregator 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 setShouldDeleteIfExists(boolean shouldDeleteIfExists)
           
 void update(ExecutionContext executionContext)
          Indicates that the execution context provided during open is about to be saved.
 void write(Object data)
          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, setName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FlatFileItemWriter

public FlatFileItemWriter()
Method Detail

afterPropertiesSet

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

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

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 lineAggregator)
Public setter for the LineAggregator. This will be used to translate a FieldSet into a line for output.

Parameters:
lineAggregator - the LineAggregator to set

setFieldSetCreator

public void setFieldSetCreator(FieldSetCreator fieldSetCreator)
Public setter for the FieldSetCreator. This will be used to transform the item into a FieldSet before it is aggregated by the LineAggregator.

Parameters:
fieldSetCreator - the FieldSetCreator to set

setResource

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

Parameters:
resource -

setEncoding

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


setBufferSize

public void setBufferSize(int newSize)
Sets buffer size for output template


setShouldDeleteIfExists

public void setShouldDeleteIfExists(boolean shouldDeleteIfExists)
Parameters:
shouldDeleteIfExists - the shouldDeleteIfExists 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 -

setHeaderLines

public void setHeaderLines(String[] headerLines)
Public setter for the header lines. These will be output at the head of the file before any calls to write(Object) (and not on restart unless the restart is after a failure before the first flush).

Parameters:
headerLines - the header lines to set

write

public void write(Object data)
           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
Parameters:
data - Object (a String or Object that can be converted) 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(ExecutionContext executionContext)
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
Parameters:
executionContext - the current execution context in case it is needed
See Also:
ItemStream.close(ExecutionContext)

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)

flush

public void flush()
           throws FlushFailedException
Description copied from interface: ItemWriter
Flush any buffers that are being held. This will usually be performed prior to committing any transactions.

Specified by:
flush in interface ItemWriter
Throws:
FlushFailedException - in case of an error. If this exception is thrown the writer may be in an inconsistent state and manual intervention might be required to reconcile the data with persistent output.

clear

public void clear()
           throws ClearFailedException
Description copied from interface: ItemWriter
Clear any buffers that are being held. This will usually be performed prior to rolling back any transactions.

Specified by:
clear in interface ItemWriter
Throws:
ClearFailedException - in case of an error. If this exception is thrown the writer may be in an inconsistent state and manual intervention might be required to reconcile the data with persistent output.


Copyright © 2009 SpringSource. All Rights Reserved.