【问题标题】:How is Apache Camel's Enrich EIP Supposed to Work with File Endpoints?Apache Camel 的丰富 EIP 如何与文件端点一起使用?
【发布时间】:2018-09-29 06:21:26
【问题描述】:

我正在尝试通过编写使用各种 EIP 的简单路由来学习如何使用 Apache Camel。我的导师建议 Apache ServiceMix 作为测试简单路由的好服务器,所以我使用的是 Apache ServiceMix 5.1.0。目前,我正在尝试创建一个从文件中读取请求的路由,将请求的内容替换为来自第二个文件的响应,然后将该响应写入第三个文件。

理想情况下,第二个文件不会被删除;我最终想在其他自学路线中使用这种模式,第二个文件将模仿 web 服务的响应。但是,我现在只是想让它工作,所以我不会使用noop 之类的选项使事情复杂化。

如果我正确地阅读了the Apache Camel Wiki page for the Content Enricher,从enrich EIP 中省略聚合策略将使Camel 丢弃消息的内容并将其替换为从资源中获取的正文。所以,我认为这条骆驼路线会做我想做的事:

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:camel="http://camel.apache.org/schema/blueprint" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
       http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">

    <camelContext xmlns="http://camel.apache.org/schema/blueprint">
        <route>
            <from uri="file:camel/inMessage/?fileName=replaceMe.txt" />
            <enrich uri="file:camel/storedResponse/?fileName=withThis.txt" />
            <to uri="file:camel/outMessage/?fileName=output.txt" />
        </route>
    </camelContext>

</blueprint>

实际发生的是camel/storedResponse/withThis.txt 的内容被camel/inMessage/replaceMe.txt 的内容替换。文件camel/outmessage/output.txt 已创建,但它包含replaceMe.txt 的内容,而不是withThis.txt。

由于我还是 Camel 的新手,我认为问题只是我误读了文档中的某些内容,或者忽略了一个明显的配置问题。

不管怎样,这里是开始路线之前的文件内容。

replaceMe.txt
This is the message sent. It should not appear in the response.

withThis.txt
This is the file stored in the server. The response should contain this text.

output.txt does not yet exist.

这是路由完成后的文件内容。

replaceMe.txt has been deleted.

withThis.txt
This is the message sent. It should not appear in the response.

output.txt
This is the message sent. It should not appear in the response.

感谢您的宝贵时间。

【问题讨论】:

    标签: apache-camel apache-servicemix blueprint-osgi


    【解决方案1】:

    来自Camel doc:

    enrich 使用 Producer 来获取附加数据。它通常用于请求回复消息传递,例如调用外部 Web 服务。另一方面,pollEnrich 使用轮询消费者来获取附加数据。它通常用于事件消息消息传递,例如读取文件或下载 FTP 文件。

    如果使用enrich,那么withThis.txt的内容被替换为replaceMe.txt的内容,这不是你想要的。请改用pollEnrich:

     <pollEnrich uri="file:camel/storedResponse/?fileName=withThis.txt" />
    

    【讨论】:

    • 感谢您的回复。我尝试了一个更复杂的例子,pollEnrich 导致路由挂起,所以我犹豫再试一次。这是否意味着 Camel 文档中有一个错误,它说“聚合策略是可选的。如果你不提供它,Camel 默认情况下只会使用从资源中获取的主体。”?在这种情况下,资源为withThis.txt,但enrich 忽略了资源的内容。
    • @JacobMorleyCarson 文档是正确的:聚合策略是可选的。使用 enrich 作为生产者 (to),例如调用 Web 服务或写入数据。消费者使用pollEnrich (from),例如从文件中读取数据。
    • 所以,我从骆驼文档中引用的第一句话是正确的。我引用的第二个说:“如果你不提供它,Camel 默认情况下只会使用从资源中获得的主体。”这是似乎不正确的部分。我认为资源是enrich 标签中给出的端点;在这种情况下,它是withThis.txt。尽管文档说 Camel 将使用从资源中获得的主体,但 Camel 却丢弃了从资源中获得的主体。
    • @JacobMorleyCarson 但是,enrich 不能用于读取文件。
    • 我想我明白我误解了什么。如果我现在明白了,enrich 会收到交换。然后它会生成自己的交换并将其发送到端点。 file 端点接收生成的交换并将其解释为好像它位于 to 标记中一样。然后enrich 将从其uri 属性(file 端点)获得的任何内容发送到路由的下一部分。我引用的那句话是指enrich 产生的交换,但我认为它指的是enrich 接收的交换。感谢您的帮助。
    【解决方案2】:

    丰富的作品就像一个 您的路线实际上是将从 replaceMe.txt 收到的正文 写入 到 withThis.txt 而不对正文进行任何更改。因此最终结果 pollEnrich 需要用于实际读取 withThis.txt

    【讨论】:

      猜你喜欢
      • 2014-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-06
      • 2020-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多