Camel supports pluggable filtering strategies. You can then configure the endpoint with such a filter to skip certain files being processed.
In the sample we have built our own filter that skips files starting with
skip
in the filename:
public class MyFileFilter implements GenericFileFilter { public boolean accept(GenericFile pathname) { // we don't accept any files starting with skip in the name return !pathname.getFileName().startsWith("skip"); } }
Then we can configure our route using the filter
attribute to reference our filter (using #
notation) that we have defines in
the Spring XML file:
<!-- define our sorter as a plain Spring bean --> <bean id="myFilter" class="com.mycompany.MyFileSorter"/> <route> <from uri="file://inbox?filter=#myFilter"/> <to uri="bean:processInbox"/> </route>
Note
There are also antInclude
and antExclude
options to make
it easy to specify ANT style include/exclude without having to define the filter. See
the URI options above for more information.
The ANT path matcher is shipped out-of-the-box in the camel-spring jar. So you need to depend on camel-spring if you are using Maven. The reason is that we leverage Spring's AntPathMatcher to do the matching.
The file paths is matched with the following rules:
?
matches one character*
matches zero or more characters**
matches zero or more directories in a path
The sample below demonstrates how to use it:
<camelContext xmlns="http://camel.apache.org/schema/spring"> <template id="camelTemplate"/> <!-- use myFilter as filter to allow setting ANT paths for which files to scan for --> <endpoint id="myFileEndpoint" uri= "file://target/antpathmatcher?recursive=true&filter=#myAntFilter"/> <route> <from ref="myFileEndpoint"/> <to uri="mock:result"/> </route> </camelContext> <!-- we use the antpath file filter to use Ant paths --> <!-- for includes and excludes --> <bean id="myAntFilter" class="org.apache.camel.component.file.AntPathMatcherGenericFileFilter"> <!-- include and file in the subfolder that has 'day' in the name --> <property name="includes" value="**/subfolder/**/*day*"/> <!-- exclude all files with 'bad' in name or .xml files. --> <!-- Use comma to separate multiple excludes --> <property name="excludes" value="**/*bad*,**/*.xml"/> </bean>