Migrate Groovy DSL extension project (#3914)
* Migrate Groovy DSL extension project * Upgrade to Groovy `4.0.5` * Apply `groovy` plugin for Groovy module to be able to compile Groovy classes for DSL * Document new feature * * Restore `lambdaWrapper` for the `GroovyIntegrationFlowDefinition.handle()` to be able to preserve a generic argument type * * Migrate more Spock test methods to JUnit style * * Add missed `assert` to Groovy tests * Fix language in docs Co-authored-by: Gary Russell <grussell@vmware.com> * * Fix Copyright and `@since` in new classes * Remove deprecated `IntegrationFlows` class mentions in the `groovy-dsl.adoc` Co-authored-by: Gary Russell <grussell@vmware.com>
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
The Spring Integration Java configuration and DSL provides a set of convenient builders and a fluent API that lets you configure Spring Integration message flows from Spring `@Configuration` classes.
|
||||
|
||||
(See also <<./kotlin-dsl.adoc#kotlin-dsl,Kotlin DSL>>.)
|
||||
(See also <<./groovy-dsl.adoc#groovy-dsl,Groovy DSL>>.)
|
||||
|
||||
The Java DSL for Spring Integration is essentially a facade for Spring Integration.
|
||||
The DSL provides a simple way to embed Spring Integration Message Flows into your application by using the fluent `Builder` pattern together with existing Java configuration from Spring Framework and Spring Integration.
|
||||
|
||||
103
src/reference/asciidoc/groovy-dsl.adoc
Normal file
103
src/reference/asciidoc/groovy-dsl.adoc
Normal file
@@ -0,0 +1,103 @@
|
||||
[[groovy-dsl]]
|
||||
== Groovy DSL
|
||||
|
||||
The Groovy DSL is a wrapper and extension to <<./dsl.adoc#java-dsl,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.adoc#groovy,Groovy Support>> module.
|
||||
|
||||
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.
|
||||
|
||||
For `IntegrationFlow` definitions as lambdas we typically don't need anything else from Groovy and just declare a bean like this:
|
||||
|
||||
====
|
||||
[source, groovy]
|
||||
----
|
||||
@Bean
|
||||
IntegrationFlow oddFlow() {
|
||||
{ IntegrationFlowDefinition flow ->
|
||||
flow.handle(Object, { p, h -> 'odd' })
|
||||
}
|
||||
}
|
||||
----
|
||||
====
|
||||
|
||||
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.
|
||||
|
||||
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:
|
||||
|
||||
====
|
||||
[source, groovy]
|
||||
----
|
||||
@Bean
|
||||
flowLambda() {
|
||||
integrationFlow {
|
||||
filter String, { it == 'test' }, { id 'filterEndpoint' }
|
||||
wireTap integrationFlow {
|
||||
channel { queue 'wireTapChannel' }
|
||||
}
|
||||
delay 'delayGroup', { defaultDelay 100 }
|
||||
transform String, { it.toUpperCase() }
|
||||
}
|
||||
}
|
||||
----
|
||||
====
|
||||
|
||||
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.
|
||||
|
||||
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:
|
||||
|
||||
====
|
||||
[source, groovy]
|
||||
----
|
||||
@Bean
|
||||
flowFromSupplier() {
|
||||
IntegrationFlow.fromSupplier({ 'bar' }) { e -> e.poller { p -> p.fixedDelay(10).maxMessagesPerPoll(1) } }
|
||||
.channel({ c -> c.queue('fromSupplierQueue') } as Function)
|
||||
.get()
|
||||
}
|
||||
----
|
||||
====
|
||||
|
||||
But unfortunately not all `from()` methods are compatible with Groovy structures.
|
||||
To solve this, Spring Integration provides a Groovy DSL factory around the `IntegrationFlows` 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:
|
||||
|
||||
====
|
||||
[source, groovy]
|
||||
----
|
||||
@Bean
|
||||
functionFlow() {
|
||||
integrationFlow Function<byte[], String>,
|
||||
{ beanName 'functionGateway' },
|
||||
{
|
||||
transform Transformers.objectToString(), { id 'objectToStringTransformer' }
|
||||
transform String, { it.toUpperCase() }
|
||||
split Message<?>, { it.payload }
|
||||
split Object, { it }, { id 'splitterEndpoint' }
|
||||
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' }
|
||||
}
|
||||
}
|
||||
----
|
||||
====
|
||||
@@ -1,5 +1,5 @@
|
||||
[[groovy]]
|
||||
=== Groovy support
|
||||
=== Groovy Support
|
||||
|
||||
In Spring Integration 2.0, we added Groovy support, letting you use the Groovy scripting language to provide the logic for various integration components -- similar to the way the Spring Expression Language (SpEL) is supported for routing, transformation, and other integration concerns.
|
||||
For more information about Groovy, see the Groovy documentation, which you can find on the https://groovy-lang.org/[project website].
|
||||
@@ -23,6 +23,8 @@ compile "org.springframework.integration:spring-integration-groovy:{project-vers
|
||||
----
|
||||
====
|
||||
|
||||
In addition, starting with version 6.0, a <<./groovy-dsl.adoc#groovy-dsl,Groovy DSL>> for integration flow configurations is provided.
|
||||
|
||||
[[groovy-config]]
|
||||
==== Groovy Configuration
|
||||
|
||||
|
||||
@@ -17,6 +17,9 @@ In general the project has been moved to Java 17 baseline and migrated from Java
|
||||
[[x6.0-new-components]]
|
||||
=== New Components
|
||||
|
||||
A Groovy DSL implementation for integration flow definitions has been added.
|
||||
See <<./groovy-dsl.adoc#groovy-dsl,Groovy DSL>> for more information.
|
||||
|
||||
[[x6.0-mqtt]]
|
||||
==== MQTT ClientManager
|
||||
|
||||
|
||||
Reference in New Issue
Block a user