File Support
Introduction Spring Integration File extends the Spring Integration Core with dedicated vocabulary to deal with reading, writing and transforming files. There is a namespace that enables elements that define channel adapters dedicated to files and support for transformers that transform files into strings or byte arrays. This section will explain the workings of FileReadingMessageSource, FileWritingMessageHandler and how to configure them as beans. Also the support for dealing with files through file specific implementations of Transformer will be discussed. Finally the file specific namespace will be explained.
Reading Files A FileReadingMessageSource can be used to consume files from the filesystem. This is an implementation of MessageSource that creates messages from a file system directory. ]]> To prevent creating messages for certain files, you may supply a FileListFilter. By default, an AcceptOnceFileListFilter is used. This filter ensures files are picked up only once from the directory. ]]> A common problem with reading files is that a file may be detected before it is ready. The default AcceptOnceFileListFilter does not prevent this. In most cases, this can be prevented if the file-writing process renames each file as soon as it is ready for reading. A pattern-matching filter that accepts only files that are ready (e.g. based on a known suffix), composed with the default AcceptOnceFileListFilter allows for this. ]]> The configuration can be simplified using the file specific namespace. To do this use the following template. ]]> Within this namespace you can reduce the FileReadingMessageSource and wrap it in an InboundChannelAdapter like this: ]]> Where the first channel adapter is relying on the default filter that just prevents duplication. The second is using a custom filter and the third is using the filename-pattern attribute to add a Pattern based filter to the FileReadingMessageSource
Writing files To write messages to the file system you can use a FileWritingMessageHandler . This class can deal with File or byte[] payloads and otherwise invokes the toString() method on the payload to establish the contents of the File. In its simplest form the FileWritingMessageHandler just needs a parent directory for the files. Additionally, you can configure the encoding and the charset that will be used in case of a fallback on the toString() method. To make things easier you can configure the FileWritingMessageHandler as part of an outbound channel adapter using the namespace. ]]> If you have more elaborate requirements to the payload to file conversion you could extend the FileWritingMessageHandler, but a much better option is to rely on a Transformer
File Transformers To transform data read from the file system to objects and the other way around you need to do some work. Contrary to FileReadingMessageSource and to a lesser extent FileWritingMessageHandler it is very likely that you will need your own mechanism to get the job done. For this you can implement the Transformer interface. Or extend the AbstractFilePayloadTransformer for inbound messages. Some obvious implementations have been provided. FileToByteArrayTransformer transforms Files into byte[]s using FileCopyUtils . It is often better to use a sequence of transformers than to put all transformations in a single class, in that case the File to byte[] conversion might be a logical first step. FileToStringTransformer will convert Files to Strings as the name suggests. This is mainly useful for debugging. To configure File specific transformers you can use the appropriate elements from the file namespace. ]]> The delete-files option signals the transformer to delete the File after the transformation is done. This is in no way a replacement for using the AcceptOnceFileListFilter with the PollableFileSource in a multi-threaded environment (e.g. Spring Integration in general).