|
如需使用最新稳定版本,请使用 Spring Integration 7.0.4! |
消息通道
除了使用 EIP 方法的 IntegrationFlowBuilder 之外,Java DSL 还提供了流畅的 API 来配置 MessageChannel 实例。
为此,提供了 MessageChannels 构建器工厂。
以下示例展示了如何使用它:
@Bean
public PriorityChannelSpec priorityChannel() {
return MessageChannels.priority(this.mongoDbChannelMessageStore, "priorityGroup")
.interceptor(wireTap());
}
相同的 MessageChannels 构建器工厂可用于来自 IntegrationFlowBuilder 的 channel() EIP 方法以连接端点,类似于在 XML 配置中连接 input-channel/output-channel 对。
默认情况下,端点使用 DirectChannel 实例进行连接,其中 Bean 名称基于以下模式:[IntegrationFlow.beanName].channel#[channelNameIndex]。
此规则也适用于由内联 MessageChannels 构建器工厂用法生成的未命名通道。
然而,所有 MessageChannels 方法都有一个变体,能够感知 channelId,您可以使用它来为 MessageChannel 实例设置 Bean 名称。
MessageChannel 引用和 beanName 可用作 Bean 方法调用。
以下示例展示了使用 channel() EIP 方法的几种可能方式:
@Bean
public QueueChannelSpec queueChannel() {
return MessageChannels.queue();
}
@Bean
public PublishSubscribeChannelSpec<?> publishSubscribe() {
return MessageChannels.publishSubscribe();
}
@Bean
public IntegrationFlow channelFlow() {
return IntegrationFlow.from("input")
.fixedSubscriberChannel()
.channel("queueChannel")
.channel(publishSubscribe())
.channel(MessageChannels.executor("executorChannel", this.taskExecutor))
.channel("output")
.get();
}
-
from("input")表示“查找并使用 id 为MessageChannel的‘输入’元素,或者创建一个”。 -
fixedSubscriberChannel()会生成一个FixedSubscriberChannel的实例,并以名称channelFlow.channel#0将其注册。 -
channel("queueChannel")工作方式相同,但使用现有的queueChannelBean。 -
channel(publishSubscribe())是 bean 方法的引用。 -
channel(MessageChannels.executor("executorChannel", this.taskExecutor))是IntegrationFlowBuilder,它将IntegrationComponentSpec暴露给ExecutorChannel并将其注册为executorChannel。 -
channel("output")将DirectChannelBean 注册为名称output,前提是尚不存在具有该名称的 Bean。
注意:前述 IntegrationFlow 定义是有效的,其所有通道均应用于具有 BridgeHandler 个实例的端点。
请小心,在使用来自不同 IntegrationFlow 实例的 MessageChannels 工厂时,应确保使用相同的内联通道定义。
即使 DSL 解析器将不存在的对象注册为 Bean,它也无法从不同的 IntegrationFlow 容器中识别出同一个对象(MessageChannel)。
以下示例是错误的: |
@Bean
public IntegrationFlow startFlow() {
return IntegrationFlow.from("input")
.transform(...)
.channel(MessageChannels.queue("queueChannel"))
.get();
}
@Bean
public IntegrationFlow endFlow() {
return IntegrationFlow.from(MessageChannels.queue("queueChannel"))
.handle(...)
.get();
}
该错误示例的结果是以下异常:
Caused by: java.lang.IllegalStateException:
Could not register object [queueChannel] under bean name 'queueChannel':
there is already object [queueChannel] bound
at o.s.b.f.s.DefaultSingletonBeanRegistry.registerSingleton(DefaultSingletonBeanRegistry.java:129)
要使其生效,您需要为该通道声明 @Bean,并在不同的 IntegrationFlow 实例中使用其 Bean 方法。