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;