此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Integration 6.3.1Spring中文文档

此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Integration 6.3.1Spring中文文档

除了 with EIP 方法外,Java DSL 还提供了一个流畅的 API 来配置实例。 为此,提供了建筑厂。 以下示例演示如何使用它:IntegrationFlowBuilderMessageChannelMessageChannelsSpring中文文档

@Bean
public PriorityChannelSpec priorityChannel() {
    return MessageChannels.priority(this.mongoDbChannelMessageStore, "priorityGroup")
                        .interceptor(wireTap());
}

在 EIP 方法中可以使用相同的构建器工厂 from to wire endpoint,类似于在 XML 配置中连接 / 对。 缺省情况下,端点与 Bean 名称基于以下模式的实例连接:. 此规则也适用于由内联生成器出厂使用生成的未命名通道。 但是,所有方法都有一个变体,该变体可以识别可用于设置实例的 Bean 名称。 引用 和 可用作 Bean-Method 调用。 以下示例显示了使用弹性公网IP方法的可能方法:MessageChannelschannel()IntegrationFlowBuilderinput-channeloutput-channelDirectChannel[IntegrationFlow.beanName].channel#[channelNameIndex]MessageChannelsMessageChannelschannelIdMessageChannelMessageChannelbeanNamechannel()Spring中文文档

@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 的 ID,或创建一个”。MessageChannelSpring中文文档

  • fixedSubscriberChannel()生成 的实例,并使用名称 注册它。FixedSubscriberChannelchannelFlow.channel#0Spring中文文档

  • channel("queueChannel")工作方式相同,但使用现有 Bean。queueChannelSpring中文文档

  • channel(publishSubscribe())是 Bean-method 引用。Spring中文文档

  • channel(MessageChannels.executor("executorChannel", this.taskExecutor))是向 公开并将其注册为 的 。IntegrationFlowBuilderIntegrationComponentSpecExecutorChannelexecutorChannelSpring中文文档

  • channel("output")注册 Bean 作为其名称,只要不存在具有此名称的 Bean。DirectChanneloutputSpring中文文档

注意:上述定义有效,其所有通道都应用于具有实例的终端节点。IntegrationFlowBridgeHandlerSpring中文文档

请注意,通过工厂从不同实例使用相同的内联通道定义。 即使 DSL 解析器将不存在的对象注册为 Bean,它也无法从不同的容器中确定相同的对象 ()。 以下示例是错误的:MessageChannelsIntegrationFlowMessageChannelIntegrationFlow
请注意,通过工厂从不同实例使用相同的内联通道定义。 即使 DSL 解析器将不存在的对象注册为 Bean,它也无法从不同的容器中确定相同的对象 ()。 以下示例是错误的:MessageChannelsIntegrationFlowMessageChannelIntegrationFlow
@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中文文档

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 方法。@BeanIntegrationFlowSpring中文文档