Spring Integration

org.springframework.integration.file
Class FileWritingMessageHandler

java.lang.Object
  extended by org.springframework.integration.context.IntegrationObjectSupport
      extended by org.springframework.integration.handler.AbstractMessageHandler
          extended by org.springframework.integration.handler.AbstractReplyProducingMessageHandler
              extended by org.springframework.integration.file.FileWritingMessageHandler
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.InitializingBean, org.springframework.core.Ordered, NamedComponent, Orderable, MessageHandler, MessageProducer, TrackableComponent

public class FileWritingMessageHandler
extends AbstractReplyProducingMessageHandler

A MessageHandler implementation that writes the Message payload to a file. If the payload is a File object, it will copy the File to the specified destination directory. If the payload is a byte array or String, it will write it directly. Otherwise, the payload type is unsupported, and an Exception will be thrown.

If the 'deleteSourceFiles' flag is set to true, the original Files will be deleted. The default value for that flag is false. See the setDeleteSourceFiles(boolean) method javadoc for more information.

Other transformers may be useful to precede this handler. For example, any Serializable object payload can be converted into a byte array by the PayloadSerializingTransformer. Likewise, any Object can be converted to a String based on its toString() method by the ObjectToStringTransformer.

Author:
Mark Fisher, Iwein Fuld, Alex Peters, Oleg Zhurakousky, Artem Bilan, Gunnar Hillert

Nested Class Summary
 
Nested classes/interfaces inherited from class org.springframework.integration.handler.AbstractReplyProducingMessageHandler
AbstractReplyProducingMessageHandler.RequestHandler
 
Field Summary
 
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
 
Constructor Summary
FileWritingMessageHandler(org.springframework.expression.Expression destinationDirectoryExpression)
          Constructor which sets the destinationDirectoryExpression.
FileWritingMessageHandler(java.io.File destinationDirectory)
          Constructor which sets the destinationDirectoryExpression using a LiteralExpression.
 
Method Summary
protected  java.lang.String getTemporaryFileSuffix()
           
protected  java.lang.Object handleRequestMessage(Message<?> requestMessage)
          Subclasses must implement this method to handle the request Message.
 void onInit()
          Subclasses may implement this for initialization logic.
 void setAutoCreateDirectory(boolean autoCreateDirectory)
          Specify whether to create the destination directory automatically if it does not yet exist upon initialization.
 void setCharset(java.lang.String charset)
          Set the charset name to use when writing a File from a String-based Message payload.
 void setDeleteSourceFiles(boolean deleteSourceFiles)
          Specify whether to delete source Files after writing to the destination directory.
 void setExpectReply(boolean expectReply)
          Specify whether a reply Message is expected.
 void setFileExistsMode(FileExistsMode fileExistsMode)
          Will set the FileExistsMode that specifies what will happen in case the destination exists.
 void setFileNameGenerator(FileNameGenerator fileNameGenerator)
          Provide the FileNameGenerator strategy to use when generating the destination file's name.
 void setTemporaryFileSuffix(java.lang.String temporaryFileSuffix)
          By default, every file that is in the process of being transferred will appear in the file system with an additional suffix, which by default is ".writing".
 
Methods inherited from class org.springframework.integration.handler.AbstractReplyProducingMessageHandler
getMessagingTemplate, handleMessageInternal, setAdviceChain, setBeanClassLoader, setChannelResolver, setOutputChannel, setRequiresReply, setSendTimeout, shouldCopyRequestHeaders
 
Methods inherited from class org.springframework.integration.handler.AbstractMessageHandler
getComponentType, getOrder, handleMessage, setOrder, setShouldTrack
 
Methods inherited from class org.springframework.integration.context.IntegrationObjectSupport
afterPropertiesSet, getBeanFactory, getComponentName, getConversionService, getTaskScheduler, setBeanFactory, setBeanName, setComponentName, setConversionService, setTaskScheduler, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.springframework.integration.context.NamedComponent
getComponentName
 

Constructor Detail

FileWritingMessageHandler

public FileWritingMessageHandler(java.io.File destinationDirectory)
Constructor which sets the destinationDirectoryExpression using a LiteralExpression.

Parameters:
destinationDirectory - Must not be null
See Also:
FileWritingMessageHandler(Expression)

FileWritingMessageHandler

public FileWritingMessageHandler(org.springframework.expression.Expression destinationDirectoryExpression)
Constructor which sets the destinationDirectoryExpression.

Parameters:
destinationDirectoryExpression - Must not be null
See Also:
FileWritingMessageHandler(File)
Method Detail

setAutoCreateDirectory

public void setAutoCreateDirectory(boolean autoCreateDirectory)
Specify whether to create the destination directory automatically if it does not yet exist upon initialization. By default, this value is true. If set to false and the destination directory does not exist, an Exception will be thrown upon initialization.


setTemporaryFileSuffix

public void setTemporaryFileSuffix(java.lang.String temporaryFileSuffix)
By default, every file that is in the process of being transferred will appear in the file system with an additional suffix, which by default is ".writing". This can be changed by setting this property.

Parameters:
temporaryFileSuffix -

setFileExistsMode

public void setFileExistsMode(FileExistsMode fileExistsMode)
Will set the FileExistsMode that specifies what will happen in case the destination exists. For example FileExistsMode.APPEND instructs this handler to append data to the existing file rather then creating a new file for each Message. If set to FileExistsMode.APPEND, the adapter will also create a real instance of the LockRegistry to ensure that there is no collisions when multiple threads are writing to the same file. Otherwise the LockRegistry is set to PassThruLockRegistry which has no effect.

Parameters:
fileExistsMode - Must not be null

setExpectReply

public void setExpectReply(boolean expectReply)
Specify whether a reply Message is expected. If not, this handler will simply return null for a successful response or throw an Exception for a non-successful response. The default is true.


getTemporaryFileSuffix

protected java.lang.String getTemporaryFileSuffix()

setFileNameGenerator

public void setFileNameGenerator(FileNameGenerator fileNameGenerator)
Provide the FileNameGenerator strategy to use when generating the destination file's name.


setDeleteSourceFiles

public void setDeleteSourceFiles(boolean deleteSourceFiles)
Specify whether to delete source Files after writing to the destination directory. The default is false. When set to true, it will only have an effect if the inbound Message has a File payload or a FileHeaders.ORIGINAL_FILE header value containing either a File instance or a String representing the original file path.


setCharset

public void setCharset(java.lang.String charset)
Set the charset name to use when writing a File from a String-based Message payload.


onInit

public final void onInit()
Description copied from class: IntegrationObjectSupport
Subclasses may implement this for initialization logic.

Overrides:
onInit in class AbstractReplyProducingMessageHandler

handleRequestMessage

protected java.lang.Object handleRequestMessage(Message<?> requestMessage)
Description copied from class: AbstractReplyProducingMessageHandler
Subclasses must implement this method to handle the request Message. The return value may be a Message, a MessageBuilder, or any plain Object. The base class will handle the final creation of a reply Message from any of those starting points. If the return value is null, the Message flow will end here.

Specified by:
handleRequestMessage in class AbstractReplyProducingMessageHandler

Spring Integration