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.
<bean id="pollableFileSource" class="org.springframework.integration.file.FileReadingMessageSource" p:inputDirectory="file:${input.directory.property}"/>
To prevent creating messages for certain files, you may supply a
{@link FileListFilter}. By default, an
AcceptOnceFileListFilter
is used. This filter ensures files are picked up only once from the
directory.
<bean id="pollableFileSource" class="org.springframework.integration.file.FileReadingMessageSource" p:inputDirectory="file:${input.directory.property}" p:filter-ref="customFilterBean"/>
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.
<bean id="pollableFileSource" class="org.springframework.integration.file.FileReadingMessageSource" p:inputDirectory="file:${input.directory.property}" p:filter-ref="compositeFilter"/> <bean id="compositeFilter" class="org.springframework.integration.file.CompositeFileListFilter"> <constructor-arg> <list> <bean class="org.springframework.integration.file.AcceptOnceFileListFilter" /> <bean class="org.springframework.integration.file.PatternMatchingFileListFilter"> <constructor-arg value="^test.*$"/> </bean> </list> </constructor-arg> </bean>
The configuration can be simplified using the file specific namespace. To do this use the following template.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:integration="http://www.springframework.org/schema/integration" xmlns:file="http://www.springframework.org/schema/integration/file" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-1.0.xsd http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file-1.0.xsd"> </beans>
Within this namespace you can reduce the FileReadingMessageSource and wrap it in an InboundChannelAdapter like this:
<file:inbound-channel-adapter id="filesIn" directory="file:${input.directory.property}"/> (1) <file:inbound-channel-adapter id="filesIn" directory="file:${input.directory.property}" filter="customFilterBean" /> (2) <file:inbound-channel-adapter id="filesIn" directory="file:${input.directory.property}" filename-pattern="^test.*$" /> (3)
Where (1) is relying on the default filter that just prevents
duplication, (2) is using a custom filter and (3) is using the
filename-pattern
attribute to add a
Pattern
based filter to the
FileReadingMessageSource