【问题标题】:Camel File processing骆驼文件处理
【发布时间】:2014-02-11 01:23:24
【问题描述】:

我正在使用 Camel (2.11.0) 来尝试并实现以下功能:

  • 如果某个文件存在于某个位置,请将其复制到另一个位置,然后开始处理它
  • 如果不存在这样的文件,那么我不希望文件使用者/轮询器阻塞;我只想继续处理direct:cleanup 路线

我只想轮询文件一次!

这是我目前所拥有的(使用 Spring XML):

<camelContext id="my-camel-context" xmlns="http://camel.apache.org/schema/spring">
    <route id="my-route
        <from uri="file:///home/myUser/myApp/fizz?include=buzz_.*txt"/>

        <choice>
            <when>
                <!-- If the body is empty/NULL, then there was no file. Send to cleanup route. -->
                <simple>${body} == null</simple>
                <to uri="direct:cleanup" />
            </when>

            <otherwise>
                <!-- Otherwise we have a file. Copy it to the parent directory, and then continue processing. -->
                <to uri="file:///home/myUser/myApp" />
            </otherwise>
        </choice>

        <!-- We should only get here if a file existed and we've already copied it to the parent directory. -->
        <to uri="bean:shouldOnlyGetHereIfFileExists?method=doSomething" />
    </route>

    <!--
        Other routes defined down here, including one with a "direct:cleanup" endpoint.
    -->
</camelContext>

通过上述配置,如果/home/myUser/myApp/fizz 处没有文件,那么 Camel 只会等待/阻塞,直到有文件为止。相反,我希望它放弃并转到direct:cleanup

如果有文件,我看到它在 shouldOnlyGetHereIfFileExists bean 中得到处理,但我没有看到它被复制到 /home/myUser/myApp;所以几乎就好像 &lt;otherwise&gt; 元素被完全跳过/忽略了!

有什么想法吗?提前致谢!

【问题讨论】:

    标签: java configuration routing apache-camel file-uri


    【解决方案1】:

    试试这个设置,并调整你的轮询间隔以适应:

    来自Camel File Component docs:

    sendEmptyMessageWhenIdle

    默认=假

    Camel 2.9:如果轮询消费者没有轮询任何文件,您可以启用此选项以发送空消息(无正文)。

    关于写入文件,在&lt;otherwise&gt; 中添加一条日志语句以确保它正在执行。如果是,请检查文件/文件夹权限等。

    祝你好运。

    【讨论】:

    • 谢谢@vikingsteve - 我会给你+1,但我没有足够的代表。我感觉很糟糕,有一个重要的部分我忘了提及:我只希望路由轮询文件一次。您对sendEmptyMessageWhenIdle 的建议确实有效,但路由不断轮询,一遍又一遍再次为一个文件。关于如何将 Camel 配置为仅轮询一次的任何想法?再次感谢!
    • 还有一个delay 选项 - 您可以尝试将其设置为 0 或 -1 或某个非常大的值,然后看看会发生什么。否则,在您的路线第一次执行期间,您可以使用controlbus 停止路线。
    • 看起来延迟不能
    【解决方案2】:

    我在尝试使用条件时遇到的一个错误:

     <simple>${body} != null</simple>
    

    它是否总是返回 true。

    请通过以下链接:

    http://camel.465427.n5.nabble.com/choice-when-check-BodyType-null-Body-null-td4259599.html

    也许对你有帮助。

    【讨论】:

      【解决方案3】:

      这是非常古老的,但如果有人发现这个,你只能投票一次 "?repeatCount=1"

      【讨论】:

      • 它是石英组件的一部分。
      【解决方案4】:

      我知道这个问题几乎是在 4 年前完成的,但我昨天遇到了同样的问题。 所以我会把我的答案放在这里,也许它会帮助另一个人。 我正在使用 Camel,版本 3.10.0

      要使其完全按照问题中的描述工作:

      • 如果某个文件存在于某个位置,请将其复制到另一个位置,然后开始处理它
      • 如果不存在这样的文件,那么我不希望文件使用者/轮询器阻塞;我只想继续处理直接:清理路线
      • 只希望文件被轮询一次!

      使用 ${body} == null 我们需要的配置是:

      sendEmptyMessageWhenIdle=true //空闲时会发送一个空正文

      ma​​xMessagesPerPoll=1 //一次最大文件数

      repeatCount=1 //会执行多少次Pool(上)

      greedy=true // 如果最后一个池执行文件,它将 再执行一次

      XML:

      <camel:endpoint id="" uri="file:DIRECTORY?sendEmptyMessageWhenIdle=true&amp;initialDelay=100&amp;maxMessagesPerPoll=1&amp;repeatCount=1&amp;greedy=false" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多