org.springframework.batch.item.file
Class FlatFileItemWriter

java.lang.Object
  extended by org.springframework.batch.item.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 output target that writes data to a file or stream. The writer also provides restart, statistics and transaction features by implementing corresponding interfaces where possible (with a file). The location of the file is defined by a Resource and must represent a writable file.
Uses buffered writer to improve performance.

This class will be updated in the future to use a buffering approach to handling transactions, rather than outputting directly to the file and truncating on rollback

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 Output Template.
 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 setLineAggregator(LineAggregator lineAggregator)
          Public setter for the LineAggregator.
 void setResource(Resource resource)
          Setter for resource.
 void setSaveState(boolean saveState)
          Set the boolean 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.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()

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 -

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

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 - TODO
See Also:
ItemStream.close(ExecutionContext)

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

open

public void open(ExecutionContext executionContext)
Initialize the Output Template.

Specified by:
open in interface ItemStream
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)

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.

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.

setSaveState

public void setSaveState(boolean saveState)
Set the boolean 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.

Parameters:
saveState -


Copyright © 2008 SpringSource. All Rights Reserved.