Operator gateway()

`gateway()`操作符在 `IntegrationFlow`定义中是一项特殊服务激活器实现,通过其输入通道调用其他端点或集成流,并等待答复。从技术层面看,它在 `<chain>`定义中扮演嵌套 `<gateway>`组件的相同角色(见 Calling a Chain from within a Chain),允许流变得更简洁、更直观。从商业角度或逻辑上来看,它是一个消息网关,允许在目标集成解决方案中不同部分之间分配并重复使用功能(见 Messaging Gateways)。此操作符有数个实现重载,以适应不同的目标:

The gateway() operator in an IntegrationFlow definition is a special service activator implementation, to call some other endpoint or integration flow via its input channel and wait for reply. Technically it plays the same role as a nested <gateway> component in a <chain> definition (see Calling a Chain from within a Chain) and allows a flow to be cleaner and more straightforward. Logically, and from business perspective, it is a messaging gateway to allow the distribution and reuse of functionality between different parts of the target integration solution (see Messaging Gateways). This operator has several overloads for different goals:

  • gateway(String requestChannel) to send a message to some endpoint’s input channel by its name;

  • gateway(MessageChannel requestChannel) to send a message to some endpoint’s input channel by its direct injection;

  • gateway(IntegrationFlow flow) to send a message to the input channel of the provided IntegrationFlow.

所有这些都有一个带有第二个 Consumer<GatewayEndpointSpec> 参数的变体,用于配置目标 GatewayMessageHandler 和相应的 AbstractEndpoint。此外,基于 IntegrationFlow 的方法允许调用现有的 IntegrationFlow bean 或通过 IntegrationFlow 函数接口的内嵌 lambda 声明流程为子流程,或者将其解压缩为 private 方法,以获得更简洁的代码样式:

All of these have a variant with the second Consumer<GatewayEndpointSpec> argument to configure the target GatewayMessageHandler and respective AbstractEndpoint. Also, the IntegrationFlow-based methods allows calling existing IntegrationFlow bean or declare the flow as a sub-flow via an in-place lambda for an IntegrationFlow functional interface or have it extracted in a private method cleaner code style:

@Bean
IntegrationFlow someFlow() {
        return IntegrationFlow
                .from(...)
                .gateway(subFlow())
                .handle(...)
                .get();
}

private static IntegrationFlow subFlow() {
        return f -> f
                .scatterGather(s -> s.recipientFlow(...),
                        g -> g.outputProcessor(MessageGroup::getOne))
}

如果下游流不总返回响应,你应该将 requestTimeout 设置为 0,以防止无限期挂起调用线程。在这种情况下,流将在该点结束并且线程将被释放以执行进一步的工作。

If the downstream flow does not always return a reply, you should set the requestTimeout to 0 to prevent hanging the calling thread indefinitely. In that case, the flow will end at that point and the thread released for further work.