IntegrationFlow 作為閘道器

IntegrationFlow 可以從提供 GatewayProxyFactoryBean 元件的服務介面開始,如下例所示

public interface ControlBusGateway {

    void send(String command);
}

...

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

介面方法的所有代理都提供通道,用於將訊息傳送到 IntegrationFlow 中的下一個整合元件。你可以使用 @MessagingGateway 註解標記服務介面,並使用 @Gateway 註解標記方法。然而,requestChannel 將被忽略並被 IntegrationFlow 中下一個元件的內部通道覆蓋。否則,使用 IntegrationFlow 建立此類配置就沒有意義。

預設情況下,GatewayProxyFactoryBean 會獲得一個約定俗成的 bean 名稱,例如 [FLOW_BEAN_NAME.gateway]。你可以使用 @MessagingGateway.name() 屬性或過載的 IntegrationFlow.from(Class<?> serviceInterface, Consumer<GatewayProxySpec> endpointConfigurer) 工廠方法來更改此 ID。此外,介面上 @MessagingGateway 註解的所有屬性都應用於目標 GatewayProxyFactoryBean。當註解配置不適用時,可以使用 Consumer<GatewayProxySpec> 變體為目標代理提供適當的選項。此 DSL 方法從版本 5.2 開始提供。

使用 Java 8,你甚至可以使用 java.util.function 介面建立整合閘道器,如下例所示

@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 可以按如下方式使用

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