-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Document Kotlin DSL #3210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
garyrussell
merged 2 commits into
spring-projects:master
from
artembilan:kotlin_dsl_docs
Mar 10, 2020
Merged
Document Kotlin DSL #3210
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,96 @@ | ||||||
[[kotlin-dsl]] | ||||||
== Kotlin DSL | ||||||
|
||||||
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. | ||||||
|
||||||
All you need to get started is just an import for `org.springframework.integration.dsl.integrationFlow` - an overloaded global function for Kotlin DSL. | ||||||
|
||||||
For `IntegrationFlow` definitions as lambdas we typically don't need anything else from Kotlin and just declare a bean like this: | ||||||
|
||||||
==== | ||||||
[source, kotlin] | ||||||
---- | ||||||
@Bean | ||||||
fun oddFlow() = | ||||||
IntegrationFlow { flow -> | ||||||
flow.handle<Any> { _, _ -> "odd" } | ||||||
} | ||||||
---- | ||||||
==== | ||||||
|
||||||
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. | ||||||
|
||||||
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: | ||||||
|
||||||
==== | ||||||
[source, kotlin] | ||||||
---- | ||||||
@Bean | ||||||
fun flowLambda() = | ||||||
integrationFlow { | ||||||
filter<String> { it === "test" } | ||||||
wireTap { | ||||||
handle { println(it.payload) } | ||||||
} | ||||||
transform<String, String> { it.toUpperCase() } | ||||||
} | ||||||
---- | ||||||
==== | ||||||
|
||||||
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. | ||||||
See more overloaded `integrationFlow()` variants below. | ||||||
|
||||||
Many other scenarios require an `IntegrationFlow` to be started from source of data (e.g. `JdbcPollingChannelAdapter`, `JmsInboundGateway` or just an existing `MessageChannel`). | ||||||
For this purpose, the Spring Integration Java DSL provides an `IntegrationFlows` factory with its large number of overloaded `from()` methods. | ||||||
This factory can be used in Kotlin as well: | ||||||
|
||||||
==== | ||||||
[source, kotlin] | ||||||
---- | ||||||
@Bean | ||||||
fun flowFromSupplier() = | ||||||
IntegrationFlows.from<String>({ "bar" }) { e -> e.poller { p -> p.fixedDelay(10).maxMessagesPerPoll(1) } } | ||||||
.channel { c -> c.queue("fromSupplierQueue") } | ||||||
.get() | ||||||
---- | ||||||
==== | ||||||
|
||||||
But unfortunately not all `from()` methods are compatible with Kotlin structures. | ||||||
To fix the gap, this project provides a Kotlin DSL around an `IntegrationFlows` factory. | ||||||
It is implemented as a set of overloaded `integrationFlow()` functions. | ||||||
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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
For example: | ||||||
|
||||||
==== | ||||||
[source, kotlin] | ||||||
---- | ||||||
@Bean | ||||||
fun functionFlow() = | ||||||
integrationFlow<Function<String, String>>({ beanName("functionGateway") }) { | ||||||
transform<String, String> { it.toUpperCase() } | ||||||
} | ||||||
|
||||||
@Bean | ||||||
fun messageSourceFlow() = | ||||||
integrationFlow(MessageProcessorMessageSource { "testSource" }, | ||||||
{ poller { it.fixedDelay(10).maxMessagesPerPoll(1) } }) { | ||||||
channel { queue("fromSupplierQueue") } | ||||||
} | ||||||
---- | ||||||
==== | ||||||
|
||||||
In addition, Kotlin extensions are provided for the Java DSL API which needs some refinement for Kotlin structures. | ||||||
For example `IntegrationFlowDefinition<*>` requires a reifying for many methods with `Class<P>` argument: | ||||||
|
||||||
==== | ||||||
[source, kotlin] | ||||||
---- | ||||||
@Bean | ||||||
fun convertFlow() = | ||||||
integrationFlow("convertFlowInput") { | ||||||
convert<TestPojo>() | ||||||
} | ||||||
---- | ||||||
==== | ||||||
|
||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.