如需使用最新稳定版本,请使用 Spring Integration 7.0.4spring-doc.cadn.net.cn

IntegrationFlow 作为网关

IntegrationFlow 可以从提供 GatewayProxyFactoryBean 组件的服务接口开始,如下示例所示:spring-doc.cadn.net.cn

public interface ControlBusGateway {

    void send(String command);
}

...

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

所有用于接口方法的代理都会附带一个通道,用于向 IntegrationFlow 中的下一个集成组件发送消息。 您可以使用 @MessagingGateway 注解标记服务接口,并使用 @Gateway 注解标记方法。 然而,requestChannel 将被忽略,并在 IntegrationFlow 中由内部通道(用于下一个组件)所覆盖。 否则,使用 IntegrationFlow 创建此类配置是没有意义的。spring-doc.cadn.net.cn

默认情况下,GatewayProxyFactoryBean 会获得一个常规的 Bean 名称,例如 [FLOW_BEAN_NAME.gateway]。 您可以通过使用 @MessagingGateway.name() 属性或重载的 IntegrationFlow.from(Class<?> serviceInterface, Consumer<GatewayProxySpec> endpointConfigurer) 工厂方法来更改该 ID。 此外,接口上 @MessagingGateway 注解的所有属性都会应用到目标 GatewayProxyFactoryBean。 当注解配置不适用时,可以使用 Consumer<GatewayProxySpec> 变体为目标代理提供合适的选项。 此 DSL 方法从版本 5.2 开始可用。spring-doc.cadn.net.cn

使用 Java 8,您甚至可以创建集成网关,无需实现 java.util.function 接口,如下示例所示:spring-doc.cadn.net.cn

@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 可按如下方式使用:spring-doc.cadn.net.cn

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