IntegrationFlow 作为网关
IntegrationFlow as a Gateway
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;