public class FileWritingMessageHandler extends AbstractReplyProducingMessageHandler
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
.
AbstractReplyProducingMessageHandler.RequestHandler
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
Constructor and Description |
---|
FileWritingMessageHandler(Expression destinationDirectoryExpression)
Constructor which sets the
destinationDirectoryExpression . |
FileWritingMessageHandler(File destinationDirectory)
Constructor which sets the
destinationDirectoryExpression using
a LiteralExpression . |
Modifier and Type | Method and Description |
---|---|
protected void |
doInit() |
protected String |
getTemporaryFileSuffix() |
protected Object |
handleRequestMessage(Message<?> requestMessage)
Subclasses must implement this method to handle the request Message.
|
void |
setAutoCreateDirectory(boolean autoCreateDirectory)
Specify whether to create the destination directory automatically if it
does not yet exist upon initialization.
|
void |
setCharset(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(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".
|
doInvokeAdvisedRequestHandler, getMessagingTemplate, handleMessageInternal, hasAdviceChain, onInit, setAdviceChain, setBeanClassLoader, setChannelResolver, setOutputChannel, setOutputChannelName, setRequiresReply, setSendTimeout, shouldCopyRequestHeaders
getComponentType, getOrder, handleMessage, setOrder, setShouldTrack
afterPropertiesSet, getApplicationContextId, getBeanFactory, getComponentName, getConversionService, getIntegrationProperties, getIntegrationProperty, getMessageBuilderFactory, getTaskScheduler, setApplicationContext, setBeanFactory, setBeanName, setComponentName, setConversionService, setMessageBuilderFactory, setTaskScheduler, toString
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
getComponentName
public FileWritingMessageHandler(File destinationDirectory)
destinationDirectoryExpression
using
a LiteralExpression
.destinationDirectory
- Must not be nullFileWritingMessageHandler(Expression)
public FileWritingMessageHandler(Expression destinationDirectoryExpression)
destinationDirectoryExpression
.destinationDirectoryExpression
- Must not be nullFileWritingMessageHandler(File)
public void setAutoCreateDirectory(boolean autoCreateDirectory)
autoCreateDirectory
- true to create the directory if needed.public void setTemporaryFileSuffix(String temporaryFileSuffix)
temporaryFileSuffix
- The temporary file suffix.public void setFileExistsMode(FileExistsMode fileExistsMode)
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.fileExistsMode
- Must not be nullpublic void setExpectReply(boolean expectReply)
expectReply
- true if a reply is expected.protected String getTemporaryFileSuffix()
public void setFileNameGenerator(FileNameGenerator fileNameGenerator)
FileNameGenerator
strategy to use when generating
the destination file's name.fileNameGenerator
- The file name generator.public void setDeleteSourceFiles(boolean deleteSourceFiles)
FileHeaders.ORIGINAL_FILE
header value containing either a
File instance or a String representing the original file path.deleteSourceFiles
- true to delete the source files.public void setCharset(String charset)
charset
- The charset.protected void doInit()
doInit
in class AbstractReplyProducingMessageHandler
protected Object handleRequestMessage(Message<?> requestMessage)
AbstractReplyProducingMessageHandler
handleRequestMessage
in class AbstractReplyProducingMessageHandler
requestMessage
- The request message.null
.