我们有一个包含行项目的购物车域模型。在外部,我们有一个 API 来结帐购物车。不过,在内部,我们有 3 种不同的 HTTP 服务:
- 创建购物车
- 添加订单项 - 每个订单项一个 HTTP 调用
- 收款处
我们想在集成流中表达这一点,如果任何步骤失败或超时,则中止。下面是流的一些伪代码:
@Bean
public IntegrationFlow cartFlow() {
return IntegrationFlows.from(channel())
.transform(fromJson(ShoppingCart.class))
.handle(Http.outboundGateway(.....) // How do we proceed here to create a shopping cart?
.split(ShoppingCart.class, ShoppingCart::getLineItems)
.handle(Http.outboundGateway(.....) // And here to add each line item?
.aggregate()
.handle(Http.outboundGateway(.....) // And here to checkout
.get();
}
添加 Http.outboundGateway 调用不是问题。问题实际上是关于保留上下文(关于HTTP方法调用后的购物车)。除了确认调用成功之外,服务不会返回任何数据。
我理解的一种选择是创建一个自定义 bean,用于进行 HTTP 调用并将其注入管道。这种方法对我来说不是很惯用。
谢谢!
您的用例完全适合Content Enricher
模式,您可以使用该模式进行一些payload
,调用一些外部服务(通过request-channel
上的网关),等待回复并向原始payload
添加一些内容。
对于您的用例,您可能需要几个相应的.enrich()
定义。
另请参阅弹簧集成Content Enricher
定义以获取更多信息。
编辑
ContentEnricher
示例:
@Bean
public IntegrationFlow enricherFlow() {
return IntegrationFlows.from("enricherInput", true)
.enrich(e -> e.requestChannel("enrichChannel")
.requestPayloadExpression("payload")
.shouldClonePayload(false)
.propertyExpression("name", "payload['name']")
.propertyFunction("date", m -> new Date())
.headerExpression("foo", "payload['name']")
)
.get();
}