【问题标题】:Is it possible to read a file after receiving an event?收到事件后是否可以读取文件?
【发布时间】:2019-10-01 20:50:55
【问题描述】:

我正在使用带有内置 Camel Routes 的 ActiveMQ 代理。我想在收到事件后读取文件。

<pseudo>
 from Event A
 read File XY
 to Event B with Body from File XY
</pseuod>

我简单地尝试根据事件从临时目录中移动文件,但只写入事件 B。日志文件中没有异常或错误消息。

<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">

        <!-- You can use Spring XML syntax to define the routes here using the <route> element -->
        <route>
            <description>Example Camel Route</description>
            <from uri="activemq:example.A"/>
            <from uri="file://tmp/a?delete=true"/>
            <to uri="file://tmp/b?overruleFile=copy-of-${file:name}"/>
            <to uri="activemq:example.B"/>
        </route>
</camelContext>

更新单个文件的工作解决方案:

    <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">

        <!-- You can use Spring XML syntax to define the routes here using the <route> element -->
        <route>
            <description>Example Camel Route</description>
            <from uri="activemq:example.A"/>
            <pollEnrich>
               <constant>file:///tmp/a?fileName=file1</constant>
            </pollEnrich>
            <log message="file content  ${body}"/>
            <to uri="activemq:example.B"/>
        </route>
    </camelContext>

【问题讨论】:

标签: apache-camel activemq


【解决方案1】:

您需要为此使用内容丰富器。 This 正是您要找的。

<route>
    <from uri="activemq:example.A"/>
    <pollEnrich>
        <constant>file://tmp/a?delete=true</constant>
    </pollEnrich>
    <to uri="activemq:example.B"/>
</route>

请注意,骆驼版本 2.15 或更早版本

pollEnrich 不会访问当前 Exchange 中的任何数据 意味着在轮询时它不能使用您可能使用的任何现有标题 已设置在交易所。例如,您不能在 Exchange.FILE_NAME 标头并使用 pollEnrich 仅使用该标头 文件。为此,您必须在端点 URI 中设置文件名。

【讨论】:

  • 这适用于 Camel 2.15 或更早版本
  • 对于未来的读者 - 不要忘记 unix 文件系统上的根 / file:///tmp/a?fileName=dummy.txt
  • @ClausIbsen 抱歉,我错过了提及这一点。谢谢指出
猜你喜欢
  • 2019-07-30
  • 1970-01-01
  • 2013-06-23
  • 1970-01-01
  • 2019-02-08
  • 2011-06-29
  • 1970-01-01
  • 1970-01-01
  • 2018-07-28
相关资源
最近更新 更多