对于最新的稳定版本,请使用 Spring Integration 6.4.3spring-doc.cadn.net.cn

消息通道

除了IntegrationFlowBuilder使用 EIP 方法,Java DSL 提供了一个 Fluent API 来配置MessageChannel实例。 为此,MessageChannelsbuilder factory 的 builder 工厂。 以下示例演示如何使用它:spring-doc.cadn.net.cn

@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 方式:spring-doc.cadn.net.cn

@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,或创建一个'”。spring-doc.cadn.net.cn

  • fixedSubscriberChannel()生成一个FixedSubscriberChannel并使用名称channelFlow.channel#0.spring-doc.cadn.net.cn

  • channel("queueChannel")工作方式相同,但使用现有的queueChannel豆。spring-doc.cadn.net.cn

  • channel(publishSubscribe())是 Bean 方法引用。spring-doc.cadn.net.cn

  • channel(MessageChannels.executor("executorChannel", this.taskExecutor))IntegrationFlowBuilder这暴露了IntegrationComponentSpecExecutorChannel并将其注册为executorChannel.spring-doc.cadn.net.cn

  • channel("output")注册DirectChannelbean 替换为output作为其名称,只要不存在具有此名称的 bean。spring-doc.cadn.net.cn

注意:前面的IntegrationFlow定义有效,并且其所有通道都应用于具有BridgeHandler实例。spring-doc.cadn.net.cn

请小心使用相同的内联通道定义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();
}

该错误示例的结果是以下异常:spring-doc.cadn.net.cn

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实例。spring-doc.cadn.net.cn