搜索

104.21. 避免多次读取同一文件(验证消费者)

download PDF

Camel 支持在组件内直接支持 Idempotent Consumer,以便跳过已处理的文件。通过设置 idempotent=true 选项可以启用此功能。

from("file://inbox?idempotent=true").to("...");

Camel 使用绝对文件名作为幂等键,来检测重复的文件。从 Camel 2.11 开始,您可以使用 idempotentKey 选项中的表达式来自定义这个密钥。例如,使用名称和文件大小作为密钥

<route>
  <from uri="file://inbox?idempotent=true&amp;idempotentKey=${file:name}-${file:size}"/>
  <to uri="bean:processInbox"/>
</route>

默认情况下,Camel 使用基于内存的存储来跟踪消耗的文件,它会使用最少使用最多 1000 个条目的缓存。您可以使用值中的 idempotentRepository 选项使用 idempotentRepository 选项来插件您自己的存储实现,以指示它在带有指定 id 的 Registry 中的 bean 的引用。

 <!-- define our store as a plain spring bean -->
 <bean id="myStore" class="com.mycompany.MyIdempotentStore"/>

<route>
  <from uri="file://inbox?idempotent=true&amp;idempotentRepository=#myStore"/>
  <to uri="bean:processInbox"/>
</route>

如果一个文件在 DEBUG 级别被使用,则 Camel 将在 DEBUG 级别记录,因为它已在前面被使用:

DEBUG FileConsumer is idempotent and the file has been consumed before. Will skip this file: target\idempotent\report.txt
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.