如需使用最新稳定版本,请使用 Spring Integration 7.0.4spring-doc.cadn.net.cn

Groovy DSL

Groovy DSL 是对 Java DSL 的封装和扩展。 我们在此追求的主要目标是,在保持与现有 Java DSL 互操作性以及利用部分 Groovy 扩展或语言特定结构的基础上,使基于 Groovy 的 Spring Integration 开发尽可能顺畅和直观。 该实现属于 Groovy 支持 模块的一部分。spring-doc.cadn.net.cn

开始所需的一切只是一个对 import static org.springframework.integration.groovy.dsl.IntegrationGroovyDsl.integrationFlow 的导入——这是一个包含用于 Groovy DSL 的重载工厂方法的类。spring-doc.cadn.net.cn

对于 IntegrationFlow 个作为 Lambda 表达式的定义,我们通常不需要 Groovy 的其他功能,只需像这样声明一个 Bean:spring-doc.cadn.net.cn

@Bean
IntegrationFlow oddFlow() {
    { IntegrationFlowDefinition flow ->
	    flow.handle(Object, { p, h -> 'odd' })
    }
}

在这种情况下,Groovy 理解该闭包应转换为一个 IntegrationFlow 匿名实例,目标 Java DSL 处理器会将此结构正确解析为 Java 对象。spring-doc.cadn.net.cn

作为上述构造的替代方案,并且为了与下面解释的用例保持一致,spring-integration-groovy 模块提供了一个特定于 Groovy 的 DSL,用于以构建器模式风格声明集成流程:spring-doc.cadn.net.cn

@Bean
flowLambda() {
    integrationFlow {
        filter String, { it == 'test' }, { id 'filterEndpoint' }
        wireTap integrationFlow {
            channel { queue 'wireTapChannel' }
        }
        delay {
		    messageGroupId 'delayGroup'
		    defaultDelay 100
        }
        transform {
		    transformer { it.toUpperCase() }
            expectedType String
        }
    }
}

这样一个全局 integrationFlow() 函数期望在构建器风格中为 GroovyIntegrationFlowDefinitionIntegrationFlowDefinition 的 Groovy 包装器)提供一个闭包,并生成一个常规的 IntegrationFlow Lambda 实现。 请参阅下方更多的重载 integrationFlow() 变体。spring-doc.cadn.net.cn

许多其他场景需要从数据源(例如 IntegrationFlowJdbcPollingChannelAdapterJmsInboundGateway,或只是一个现有的 MessageChannel)启动一个 IntegrationFlow。 为此,Spring Integration Java DSL 提供了一个 from() 工厂,其中包含多个重载的 6 方法。 该工厂也可以在 Groovy 中使用:spring-doc.cadn.net.cn

@Bean
flowFromSupplier() {
    IntegrationFlow.fromSupplier({ 'bar' }) { e -> e.poller { p -> p.fixedDelay(10).maxMessagesPerPoll(1) } }
            .channel({ c -> c.queue('fromSupplierQueue') } as Function)
            .get()
}

但不幸的是,并非所有 from() 方法都与 Groovy 结构兼容。 为解决此问题,Spring Integration 在 IntegrationFlow 工厂周围提供了一个 Groovy DSL 工厂。 它被实现为一组重载的 integrationFlow() 函数。 通过为 GroovyIntegrationFlowDefinition 提供消费者,将流的其余部分声明为 IntegrationFlow 闭包,以复用上述经验,同时避免在末尾进行 get() 调用的需求。 例如:spring-doc.cadn.net.cn

@Bean
functionFlow() {
    integrationFlow Function<byte[], String>,
            { beanName 'functionGateway' },
            {
                transform {
		            transformer Transformers.objectToString()
                    id 'objectToStringTransformer'
                }
                transform {
		            transformer { it.toUpperCase() }
                    expectedType String
                }
                splitWith {
                    expectedType Message<?>
                    function { it.payload }
                }
                splitWith {
                    expectedType Object
                    id 'splitterEndpoint'
                    function { it }
                }
                resequence()
                aggregate {
                    id 'aggregator'
                    outputProcessor { it.one }
                }
            }
}

@Bean
someFlow() {
    integrationFlow ({ 'test' },
            {
                poller { it.trigger new OnlyOnceTrigger() }
                id 'pollingSource'
            })
            {
                log LoggingHandler.Level.WARN, 'test.category'
                channel { queue 'pollerResultChannel' }
            }
}