【问题标题】:Apache Camel merge two files from different routesApache Camel 合并来自不同路由的两个文件
【发布时间】:2014-02-05 16:31:23
【问题描述】:

这就是我想要做的。我必须阅读两个具有此内容的文件

<Person>
<Requestor>Dinesh</Requestor>
</Person>

为此,我创建了一条路线

<route id="getPerson">
    <from uri="file:src/main/resources/xml?noop=true"/>
    </route>

接下来我需要读取另一个名为 Address 的文件

<Address>
  <City>New York </City>
</Address>

这是我的第二条路线

<route id="getAddress">
    <from uri="file:src/main/resources/xmlAddress?noop=true"/>
    </route>

如何将这两个 xml 合并为一个使用 Enricher 或 Aggregate 使最终的 xml 消息看起来像这样

<Person>
  <Requestor>Dinesh</Requestor>
  <Address>
     <City>New York</City>
  </Address>
</Person>

有什么想法吗?我尝试按照文档进行操作,但它所说的只是发送到一些 Web 服务 uri。

上面的场景是我真正想做的事情的淡化版本。对我来说,现实生活中的情况是这样做—— 步骤 1. 读取一个 xml 文件, 第 2 步:调用 Web 服务并获取响应。 第 3 步:在第 2 步中合并响应并在第 1 步中将其添加到 Xml 正文中

编辑 1: 我可以编写自定义 AggregatorStartegy 类。我写了这样的东西

public class AggregationStrategy implements org.apache.camel.processor.aggregate.AggregationStrategy{

@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
    newExchange.getOut().setBody("<all>" 
              + oldExchange.getIn().getBody(String.class) 
              + newExchange.getIn().getBody(String.class) 
              + "</all>");
            return newExchange;
}

   }

我正在苦苦挣扎的是如何编写 Spring xml,在这里我可以看出我的消息或文件 1+ 消息或文件 2,加入他们。这是我的实际 context.xml 的样子

<camelContext id="myCamel" xmlns="http://camel.apache.org/schema/spring">
<route>
    <from uri="file:src/main/resources/xmlPerson?noop=true"/>

    <camel:to uri="direct:enrichMessage"></camel:to>
</route> 
<route  >
    <from uri="file:src/main/resources/xmlAddress?noop=true"/> 
    <log message="**************RESPONSE FROM CATASK DSS:: \n ${body}" id="log_output"/>
    <camel:to uri="direct:enrichMessage"></camel:to> 

</route>
 <route id="enrichMessage">
    <from uri="direct:enrichMessage"/>
    <camel:enrich strategyRef="aggregationBean" />
    <log message="**************MERGED RESPONSE :: \n ${body}"/>
</route>

【问题讨论】:

  • 那么网络服务究竟在哪里发挥作用?
  • 例如 .enrich("serviceA.com?xxx").enrich("http://serviceB?xx")。这是一个例子。andrejkoelewijn.com/blog/2010/06/13/… 。我得到什么解决方案并不重要,我只想将两个文件或两条消息合并为一个。我实际上修改了这篇文章来解释我的情况。我的实际情况是 - Step 1. Read an x​​ml file, Step 2: Call a web service and get response. Step 3: Merge Response in Step 2 and add it o Xml body in Step 1.
  • 编辑了问题标题。
  • 使用自定义的 AggregationStrategy 进行丰富应该可以解决问题。
  • @vikingsteve 查看我的编辑 1。这不是我要找的。感谢您的意见:-)

标签: java jakarta-ee apache-camel esb


【解决方案1】:

我终于明白了。我最初的理解是我们可以手头有两条消息并将它们合并为 Ex - Route 1 最终消息 -

迪内什

Route 2 最终消息 -

<Address>
  <city>New York</city>
</Address>

我的理解是,在获得上述两条消息后,我可以构建一个 aggregationStartegy 并将它们合并。好吧,我的假设是错误的。丰富器的工作方式是它手头有一条消息,在它从第二条路由获取消息之前,我们需要告诉 Camel - “嘿,在你收到 Route 2 的消息之前,这是来自 Route 1 的消息。当你获​​取来自 Route 1 的消息,使用我的聚合策略类来合并它们”。”

所以我在使用 Enricher 后期待以下结果

<Person>
   <name>Dinesh</name>
</Person>
<Address>
  <city>New York</city>
</Address>

但我不知道该怎么做。这就是我正在做的事情,这是错误的方法

<route id="getPerson">
<from uri="file:src/data/catask/person?noop=true" />
<to uri="direct:enrich"/> 
</route>

<route id="getAddress">
<from uri="file:src/data/catask/address?noop=true" />
<to uri="direct:enrich"/>
</route>

<route id="enrich">
<from uri="direct:enrich"/>
<enrich strategyRef="aggregationBean"/>
<log message="After Merge ... ${body}"/> 
</route>

<bean id="aggregationBean" class="com.mycompany.camel.canadatask.AggregationStrategy"/>  

我的 Java 类看起来像这样

public class AggregationStrategy implements   
     org.apache.camel.processor.aggregate.AggregationStrategy{



@Override
public Exchange aggregate(Exchange message,Exchange resource) {
    String old = resource.getIn().getBody(String.class);
    System.out.println("OLD:: \n"+old);
    String newMsg = message.getIn().getBody(String.class);
    System.out.println("NEW:: \n"+newMsg);
    System.out.println("MERGED::" + old + newMsg);
    message.getIn().setBody(old+newMsg);
            return message;
}

  }

当然,上面的代码不起作用。后来我意识到了错误,我对浓缩器的理解是错误的。

正确的实现是这样的-

<route id="getPerson">
   <from uri="file:src/data/catask/person?noop=true" />
   <pollEnrich strategyRef="aggregationBean" uri="file:src/data/catask/address?noop=true"/> 
   <log message="After Merge ... ${body}"/>
 </route>

<bean id="aggregationBean" class="com.mycompany.camel.canadatask.AggregationStrategy"/>  

java 代码保持不变 -

public class AggregationStrategy implements   
     org.apache.camel.processor.aggregate.AggregationStrategy{



@Override
public Exchange aggregate(Exchange message,Exchange resource) {
    String old = resource.getIn().getBody(String.class);
    System.out.println("OLD:: \n"+old);
    String newMsg = message.getIn().getBody(String.class);
    System.out.println("NEW:: \n"+newMsg);
    System.out.println("MERGED::" + old + newMsg);
    message.getIn().setBody(old+newMsg);
            return message;
}

  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    相关资源
    最近更新 更多