Resource Support

资源入站频道适配器以 Spring 的 Resource 抽象为基础,以支持对各种实际类型的底层资源(例如文件、URL 或类路径资源)的更强的灵活性。因此,它类似于文件入站通道适配器,但比其更加通用。

The resource inbound channel adapter builds upon Spring’s Resource abstraction to support greater flexibility across a variety of actual types of underlying resources, such as a file, a URL, or a class path resource. Therefore, it is similar to but more generic than the file inbound channel adapter.

Resource Inbound Channel Adapter

资源入站频道适配器是一种轮询适配器,用于创建一个 Message,其有效载荷是 Resource 对象的集合。

The resource inbound channel adapter is a polling adapter that creates a Message whose payload is a collection of Resource objects.

Resource 对象基于 pattern 属性指定的形式解决。然后将已解析的 Resource 对象的集合作为有效载荷发送到适配器的通道中的 Message。这是资源入站通道适配器和文件入站通道适配器之间的一个主要差异:后者缓冲 File 对象并每个 Message 发送一个 File 对象。

Resource objects are resolved based on the pattern specified by the pattern attribute. The collection of resolved Resource objects is then sent as a payload within a Message to the adapter’s channel. That is one major difference between resource inbound channel adapter and file inbound channel adapter: The latter buffers File objects and sends a single File object per Message.

以下示例显示了一个简单配置,该配置查找 classpath 上的 things.thing1 软件包中以“properties”扩展名结尾的所有文件,并将它们作为 Message 的有效载荷发送到名为 resultChannel 的通道:

The following example shows a simple configuration that finds all files that end with the 'properties' extension in the things.thing1 package available on the classpath and sends them as the payload of a Message to the channel named '`resultChannel’:

<int:resource-inbound-channel-adapter id="resourceAdapter"
               channel="resultChannel"
               pattern="classpath:things/thing1/*.properties">
    <int:poller fixed-rate="1000"/>
</int:resource-inbound-channel-adapter>

资源入站通道适配器依赖于 org.springframework.core.io.support.ResourcePatternResolver 策略接口来解析提供的模式。它默认为当前 ApplicationContext 的实例。但是,你可以通过设置 pattern-resolver 属性提供对自己的 ResourcePatternResolver 实现实例的引用,如下例所示:

The resource inbound channel adapter relies on the org.springframework.core.io.support.ResourcePatternResolver strategy interface to resolve the provided pattern. It defaults to an instance of the current ApplicationContext. However, you can provide a reference to an instance of your own implementation of ResourcePatternResolver by setting the pattern-resolver attribute, as the following example shows:

<int:resource-inbound-channel-adapter id="resourceAdapter"
               channel="resultChannel"
               pattern="classpath:things/thing1/*.properties"
               pattern-resolver="myPatternResolver">
    <int:poller fixed-rate="1000"/>
</int:resource-inbound-channel-adapter>

<bean id="myPatternResolver" class="org.example.MyPatternResolver"/>

你可能有一个用例,需要进一步筛选由 ResourcePatternResolver 解析的资源集合。例如,你可能希望防止已解析的资源再次出现在已解析资源的集合中。另一方面,你的资源可能会经常更新,你确实希望再次获取它们。换句话说,定义一个其他筛选器和完全禁用筛选都是有效用例。你可以提供自己的 org.springframework.integration.util.CollectionFilter 策略接口实现,如下例所示:

You may have a use case where you need to further filter the collection of resources resolved by the ResourcePatternResolver. For example, you may want to prevent resources that were already resolved from appearing in a collection of resolved resources ever again. On the other hand, your resources might be updated rather often, and you do want them to be picked up again. In other words, both defining an additional filter and disabling filtering altogether are valid use cases. You can provide your own implementation of the org.springframework.integration.util.CollectionFilter strategy interface, as the following example shows:

public interface CollectionFilter<T> {

    Collection<T> filter(Collection<T> unfilteredElements);

}

CollectionFilter 接收未筛选元素的集合(在前面的示例中是 Resource 对象),并返回同类筛选元素的集合。

The CollectionFilter receives a collection of un-filtered elements (which are Resource objects in the preceding example), and it returns a collection of filtered elements of that same type.

如果使用 XML 定义适配器但未指定筛选器引用,则资源入站通道适配器将使用 CollectionFilter 的默认实现。该默认筛选器的实现类为 org.springframework.integration.util.AcceptOnceCollectionFilter。它记住前一次调用中传递的元素,以避免多次返回这些元素。

If you define the adapter with XML but you do not specify a filter reference, the resource inbound channel adapter uses a default implementation of CollectionFilter. The implementation class of that default filter is org.springframework.integration.util.AcceptOnceCollectionFilter. It remembers the elements passed in the previous invocation in order to avoid returning those elements more than once.

相反,要注入自己的 CollectionFilter 实现,请使用 filter 属性,如下例所示:

To inject your own implementation of CollectionFilter instead, use the filter attribute, as the following example shows:

<int:resource-inbound-channel-adapter id="resourceAdapter"
               channel="resultChannel"
               pattern="classpath:things/thing1/*.properties"
               filter="myFilter">
    <int:poller fixed-rate="1000"/>
</int:resource-inbound-channel-adapter>

<bean id="myFilter" class="org.example.MyFilter"/>

如果你不需要任何筛选且希望禁用甚至默认的 CollectionFilter 策略,请为筛选器属性提供一个空值(例如,filter=""

If you do not need any filtering and want to disable even the default CollectionFilter strategy, provide an empty value for the filter attribute (for example, filter="")