Skip to content

Commit e309ebb

Browse files
artembilangaryrussell
authored andcommitted
GH-3288: Add Kotlin DSL transform(Transformer)
Fixes #3288 * For better end-user experience with Kotlin DSL and get a gain from existing `Transformer` implementations add a `transform(Transformer)` EI-method into the `KotlinIntegrationFlowDefinition` * Also add `filter(MessageSelector)` for any out-of-the-box `MessageSelector` **Cherry-pick to 5.3.x**
1 parent 28a5be7 commit e309ebb

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

spring-integration-core/src/main/kotlin/org/springframework/integration/dsl/KotlinIntegrationFlowDefinition.kt

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import org.springframework.integration.transformer.ClaimCheckOutTransformer
5050
import org.springframework.integration.transformer.HeaderFilter
5151
import org.springframework.integration.transformer.MessageTransformingHandler
5252
import org.springframework.integration.transformer.MethodInvokingTransformer
53+
import org.springframework.integration.transformer.Transformer
5354
import org.springframework.messaging.Message
5455
import org.springframework.messaging.MessageChannel
5556
import org.springframework.messaging.MessageHandler
@@ -287,8 +288,20 @@ class KotlinIntegrationFlowDefinition(@PublishedApi internal val delegate: Integ
287288
this.delegate.controlBus(endpointConfigurer)
288289
}
289290

291+
292+
/**
293+
* Populate the [Transformer] EI Pattern specific [MessageHandler] implementation
294+
* for the provided `Transformer` instance.
295+
* @since 5.3.1
296+
*/
297+
fun transform(transformer: Transformer,
298+
endpointConfigurer: GenericEndpointSpec<MessageTransformingHandler>.() -> Unit = {}) {
299+
300+
this.delegate.transform(transformer, Consumer { endpointConfigurer(it) })
301+
}
302+
290303
/**
291-
* Populate the `Transformer` EI Pattern specific [MessageHandler] implementation
304+
* Populate the [Transformer] EI Pattern specific [MessageHandler] implementation
292305
* for the SpEL [Expression].
293306
*/
294307
fun transform(expression: String,
@@ -298,15 +311,15 @@ class KotlinIntegrationFlowDefinition(@PublishedApi internal val delegate: Integ
298311
}
299312

300313
/**
301-
* Populate the `MessageTransformingHandler` for the [MethodInvokingTransformer]
314+
* Populate the [MessageTransformingHandler] for the [MethodInvokingTransformer]
302315
* to invoke the service method at runtime.
303316
*/
304317
fun transform(service: Any, methodName: String? = null) {
305318
this.delegate.transform(service, methodName)
306319
}
307320

308321
/**
309-
* Populate the `MessageTransformingHandler` for the [MethodInvokingTransformer]
322+
* Populate the [MessageTransformingHandler] for the [MethodInvokingTransformer]
310323
* to invoke the service method at runtime.
311324
*/
312325
fun transform(service: Any, methodName: String?,
@@ -358,9 +371,23 @@ class KotlinIntegrationFlowDefinition(@PublishedApi internal val delegate: Integ
358371
*/
359372
fun filter(messageProcessorSpec: MessageProcessorSpec<*>,
360373
filterConfigurer: KotlinFilterEndpointSpec.() -> Unit = {}) {
374+
361375
this.delegate.filter(messageProcessorSpec) { filterConfigurer(KotlinFilterEndpointSpec(it)) }
362376
}
363377

378+
379+
/**
380+
* Populate a [MessageFilter] with the provided [MessageSelector].
381+
* In addition accept options for the integration endpoint using [KotlinFilterEndpointSpec].
382+
* @since 5.3.1
383+
*/
384+
fun filter(messageSelector: MessageSelector,
385+
filterConfigurer: KotlinFilterEndpointSpec.() -> Unit = {}) {
386+
387+
this.delegate.filter(Message::class.java, messageSelector,
388+
Consumer { filterConfigurer(KotlinFilterEndpointSpec(it)) })
389+
}
390+
364391
/**
365392
* Populate a [ServiceActivatingHandler] for the selected protocol specific
366393
* [MessageHandler] implementation from `Namespace Factory`:

spring-integration-core/src/test/kotlin/org/springframework/integration/dsl/KotlinDslTests.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import assertk.assertions.isEqualTo
2121
import assertk.assertions.isGreaterThanOrEqualTo
2222
import assertk.assertions.isInstanceOf
2323
import assertk.assertions.isNotNull
24+
import assertk.assertions.isTrue
2425
import assertk.assertions.size
2526
import org.junit.jupiter.api.Test
2627
import org.springframework.beans.factory.BeanFactory
@@ -37,6 +38,7 @@ import org.springframework.integration.dsl.context.IntegrationFlowContext
3738
import org.springframework.integration.endpoint.MessageProcessorMessageSource
3839
import org.springframework.integration.handler.LoggingHandler
3940
import org.springframework.integration.scheduling.PollerMetadata
41+
import org.springframework.integration.selector.UnexpiredMessageSelector
4042
import org.springframework.integration.support.MessageBuilder
4143
import org.springframework.integration.test.util.OnlyOnceTrigger
4244
import org.springframework.messaging.Message
@@ -88,11 +90,12 @@ class KotlinDslTests {
8890

8991
@Autowired
9092
@Qualifier("functionGateway")
91-
private lateinit var upperCaseFunction: Function<String, String>
93+
private lateinit var upperCaseFunction: Function<ByteArray, String>
9294

9395
@Test
9496
fun `uppercase function`() {
95-
assertThat(this.upperCaseFunction.apply("test")).isEqualTo("TEST")
97+
assertThat(beanFactory.containsBean("objectToStringTransformer")).isTrue()
98+
assertThat(this.upperCaseFunction.apply("test".toByteArray())).isEqualTo("TEST")
9699
}
97100

98101
@Autowired
@@ -225,7 +228,8 @@ class KotlinDslTests {
225228

226229
@Bean
227230
fun functionFlow() =
228-
integrationFlow<Function<String, String>>({ beanName("functionGateway") }) {
231+
integrationFlow<Function<ByteArray, String>>({ beanName("functionGateway") }) {
232+
transform(Transformers.objectToString()) { id("objectToStringTransformer") }
229233
transform<String> { it.toUpperCase() }
230234
split<Message<*>> { it.payload }
231235
split<String>({ it }) { id("splitterEndpoint") }
@@ -240,6 +244,7 @@ class KotlinDslTests {
240244
fun functionFlow2() =
241245
integrationFlow<Function<*, *>> {
242246
transform<String> { it.toLowerCase() }
247+
filter(UnexpiredMessageSelector())
243248
route<Message<*>, Any?>({ null }) { defaultOutputToParentFlow() }
244249
route<Message<*>> { m -> m.headers.replyChannel }
245250
}

0 commit comments

Comments
 (0)