リソースサポート

リソース受信チャネルアダプターは、Spring の Resource 抽象化に基づいて構築され、ファイル、URL、クラスパスリソースなど、さまざまな実際の基になるリソース全体の柔軟性を高めます。ファイル受信チャネルアダプターに似ていますが、より一般的です。

リソース受信チャネルアダプター

リソース受信チャネルアダプターは、Resource オブジェクトのコレクションをペイロードとする Message を作成するポーリングアダプターです。

Resource オブジェクトは、pattern 属性で指定されたパターンに基づいて解決されます。解決された Resource オブジェクトのコレクションは、Message 内のペイロードとしてアダプターのチャネルに送信されます。これは、リソース受信チャネルアダプターとファイル受信チャネルアダプターの大きな違いの 1 つです。後者は、File オブジェクトをバッファーに入れ、Message ごとに単一の File オブジェクトを送信します。

次の例は、クラスパスで使用可能な things.thing1 パッケージ内の 'properties' 拡張子で終わるすべてのファイルを検索し、Message のペイロードとして '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 の独自の実装のインスタンスへの参照を提供できます。

<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 戦略インターフェースの独自の実装を提供できます。

public interface CollectionFilter<T> {

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

}

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

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 属性を使用します。

<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"/>

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="".