java - Apache Camel 合并来自不同路径的两个文件

标签 java jakarta-ee apache-camel esb

这就是我想要做的。我必须读取两个包含此内容的文件

<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>

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

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

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

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

编辑 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>

最佳答案

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

迪内什

路线 2 最终消息 -

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

我的理解是,在收到上面的两条消息后,我可以构建一个 aggregationStartegy 并将它们合并。好吧,我的假设是错误的。 enricher 的工作方式是它手头有一条消息,在它从第二条路线获取消息之前,我们需要告诉 Camel - “嘿,在你收到路线 2 的消息之前,这是来自路线 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;
}

  }

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

正确的实现是这样的-

<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;
}

  }

关于java - Apache Camel 合并来自不同路径的两个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21167348/

相关文章:

java - 初始化错误 : java.net.SocketException:打开的文件太多

java 2个不同的公式问题

java - 使用 OpenCV/JavaCV 的透视图像

java - Eclipse Memory Analyser,但总是显示发生内部错误?

logging - 如何检测 Apache Camel 中的网络断开连接?

java - Camel ReSTLet maxThreads 未按预期工作

java - EntityManager 注入(inject)良好实践

debugging - Intellij IDEA 调试时的备用源

javascript - 将 FormParams 与 Jersey 和 jQuery 结合使用

java - Camel 泉中的多条内部路线