|
| 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 | + |
0 commit comments