Camel Enrich和AggregationStrategy

时间:2016-09-12 18:07:20

标签: java apache-camel

我试图使用Camel通过Custom AggregationStrategy来丰富EIP,即

from("direct:xyz")
   .setHeader("...","")
   .enrich("http://localhost:myservice", new AggregationStrategy() {

       public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
         .....
       }
});

HTTP端点的输出返回XML响应。我希望将其编组并设置为newExchange的主体。

目前我通过向JaxbDataFormat注入AggregationStrategy并调用unmarshall方法来做到这一点..即

public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
     AnotherObj obj = dataFormat.unmarshall(newExchange,
          newExchange.getIn().getBody(InputStream.class));

     Parentobj test = oldExchange.getIn().getBody(ParentObj.class)

     test.setobj(obj)
     oldExchange.getIn().setBody(test);
     return oldExchange;
   }

有没有更好的方法来实现这个目标?

2 个答案:

答案 0 :(得分:0)

只需使用"直接:路线"在uri上并使用该路由上的新交换(呼叫服务,设置标头,解组数据)执行所需的操作,生成的交换将作为聚合策略中的新交换接收。

答案 1 :(得分:-1)

更好(更简洁)的方法可能是简单地在路由中调用unmarshal方法并传入数据格式:

Java DSL:

DataFormat jaxb = new JaxbDataFormat("com.acme.model");

from("activemq:My.Queue").
  unmarshal(jaxb).
  to("mqseries:Another.Queue");

Spring XML:

<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
  <dataFormats>
    <jaxb id="myJaxb" prettyPrint="true" contextPath="org.apache.camel.example"/>
  </dataFormats>

  <route>
    <from uri="direct:marshalled"/>
    <unmarshal ref="myJaxb"/>
    <to uri="mock:result"/>
  </route>

</camelContext>

http://camel.apache.org/data-format.html

您将在完成/聚合后立即执行此操作