100.21. 同じファイルを複数回読み取りないようにする(べき等コンシューマー)
Camel はコンポーネント内でべき等コンシューマーを直接サポートし、すでに処理されたファイルをスキップします。この機能は、idempotent=true オプションを設定して有効にできます。
from("file://inbox?idempotent=true").to("...");
Camel は絶対ファイル名をべき等キーとして使用し、重複ファイルを検出します。Camel 2.11 以降では、idempotentKey オプションの式を使用してこのキーをカスタマイズできます。たとえば、name と file の両方をキーとして使用する場合
<route>
<from uri="file://inbox?idempotent=true&idempotentKey=${file:name}-${file:size}"/>
<to uri="bean:processInbox"/>
</route>
デフォルトでは、Camel は消費されたファイルの追跡にメモリーベースのストアを使用し、最大 1000 エントリーを保持する、最も最近使用されたキャッシュを使用します。値に # 記号を使用して、指定の ID を持つレジストリー内の Bean を参照することを示すことで、このストアの独自の実装をプラグインでき ます。
<!-- define our store as a plain spring bean -->
<bean id="myStore" class="com.mycompany.MyIdempotentStore"/>
<route>
<from uri="file://inbox?idempotent=true&idempotentRepository=#myStore"/>
<to uri="bean:processInbox"/>
</route>
Camel は、以前使用されたためファイルを省略した場合、DEBUG レベルでログに記録されます。
DEBUG FileConsumer is idempotent and the file has been consumed before. Will skip this file: target\idempotent\report.txt