java - Camel 文件处理

标签 java configuration routes apache-camel file-uri

我正在使用 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 ;所以这几乎就像 <otherwise>元素被完全跳过/忽略!

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

最佳答案

尝试此设置,并调整轮询间隔以适应:

来自Camel File Component文档:

sendEmptyMessageWhenIdle

default =false

Camel 2.9: If the polling consumer did not poll any files, you can enable this option to send an empty message (no body) instead.

关于写入文件,请在 <otherwise> 内添加日志语句以确保其得到执行。如果是,请检查文件/文件夹权限等。

祝你好运。

关于java - Camel 文件处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21258313/

相关文章:

ruby-on-rails - 使用 STI 时,如何强制 Rails 在 link_to 帮助程序中使用父类(super class)名称?

java - 递归算法中的奇怪行为,

java - 为什么在常量池中对字符串、整数、浮点等使用单独的标签

java - 读取大文件(超过 60GB)并写入新文件

c# - 如何将不同的布局应用于 NLog 中的同一目标?

java - 配置 Java 安装以仅打印堆栈跟踪的前几行

php - 从 nginx 中的不同位置提供 php 文件

ruby-on-rails - 如何使用 Rails 中的 DELETE 方法创建/注销路由/登录

java - 我可以在不硬编码大小的情况下将 ArrayList 中的 Java 值分配给不同的变量吗?

php - Laravel:在 routes/api.php 或 routes/web.php 中的 AJAX 请求端点?