103.21. 同じファイルを複数回読み取ることを避ける (べき等コンシューマー)
Camel は Idempotent Consumer をコンポーネント内で直接サポートしているため、すでに処理されたファイルはスキップされます。この機能は、idempotent=true
オプションを設定することで有効にできます。
from("file://inbox?idempotent=true").to("...");
Camel は絶対ファイル名を冪等キーとして使用して、重複ファイルを検出します。Camel 2.11 以降では、idempotentKey オプションで式を使用して、このキーをカスタマイズできます。たとえば、名前とファイルサイズの両方をキーとして使用するには
<route> <from uri="file://inbox?idempotent=true&idempotentKey=${file:name}-${file:size}"/> <to uri="bean:processInbox"/> </route>
デフォルトでは、Camel は消費されたファイルを追跡するためにインメモリーベースのストアを使用し、最大 1000 エントリーを保持する最も使用頻度の低いキャッシュを使用します。値に #
記号を使用して idempotentRepository
オプションを使用して、指定された 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