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)Factory 方法。 此外,来自@MessagingGateway注解应用于目标GatewayProxyFactoryBean. 当 annotation 配置不适用时,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;