此内容没有您所选择的语言版本。

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.
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2026 Red Hat
返回顶部