首页 文章

如何将来自外部服务调用的响应与camel中的原始消息合并

提问于
浏览
1

从逻辑角度来看,这是我希望实现的路由行为:

Logical view of routing info

我希望能够将外部服务的响应与原始请求合并 .

我已经能够使用多播,聚合器和模拟 endpoints 来实现这一点,但我想知道是否有更清洁的方法 . 我目前的实现如下:

<multicast strategyRef="serviceAggregator" stopOnException="false">
            <to uri="mock:foo" />
            <to uri="http://0.0.0.0:9999/service/?throwExceptionOnFailure=false" />
        </multicast>
        <camel:to uri="log:uk.co.company.aggregated?showAll=true" />
        <to uri="http://0.0.0.0:9999/anotherService/

我特别不喜欢的部分是使用模拟 endpoints ,但我也不认为这是表达上图的一种非常易读的方式 . 所以我想知道是否有更优雅的方式这样做?

2 回答

  • 4

    我建议阅读有关EIP模式的内容,例如内容丰富http://camel.apache.org/content-enricher.html

    您可以将回复消息与请求消息合并的位置 .

    介意内容Enricher有两种模式 - 充实 - pollEnrich

    请务必注意上述链接中的文档之间的差异 .

    <route>
      <from uri="...">
      <enrich uri="http://0.0.0.0:9999/service/?throwExceptionOnFailure=false" strategyRef="serviceAggregator"/>
      <to uri="log:uk.co.company.aggregated?showAll=true" />
      <to uri="http://0.0.0.0:9999/anotherService/>
      ...
    </route>
    

    是的,您的图表显示拆分器,但示例代码使用多播EIP .

  • 0

    您可以简单地将原始消息存储在标头或属性中,然后在bean中进行一些合并 . 使用 Headers 和当前正文 .

    .setHeader(“orig”,body()) . to(“externalService”) . bean(new MyMergeBean())

相关问题