IntegrationFlow as a Gateway

IntegrationFlow 可以从提供的 GatewayProxyFactoryBean 组件的服务接口开始,如下例中所示:

The IntegrationFlow can start from the service interface that provides a GatewayProxyFactoryBean component, as the following example shows:

public interface ControlBusGateway {

    void send(String command);
}

...

@Bean
public IntegrationFlow controlBusFlow() {
    return IntegrationFlow.from(ControlBusGateway.class)
            .controlBus()
            .get();
}

为 interface 方法提供的所有代理都随负责将消息发送到 IntegrationFlow 中下一个集成组件的通道一起提供。你可以用 @MessagingGateway 注解标记服务接口,并用 @Gateway 注解标记方法。不过,requestChannel 会被忽略并用 IntegrationFlow 中下一个组件的内部通道替代。否则,使用 IntegrationFlow 创建此类配置是没有意义的。

All the proxy for interface methods are supplied with the channel to send messages to the next integration component in the IntegrationFlow. You can mark the service interface with the @MessagingGateway annotation and mark the methods with the @Gateway annotations. Nevertheless, the requestChannel is ignored and overridden with that internal channel for the next component in the IntegrationFlow. Otherwise, creating such a configuration by using IntegrationFlow does not make sense.

默认情况下,GatewayProxyFactoryBean`获取一个常规 bean 名称,如 `[FLOW_BEAN_NAME.gateway]。您可以使用 @MessagingGateway.name()`属性或重载的 `IntegrationFlow.from(Class<?> serviceInterface, Consumer<GatewayProxySpec> endpointConfigurer)`工厂方法更改该 ID。此外,界面 `@MessagingGateway`注释中的所有属性都应用于目标 `GatewayProxyFactoryBean。当注释配置不适用时,`Consumer<GatewayProxySpec>`变体可用于为目标代理提供适当的选项。此 DSL 方法从 5.2 版本开始可用。

By default, a GatewayProxyFactoryBean gets a conventional bean name, such as [FLOW_BEAN_NAME.gateway]. You can change that ID by using the @MessagingGateway.name() attribute or the overloaded IntegrationFlow.from(Class<?> serviceInterface, Consumer<GatewayProxySpec> endpointConfigurer) factory method. Also, all the attributes from the @MessagingGateway annotation on the interface are applied to the target GatewayProxyFactoryBean. When annotation configuration is not applicable, the Consumer<GatewayProxySpec> variant can be used for providing appropriate option for the target proxy. This DSL method is available starting with version 5.2.

有了 Java 8,你甚至可以使用 java.util.function 接口创建集成网关,如下例中所示:

With Java 8, you can even create an integration gateway with the java.util.function interfaces, as the following example shows:

@Bean
public IntegrationFlow errorRecovererFlow() {
    return IntegrationFlow.from(Function.class, (gateway) -> gateway.beanName("errorRecovererFunction"))
            .<Object>handle((p, h) -> {
                throw new RuntimeException("intentional");
            }, e -> e.advice(retryAdvice()))
            .get();
}

errorRecovererFlow 可按以下方式使用:

That errorRecovererFlow can be used as follows:

@Autowired
@Qualifier("errorRecovererFunction")
private Function<String, String> errorRecovererFlowGateway;