【问题标题】:Camel Inter module coordination - dependencyCamel 模块间协调 - 依赖
【发布时间】:2014-01-24 14:55:46
【问题描述】:

我的问题是我有一个 routeBuilder,它根据 url 模式路由到不同的模块,比如 A、B、D,如下所示(customProcessor 将 ArestURN、BrestURN 等添加到标头,这些是逗号分隔的 url 模式,将被匹配针对 in 标头 CamelHttpUri 路由到正确的模块)

from("servlet:///?matchOnUriPrefix=true")
  .process(customProcessor)
    .choice()
    .when(simple("${in.headers.ARestURN} contains ${in.headers.CamelHttpUri}"))
       .to("http4://" + AUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .when(simple("${in.headers.BRestURN} contains ${in.headers.CamelHttpUri}"))
       .to("http4://" + BUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .otherwise()
       .to("http4://" + DUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false");

我的问题是现在我需要为 REST url 请求提供服务,该请求需要我访问 AUrl 和 BUrl 并聚合结果,然后响应请求我该如何实现?

我想写一些如下的东西

from("servlet:///?matchOnUriPrefix=true")
  .process(customProcessor)
    .choice()
    .when(simple("${in.headers.ARestURN} contains ${in.headers.CamelHttpUri}"))
       .to("http4://" + AUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .when(simple("${in.headers.BRestURN} contains ${in.headers.CamelHttpUri}"))
       .to("http4://" + BUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
  .when(simple("${in.headers.MultiModuleRestURN} contains ${in.headers.CamelHttpUri}"))
   .to("direct:multimodule")
    .otherwise()
       .to("http4://" + DUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false");

from("direct:multimodule")
   .process(new MyProcessor())
      .to("http4://" + AUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false");

但我不知道如何从第一个“AUrl”(这是一个返回 json 结果的 REST 服务)获取结果并对其进行一些处理并将结果的特定值提供给下一个要获取的 BUrl url从中得到结果并对其进行处理并组合结果并将其发送回调用服务。

例如:

the REST call :    /AB/123/getPrice
Need to pass 123 to Module A - REST call ->  /A/123/getId     response-{A: 123, id: x1}
Need to pass x1 to Module B  - REST call ->  /B/x1/getPrice   response-{id: x1, p:$10}
Need to return to caller {name: 123, id: x1, price: $10 } (json combined object)

更新:现在做了如下, 注意:为了清楚起见,删除了 bridgeEndpoint 的东西

from("servlet:///?matchOnUriPrefix=true")
   .process(customProcessor)
     .choice()
       .when(simple("${in.headers.multiRestURN} contains ${in.headers.CamelHttpUri}"))
         .to("direct:multimodule")
       .when(simple("${in.headers.ARestURN} contains ${in.headers.CamelHttpUri}"))
         .to("http4://" +AUrl)
       .when(simple("${in.headers.BRestURN} contains ${in.headers.CamelHttpUri}"))
         .to("http4://" +BUrl)
       .otherwise()
         .to("http4://" +DUrl);
//    
from("direct:multimodule")
 .to(ExchangePattern.InOut,"http4://"+AUrl)
   .convertBodyTo(String.class) 
     .process(customAProcess)
      .to(ExchangePattern.InOut, "http4://"+BUrl+"/resources")
         .convertBodyTo(String.class) 
         .process(customBProcessor);

在 customAProcess 我做一个 exchange.getIn().setHeader("AData", exchange.getIn().getBody(String.class));

因此,当 BUrl 请求带有结果时,这些数据可用,我正在向 BUrl 发送不必要的数据,但这有望解决问题,也许当我有时间时,我会研究丰富和聚合策略来解决问题。

【问题讨论】:

  • 你看过recipient list组件吗?
  • 查看了收件人列表,但这有点不适合收件人列表,更丰富和聚合的策略似乎更合适。

标签: apache-camel esb


【解决方案1】:

Enricher component 可能是您正在寻找的。它是一种常见模式的实现。请注意,可以通过 strategyRef 参数传递聚合策略的实现。

在为您的第一个休息调用实施聚合策略时,您可以通过您的第一个休息响应的返回数据来丰富原始交换 - 请参阅 AggregationStrategy 的接口。只需创建策略的 bean 并配置丰富器。

希望对您有所帮助。示例上下文:) :

<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
   <route>
      <from uri="servlet:///?matchOnUriPrefix=true"/>
      <enrich uri="http4://serviceA" strategyRef="strategyA"/>
      <enrich uri="http4://serviceB" strategyRef="strategyB"/>
    </route>
</camelContext>
<bean id="strategyA" class="..." />
<bean id="strategyB" class="..." />

您可以对您的交易做任何您想做的事情,无论是原始的(丰富的)还是新的(丰富的)聚合策略。

【讨论】:

  • 暂时没有尝试以其他方式解决它,也许我也会看看这个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-14
  • 1970-01-01
  • 1970-01-01
  • 2016-08-14
相关资源
最近更新 更多