104.21. 避免多次读取同一文件(idempotent 消费者)
Camel 直接支持组件内的 Idempotent Consumer,以便跳过已经处理的文件。可以通过设置 idempotent=true 选项来启用此功能。
from("file://inbox?idempotent=true").to("...");
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>
<route>
<from uri="file://inbox?idempotent=true&idempotentKey=${file:name}-${file:size}"/>
<to uri="bean:processInbox"/>
</route>
默认情况下,Camel 使用基于内存的存储来跟踪消耗的文件,它使用最早使用的缓存,最多 1000 个条目。您可以使用值中的 # 符号指示 它引用 带有指定 id 的 Registry 中的 bean,以自行编写此存储实现。
如果 Camel 跳过了一个文件,则 Camel 会在 DEBUG 级别进行日志,因为它已被使用:
DEBUG FileConsumer is idempotent and the file has been consumed before. Will skip this file: target\idempotent\report.txt
DEBUG FileConsumer is idempotent and the file has been consumed before. Will skip this file: target\idempotent\report.txt