此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Integration 6.5.1! |
Kotlin DSL
Kotlin DSL 是 Java DSL 的包装器和扩展,旨在使 Kotlin 上的 Spring Integration 开发尽可能流畅和直接,并具有与现有 Java API 和 Kotlin 语言特定结构的互作性。
您只需导入即可开始org.springframework.integration.dsl.integrationFlow
- Kotlin DSL 的重载全局函数。
为IntegrationFlow
定义为 lambda,我们通常不需要 Kotlin 中的任何其他内容,只需声明一个这样的 bean:
@Bean
fun oddFlow() =
IntegrationFlow { flow ->
flow.handle<Any> { _, _ -> "odd" }
}
在这种情况下,Kotlin 理解 lambda 应该转换为IntegrationFlow
匿名实例和目标 Java DSL 处理器将此结构正确解析为 Java 对象。
作为上述构造的替代方法,为了与下面解释的用例保持一致,应使用特定于 Kotlin 的 DSL 在构建器模式样式中声明集成流:
@Bean
fun flowLambda() =
integrationFlow {
filter<String> { it === "test" }
wireTap {
handle { println(it.payload) }
}
transform<String> { it.toUpperCase() }
}
这样的全球integrationFlow()
函数需要一个构建器样式的 lambdaKotlinIntegrationFlowDefinition
(用于IntegrationFlowDefinition
) 并产生常规的IntegrationFlow
lambda 实现。查看更多重载integrationFlow()
下面的变体。
许多其他方案需要IntegrationFlow
从数据源(例如JdbcPollingChannelAdapter
,JmsInboundGateway
或者只是现有的MessageChannel
). 为此,Spring Integration Java DSL 提供了一个IntegrationFlow
流畅的 API 及其大量重载from()
方法。 此 API 也可以在 Kotlin 中使用:
@Bean
fun flowFromSupplier() =
IntegrationFlow.fromSupplier({ "bar" }) { e -> e.poller { p -> p.fixedDelay(10).maxMessagesPerPoll(1) } }
.channel { c -> c.queue("fromSupplierQueue") }
.get()
但不幸的是,并非全部from()
方法与 Kotlin 结构兼容。为了解决这一差距,该项目围绕IntegrationFlow
流畅的 API。它作为一组重载的integrationFlow()
功能。 与消费者一起KotlinIntegrationFlowDefinition
将流的其余部分声明为IntegrationFlow
lambda 重用上述经验,同时避免get()
最后打电话。 例如:
@Bean
fun functionFlow() =
integrationFlow<Function<String, String>>({ beanName("functionGateway") }) {
transform<String> { it.toUpperCase() }
}
@Bean
fun messageSourceFlow() =
integrationFlow(MessageProcessorMessageSource { "testSource" },
{ poller { it.fixedDelay(10).maxMessagesPerPoll(1) } }) {
channel { queue("fromSupplierQueue") }
}
此外,还为 Java DSL API 提供了 Kotlin 扩展,这需要对 Kotlin 结构进行一些改进。 例如IntegrationFlowDefinition<*>
需要对许多方法进行重新化Class<P>
论点:
@Bean
fun convertFlow() =
integrationFlow("convertFlowInput") {
convert<TestPojo>()
}
具体化型可以是一个整体Message<*> 如果运算符的 lambda 中也需要访问标头。 |