【发布时间】: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