Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.10.6. File filtering
Overview
When a poller endpoint is configured to poll a directory it will attempt to consume any file placed into that directory. If you want to limit the files a poller endpoint will attempt to consume, you can configure the endpoint to filter files based on their names. To do so, you must supply the endpoint with an implementation of the
java.io.FileFilter
interface.
There are several file filter implementation available in open source including the Apache Commons IO implementations and the Apache Jakarta-ORO implementations. You can also implement your own file filter if you need specific filtering capabilities.
Implementing a file filter
To implement a file filter, you need to provide an implementation of the
java.io.FileFilter
interface. The FileFilter
interface has a single method, accept()
, that needs to be implemented. Example 10.11, “File filter's accept method” shows the signature of the accept()
method.
Example 10.11. File filter's accept method
public boolean accept()(java.io.File pathname);
The
accept()
method takes a File
object that represents the file being checked against the filter. If the file passes the filter, the accept()
method should return true
. If the file does not pass, then the method should return false
.
Example 10.12, “Simple file filter implementation” shows a file filter implementation that matches against a string passed into its constructor.
Example 10.12. Simple file filter implementation
package org.apache.servicemix.demo; import java.io.File; import java.io.FileFilter; public class myFileFilter implements FileFilter { String filtername = "joe.xml"; public myFileFilter() { } public myFileFilter(String filtername) { this.filtername = filtername; } public boolean accept(File file) { String name = file.getName(); return name.equals(this.filtername); } }
Configuring an endpoint to use a file filter
You configure a poller endpoint to use a file filter using its
filter
attribute. The filter
attribute's value is a reference to a bean
element specifying the class of the file filter implementation.
Example 10.13, “Poller endpoint using a file filter” shows configuration for a poller endpoint that uses the file filter implemented in Example 10.11, “File filter's accept method”. The
constructor-arg
element sets the filter's fitlername by passing a value into the constructor.
Example 10.13. Poller endpoint using a file filter
<beans xmlns:file="http://servicemix.apache.org/file/1.0" xmlns:foo="http://servicemix.org/demo/"> <file:poller service="foo:filePoller" endpoint="filePoller" targetService="foo:fileSender" file="inbox" filter="#myFilter" /> <bean id="myFilter" class="org.apache.servicemix.demo.myFileFilter"> <constructor-arg value="joefred.xml" /> </bean> ... </beans>
Note
You can also configure a poller endpoint to use a file filter by adding a child
filter
element to the endpoint's configuration. The filter
element simply wraps the bean
element that configures the file filter.