对于最新的稳定版本,请使用 Spring Integration 6.4.3! |
消息通道
除了IntegrationFlowBuilder
使用 EIP 方法,Java DSL 提供了一个 Fluent API 来配置MessageChannel
实例。
为此,MessageChannels
builder factory 的 builder 工厂。
以下示例演示如何使用它:
@Bean
public PriorityChannelSpec priorityChannel() {
return MessageChannels.priority(this.mongoDbChannelMessageStore, "priorityGroup")
.interceptor(wireTap());
}
一样MessageChannels
builder factory 可以在channel()
EIP 方法从IntegrationFlowBuilder
连接端点,类似于连接input-channel
/output-channel
对。
默认情况下,端点与DirectChannel
bean 名称基于以下模式的实例:[IntegrationFlow.beanName].channel#[channelNameIndex]
.
此规则也适用于 inline 生成的未命名通道MessageChannels
builder factory 使用情况。
但是,所有MessageChannels
方法具有一个变体,该变体知道channelId
可用于设置MessageChannel
实例。
这MessageChannel
references 和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")
注册DirectChannel
bean 替换为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
实例。