Kotlin DSL 是 JavaDSL的包装器和扩展,旨在使 Kotlin 上的 Spring Integration 开发尽可能流畅和直接,并与现有的 Java API 和 Kotlin 语言特定结构互操作。
您只需要导入 - Kotlin DSL 的重载全局函数即可开始。org.springframework.integration.dsl.integrationFlow
对于 lambda 的定义,我们通常不需要来自 Kotlin 的任何其他东西,只需像这样声明一个 bean:IntegrationFlow
@Bean
fun oddFlow() =
IntegrationFlow { flow ->
flow.handle<Any> { _, _ -> "odd" }
}
在这种情况下,Kotlin 理解 lambda 应该转换为匿名实例,目标 Java DSL 处理器将此构造正确解析为 Java 对象。IntegrationFlow
作为上述构造的替代方案,并且为了与下面解释的用例保持一致,应该使用 Kotlin 特定的 DSL 来声明构建器模式样式中的集成流:
@Bean
fun flowLambda() =
integrationFlow {
filter<String> { it === "test" }
wireTap {
handle { println(it.payload) }
}
transform<String, String> { it.toUpperCase() }
}
这样的全局函数需要构建器风格的lambda(Kotlin包装器)并产生常规lambda实现。 请参阅下面的更多重载变体。integrationFlow()
KotlinIntegrationFlowDefinition
IntegrationFlowDefinition
IntegrationFlow
integrationFlow()
许多其他场景需要从数据源(例如,或只是一个现有的)启动anto。 为此,Spring Integration Java DSL 提供了具有大量重载方法的流畅 API。 此 API 也可以在 Kotlin 中使用:IntegrationFlow
JdbcPollingChannelAdapter
JmsInboundGateway
MessageChannel
IntegrationFlow
from()
@Bean
fun flowFromSupplier() =
IntegrationFlow.fromSupplier({ "bar" }) { e -> e.poller { p -> p.fixedDelay(10).maxMessagesPerPoll(1) } }
.channel { c -> c.queue("fromSupplierQueue") }
.get()
但不幸的是,并非所有方法都与 Kotlin 结构兼容。 为了弥补这一差距,该项目围绕流利的API提供了一个Kotlin DSL。 它作为一组重载函数实现。 使用 ato 的使用者将流的其余部分声明为 anlambda,以重用上述体验并最终避免调用。 例如:from()
IntegrationFlow
integrationFlow()
KotlinIntegrationFlowDefinition
IntegrationFlow
get()
@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") }
}
此外,还为Java DSL API提供了Kotlin扩展,该API需要对Kotlin结构进行一些改进。 例如,需要对许多带有参数的方法进行重新调整:IntegrationFlowDefinition<*>
Class<P>
@Bean
fun convertFlow() =
integrationFlow("convertFlowInput") {
convert<TestPojo>()
}
如果还需要访问运算符的 lambda 中的标头,则化类型可以是整体。 |