|
此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Integration 6.4.0! |
消息通道
除了IntegrationFlowBuilder使用 EIP 方法,Java DSL 提供了一个 Fluent API 来配置MessageChannel实例。
为此,MessageChannelsbuilder factory 的 builder 工厂。
以下示例演示如何使用它:
@Bean
public PriorityChannelSpec priorityChannel() {
return MessageChannels.priority(this.mongoDbChannelMessageStore, "priorityGroup")
.interceptor(wireTap());
}
一样MessageChannelsbuilder factory 可以在channel()EIP 方法从IntegrationFlowBuilder连接端点,类似于连接input-channel/output-channel对。
默认情况下,端点与DirectChannelbean 名称基于以下模式的实例:[IntegrationFlow.beanName].channel#[channelNameIndex].
此规则也适用于 inline 生成的未命名通道MessageChannelsbuilder factory 使用情况。
但是,所有MessageChannels方法具有一个变体,该变体知道channelId可用于设置MessageChannel实例。
这MessageChannelreferences 和beanName可以用作 Bean 方法调用。
以下示例显示了使用channel()弹性公网 IP 方式:
@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")的意思是“'查找并使用MessageChannel替换为 “input” id,或创建一个'”。 -
fixedSubscriberChannel()生成一个FixedSubscriberChannel并使用名称channelFlow.channel#0. -
channel("queueChannel")工作方式相同,但使用现有的queueChannel豆。 -
channel(publishSubscribe())是 Bean 方法引用。 -
channel(MessageChannels.executor("executorChannel", this.taskExecutor))是IntegrationFlowBuilder这暴露了IntegrationComponentSpec到ExecutorChannel并将其注册为executorChannel. -
channel("output")注册DirectChannelbean 替换为output作为其名称,只要不存在具有此名称的 bean。
注意:前面的IntegrationFlow定义有效,并且其所有通道都应用于具有BridgeHandler实例。
请小心使用相同的内联通道定义MessageChannels来自不同的工厂IntegrationFlow实例。
即使 DSL 解析器将不存在的对象注册为 bean,它也无法确定相同的对象(MessageChannel) 来自不同的IntegrationFlow器皿。
以下示例是错误的: |
@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实例。