对于最新的稳定版本,请使用 Spring Integration 6.5.1spring-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)工厂方法。 此外,来自@MessagingGateway接口上的注释应用于目标GatewayProxyFactoryBean. 当注释配置不适用时,Consumer<GatewayProxySpec>variant 可用于为目标代理提供适当的选项。 此 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;