Apache Camel合并来自不同路由的两个文件

Din*_*ora 5 java esb apache-camel java-ee

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

<Person>
<Requestor>Dinesh</Requestor>
</Person>
Run Code Online (Sandbox Code Playgroud)

为此,我创建了一条路线

<route id="getPerson">
    <from uri="file:src/main/resources/xml?noop=true"/>
    </route>
Run Code Online (Sandbox Code Playgroud)

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

<Address>
  <City>New York </City>
</Address>
Run Code Online (Sandbox Code Playgroud)

这是我的第二条路线

<route id="getAddress">
    <from uri="file:src/main/resources/xmlAddress?noop=true"/>
    </route>
Run Code Online (Sandbox Code Playgroud)

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

<Person>
  <Requestor>Dinesh</Requestor>
  <Address>
     <City>New York</City>
  </Address>
</Person>
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?我尝试按照文档进行操作,但所有内容都是发送给某些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;
}

   }
Run Code Online (Sandbox Code Playgroud)

我正在努力的是如何编写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>
Run Code Online (Sandbox Code Playgroud)

Din*_*ora 6

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

迪内希

路线2最终消息 -

<Address>
  <city>New York</city>
</Address>
Run Code Online (Sandbox Code Playgroud)

我的理解是,在上面有两条消息之后,我可以构建一个aggregationStartegy并合并它们.我的假设是错的.丰富的方式是它有一条消息在手中,在它从第二条路线获得消息之前,我们需要告诉Camel - "嘿,在你收到消息之前,路线2,这是来自路线1的消息.当你取来自Route 1的消息,使用我的聚合策略类将它们合并为"."

所以我在使用Enricher后期待以下内容

<Person>
   <name>Dinesh</name>
</Person>
<Address>
  <city>New York</city>
</Address>
Run Code Online (Sandbox Code Playgroud)

但我不知道该怎么做.这就是我在做什么,这是错误的方法

<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"/>  
Run Code Online (Sandbox Code Playgroud)

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

  }
Run Code Online (Sandbox Code Playgroud)

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

正确的实现是这样的 -

<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"/>  
Run Code Online (Sandbox Code Playgroud)

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

  }
Run Code Online (Sandbox Code Playgroud)