Skip to content

Commit 36f96ec

Browse files
authored
Document Kotlin DSL (#3210)
* Document Kotlin DSL * Remove unused imports in `FunctionsTests.kt` * * Fix typos in Docs
1 parent c45cc66 commit 36f96ec

File tree

4 files changed

+104
-2
lines changed

4 files changed

+104
-2
lines changed

spring-integration-core/src/test/kotlin/org/springframework/integration/function/FunctionsTests.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ import org.springframework.integration.annotation.Transformer
3535
import org.springframework.integration.channel.DirectChannel
3636
import org.springframework.integration.channel.QueueChannel
3737
import org.springframework.integration.config.EnableIntegration
38-
import org.springframework.integration.dsl.IntegrationFlows
3938
import org.springframework.integration.dsl.integrationFlow
4039
import org.springframework.integration.endpoint.SourcePollingChannelAdapter
4140
import org.springframework.integration.gateway.GatewayProxyFactoryBean
42-
import org.springframework.integration.test.util.OnlyOnceTrigger
4341
import org.springframework.messaging.Message
4442
import org.springframework.messaging.MessageChannel
4543
import org.springframework.messaging.PollableChannel

src/reference/asciidoc/dsl.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
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.
55

6+
(See also <<./kotlin-dsl.adoc#kotlin-dsl,Kotlin DSL>>.)
7+
68
The Java DSL for Spring Integration is essentially a facade for Spring Integration.
79
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.
810
We also use and support lambdas (available with Java 8) to further simplify Java configuration.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
[[kotlin-dsl]]
2+
== Kotlin DSL
3+
4+
The Kotlin DSL is a wrapper and extension to <<./dsl.adoc#java-dsl,Java DSL>> and aimed to make Spring Integration development on Kotlin as smooth and straightforward as possible with interoperability with the existing Java API and Kotlin language-specific structures.
5+
6+
All you need to get started is just an import for `org.springframework.integration.dsl.integrationFlow` - an overloaded global function for Kotlin DSL.
7+
8+
For `IntegrationFlow` definitions as lambdas we typically don't need anything else from Kotlin and just declare a bean like this:
9+
10+
====
11+
[source, kotlin]
12+
----
13+
@Bean
14+
fun oddFlow() =
15+
IntegrationFlow { flow ->
16+
flow.handle<Any> { _, _ -> "odd" }
17+
}
18+
----
19+
====
20+
21+
In this case Kotlin understands that the lambda should be translated into `IntegrationFlow` anonymous instance and target Java DSL processor parses this construction properly into Java objects.
22+
23+
As an alternative to the construction above and for consistency with use-cases explained below, a Kotlin-specif DSL should be used for declaring integration flows in the *builder* pattern style:
24+
25+
====
26+
[source, kotlin]
27+
----
28+
@Bean
29+
fun flowLambda() =
30+
integrationFlow {
31+
filter<String> { it === "test" }
32+
wireTap {
33+
handle { println(it.payload) }
34+
}
35+
transform<String, String> { it.toUpperCase() }
36+
}
37+
----
38+
====
39+
40+
Such a global `integrationFlow()` function expects a lambda in builder style for a `KotlinIntegrationFlowDefinition` (a Kotlin wrapper for the `IntegrationFlowDefinition`) and produces a regular `IntegrationFlow` lambda implementation.
41+
See more overloaded `integrationFlow()` variants below.
42+
43+
Many other scenarios require an `IntegrationFlow` to be started from source of data (e.g. `JdbcPollingChannelAdapter`, `JmsInboundGateway` or just an existing `MessageChannel`).
44+
For this purpose, the Spring Integration Java DSL provides an `IntegrationFlows` factory with its large number of overloaded `from()` methods.
45+
This factory can be used in Kotlin as well:
46+
47+
====
48+
[source, kotlin]
49+
----
50+
@Bean
51+
fun flowFromSupplier() =
52+
IntegrationFlows.from<String>({ "bar" }) { e -> e.poller { p -> p.fixedDelay(10).maxMessagesPerPoll(1) } }
53+
.channel { c -> c.queue("fromSupplierQueue") }
54+
.get()
55+
----
56+
====
57+
58+
But unfortunately not all `from()` methods are compatible with Kotlin structures.
59+
To fix the gap, this project provides a Kotlin DSL around an `IntegrationFlows` factory.
60+
It is implemented as a set of overloaded `integrationFlow()` functions.
61+
With a consumer for a `KotlinIntegrationFlowDefinition` to declare the rest of the flow as an `IntegrationFlow` lambda to reuse the mentioned above experience and also avoid `get()` call in the end.
62+
For example:
63+
64+
====
65+
[source, kotlin]
66+
----
67+
@Bean
68+
fun functionFlow() =
69+
integrationFlow<Function<String, String>>({ beanName("functionGateway") }) {
70+
transform<String, String> { it.toUpperCase() }
71+
}
72+
73+
@Bean
74+
fun messageSourceFlow() =
75+
integrationFlow(MessageProcessorMessageSource { "testSource" },
76+
{ poller { it.fixedDelay(10).maxMessagesPerPoll(1) } }) {
77+
channel { queue("fromSupplierQueue") }
78+
}
79+
----
80+
====
81+
82+
In addition, Kotlin extensions are provided for the Java DSL API which needs some refinement for Kotlin structures.
83+
For example `IntegrationFlowDefinition<*>` requires a reifying for many methods with `Class<P>` argument:
84+
85+
====
86+
[source, kotlin]
87+
----
88+
@Bean
89+
fun convertFlow() =
90+
integrationFlow("convertFlowInput") {
91+
convert<TestPojo>()
92+
}
93+
----
94+
====
95+
96+

src/reference/asciidoc/whats-new.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ A new `IntegrationFlowExtension` API has been introduced to allow extension of t
3434
This also can be used to introduce customizers for any out-of-the-box `IntegrationComponentSpec` extensions.
3535
See <<./dsl.adoc#java-dsl-extensions,DSL Extensions>> for more information.
3636

37+
[[x5.3-kotlin-dsl]]
38+
==== Kotlin DSL
39+
40+
The Kotlin DSL for integration flow configurations has been introduced.
41+
See <<./kotlin-dsl.adoc#kotlin-dsl,Kotlin DSL Chapter>> for more information.
42+
3743
[[x5.3-reactive-request-handler-advice]]
3844
==== ReactiveRequestHandlerAdvice
3945

0 commit comments

Comments
 (0)