Groovy DSL
Groovy DSL 是对 Java DSL 的包装和扩展。我们在此追求的主要目标是,通过与现有的 Java DSL 和一些 Groovy 扩展或特定于语言的结构互操作,让基于 Groovy 的 Spring Integration 开发变得尽可能地平稳且简单明了。此实现是 Groovy Support 模块的一部分。
The Groovy DSL is a wrapper and extension to Java DSL. The main goal we pursue here is to make Spring Integration development on Groovy as smooth and straightforward as is it possible with interoperability with existing Java DSL and some Groovy extensions or language-specific structures. The implementation is a part of Groovy Support module.
您开始所需要做的只是导入 import static org.springframework.integration.groovy.dsl.IntegrationGroovyDsl.integrationFlow
- 一个包含 Groovy DSL 的重载工厂方法的类。
All you need to get started is just an import for import static org.springframework.integration.groovy.dsl.IntegrationGroovyDsl.integrationFlow
- a class containing overloaded factory methods for the Groovy DSL.
对于作为 Lambda 的 IntegrationFlow
定义,我们通常不需要 Groovy 中的任何其他内容,只需像这样声明一个 bean:
For IntegrationFlow
definitions as lambdas we typically don’t need anything else from Groovy and just declare a bean like this:
@Bean
IntegrationFlow oddFlow() {
{ IntegrationFlowDefinition flow ->
flow.handle(Object, { p, h -> 'odd' })
}
}
在这种情况下,Groovy 理解应将闭包转换为 IntegrationFlow
匿名实例,并且目标 Java DSL 处理器将正确的此结构解析为 Java 对象。
In this case Groovy understands that the closure should be translated into an IntegrationFlow
anonymous instance and the target Java DSL processor parses this construction properly into Java objects.
作为上述结构的替代方案,并为了与下面解释的用例保持一致,spring-integration-groovy
模块提供了一个 Groovy 特定的 DSL,用于以*生成器*模式样式声明集成流:
As an alternative to the construction above and for consistency with use-cases explained below, the spring-integration-groovy
module provides a Groovy-specific DSL for declaring integration flows in a builder pattern style:
@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()
函数期望以生成器样式获得闭包来获取 GroovyIntegrationFlowDefinition
(IntegrationFlowDefinition
的 Groovy 封装),并生成常规 IntegrationFlow
lambda 实现。请参阅下面更多重载的 integrationFlow()
变体。
Such a global integrationFlow()
function expects a closure in the builder style for a GroovyIntegrationFlowDefinition
(a Groovy wrapper for the IntegrationFlowDefinition
) and produces a regular IntegrationFlow
lambda implementation.
See more overloaded integrationFlow()
variants below.
许多其他场景要求从数据源中启动 IntegrationFlow
(例如 JdbcPollingChannelAdapter
、JmsInboundGateway
或仅存在的 MessageChannel
)。为此,Spring Integration Java DSL 提供了一个 IntegrationFlow
工厂,其中包含许多重载的 from()
方法。此工厂也可用于 groovy:
Many other scenarios require an IntegrationFlow
to be started from the source of data (e.g. JdbcPollingChannelAdapter
, JmsInboundGateway
or just an existing MessageChannel
).
For this purpose, Spring Integration Java DSL provides an IntegrationFlow
factory with a number of overloaded from()
methods.
This factory can be used in groovy as well:
@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()
调用。例如:
But unfortunately not all from()
methods are compatible with Groovy structures.
To solve this, Spring Integration provides a Groovy DSL factory around the IntegrationFlow
factory.
It is implemented as a set of overloaded integrationFlow()
functions.
With a consumer for a GroovyIntegrationFlowDefinition
to declare the remainder of the flow as an IntegrationFlow
closure to reuse the mentioned above experience and also avoid the need for a get()
call in the end.
For example:
@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' }
}
}