23.23. 避免多次读取同一文件(回想消费者)
Camel 支持组件中的 Idempotent Consumer,因此它将跳过已处理的文件。这个功能可以通过设置 idempotent=true 选项来启用。
from("file://inbox?idempotent=true").to("...");
from("file://inbox?idempotent=true").to("...");
Camel 使用绝对文件名作为幂等键,以检测重复的文件。您可以使用 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 个条目的缓存。您可以使用值中的 # 符号使用 idempotentRepository 选项来插件此存储实施,以指示它在带有指定 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