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;