如何使用骆驼端点来管理文件?

How do I use camel endpoints to manage files?

我有一个应用程序可以读取文件夹中的文件、更改其内容并将其写回到另一个文件夹中。我正在尝试添加与 Citrus 的集成测试,以在第一个文件夹中写入包含一些内容的文件,并在应用程序修改后检查第二个文件夹中更改的内容。

我的问题与 非常相似,其中回复说使用 Camel 路线,但我对这些概念还很陌生,不知道从哪里开始...

我不太了解 <camelContext> 标签及其工作原理。到目前为止,我写了以下内容,这是我尝试在文件中写入的地方:

<citrus-camel:endpoint id="inputCamelEndpoint" camel-contxt="inputCamelContext" endpoint-uri="file://C:/HL7/source/?fileName=test.hl7"/>

<camelContext id="inputCamelContext" xmlns="http://camel.apache.org/schema/spring">
    <route id="inputRoute">
        <to uri="file://C:/HL7/source/?fileName=test.hl7"/>
    </route>
</camelContext>

<send endpoint="inputCamelEndpoint">
    <message type="plaintext">
        <data>Hello!</data>
    </message>
</send>

我应该为 <from uri=""> 写什么? 另外,camelContext 的 xmlns 属性是否损坏?我有 this error.

我真的希望有人能给我一些关于这一切的细节,我很迷茫。

欢迎使用 Whosebug! 在遇到这个问题之前,我还没有听说过 Citrus 框架,但我对 Camel 略知一二,我会尝试通过个性化的 Camel 101 来帮助你,让你继续前进。

要事第一

  • A CamelContext 就像Apache Camel 集成框架的运行时环境。可以把它想象成 Spring ApplicationContext 这样的东西。您使用 Camel 创建的所有内容都存在于上下文中。
  • A Camel Route is a processing pipeline. It takes input from something(http,jms,file... almost everything under the sun), 对其应用零个或多个处理步骤并产生输出到某物 (console,disk, http,..你的名字)。
    如果我们考虑一个简单的路由,它很可能有两个Endpoints。一种 Consumer 使用 Messages 的端点,一个或多个 Processor 检查和处理入站消息的组件以及一个 Producer 将最终消息发送到某处(如存储到磁盘,在 testminal 上打印,HTTP post 到服务器 ...)

在您的情况下,据我了解,您需要一个 Route 可以从 Citrus 框架接收消息并将其存储到磁盘,以便您的被测系统可以处理该文件。要将此用例解释为路由定义,请在 Camel 的 XML DSL 中。


<camelContext id="inputCamelContext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://camel.apache.org/schema/spring"
        xsi:schemaLocation="
            http://camel.apache.org/schema/spring
            http://camel.apache.org/schema/spring/camel-spring.xsd">
  <route id="inputRoute">
    <from uri="direct:citrusConsumer" />
    <to uri="file://C:/HL7/source/?fileName=test.hl7" />
  </route>
<camelContext/>

direct: URI 表示同步 Consumer 端点 [Docs on direct component]. What this example route does is, take whatever input message arriving at the consumer(direct:direct:citrusConsumer) and write it to a file, with name test.hl7 in the directory C:/HL7/source/. If you want to get fancy here, please read additional options for the File component. Citrus-Camel integration docs 还显示了名为 direct:newsdirect: 组件的用法。上面示例代码 Route 中的 Route 准备就绪后,Citrus 可以向 direct:citrusConsumer 发送消息以与 Camel 对话。

重复使用您的示例代码,集成将类似于下面的代码片段(注意端点 uri 的更改)

<citrus-camel:endpoint id="inputCamelEndpoint" camel-contxt="inputCamelContext" endpoint-uri="direct:citrusConsumer"/>

我希望这能帮助你取得成功。