春季集成 - 如何使用kotlin DSL根据标头值路由

发布于 2025-01-29 15:36:12 字数 534 浏览 5 评论 0原文

假设我有一个标题,上面有一个名为“计数”的属性,我该如何根据该值路由?

在Kotlin DSL中有路由函数,但是我不确定如何将其配置为HeaderValuerouter

文档对Java DSL说了以下内容,但是我不清楚我如何如何可以握住路由器块中的标题。

有人有例子(Kotlin和Java DSL)吗?

春季集成本地提供专业路由器类型, 包括:

headervaluerouter

PayloadTyperouter

异常

coverentlistrouter

xpathrouter

与许多其他DSL IntegrationFlowBuilder EIP方法一样,路由() 方法可以应用任何AbstractMessagerouter实现或用于 便利性,作为Spel表达式的弦或Ref方法对。在 此外,您可以使用lambda配置Route(),然后使用lambda 消费者< Routerspec>。

Assuming I've got a header with a property called "count" on it, how do I route based on that value ?

In the Kotlin DSL there is the route function, but I'm unsure how to configure it to be a HeaderValueRouter

The documentation says the following for the Java DSL,but it isn't clear to me how I can get hold of the headers within the router block.

Does anyone have an example (Kotlin and Java DSLs ) ?

Spring Integration natively provides specialized router types,
including:

HeaderValueRouter

PayloadTypeRouter

ExceptionTypeRouter

RecipientListRouter

XPathRouter

As with many other DSL IntegrationFlowBuilder EIP methods, the route()
method can apply any AbstractMessageRouter implementation or, for
convenience, a String as a SpEL expression or a ref-method pair. In
addition, you can configure route() with a lambda and use a lambda for
a Consumer<RouterSpec>.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不打扰别人 2025-02-05 15:36:12

这是一个单元测试,演示了Kotlin DSL的可能性:

@SpringJUnitConfig
@DirtiesContext
class RouterDslTests {

    @Autowired
    @Qualifier("routeByHeader.input")
    private lateinit var routeByHeaderInputChannel: MessageChannel

    @Autowired
    private lateinit var fooChannel: PollableChannel

    @Autowired
    private lateinit var barChannel: PollableChannel

    @Test
    fun `route by header`() {
        var message = MessageBuilder.withPayload("test1")
            .setHeader("my_route", "foo")
            .build()

        this.routeByHeaderInputChannel.send(message)

        var receive = this.fooChannel.receive(10_000)
        var payload = receive?.payload
        assertThat(payload)
            .isNotNull()
            .isEqualTo("test1")

        message = MessageBuilder.withPayload("test2")
            .setHeader("my_route", "bar")
            .build()

        this.routeByHeaderInputChannel.send(message)

        receive = this.barChannel.receive(10_000)
        payload = receive?.payload
        assertThat(payload)
            .isNotNull()
            .isEqualTo("test2")
    }

    @Configuration
    @EnableIntegration
    class Config {

        @Bean
        fun routeByHeader() =
            integrationFlow {
                route<Message<*>, String>({ it.headers["my_route"].toString() }) {
                    suffix("Channel")
                }
            }

        @Bean
        fun fooChannel() = QueueChannel()

        @Bean
        fun barChannel() = QueueChannel()
    }

}

您只需要执行route()函数来处理整个message> Message作为输入。然后,您可以在Lambda中访问其标题。

这可能无法在Kotlin DSL文档中解释,但这是Java DSL中的内容: https://docs.spring.io/spring-integration/docs/current/current/reference/reference/html/html.html.html#java-dsl-w.jjava-dsl-class-class-class-class-cast-cast-cast-cast-cast

Here is a Unit test which demonstrates how your request is possible with Kotlin DSL:

@SpringJUnitConfig
@DirtiesContext
class RouterDslTests {

    @Autowired
    @Qualifier("routeByHeader.input")
    private lateinit var routeByHeaderInputChannel: MessageChannel

    @Autowired
    private lateinit var fooChannel: PollableChannel

    @Autowired
    private lateinit var barChannel: PollableChannel

    @Test
    fun `route by header`() {
        var message = MessageBuilder.withPayload("test1")
            .setHeader("my_route", "foo")
            .build()

        this.routeByHeaderInputChannel.send(message)

        var receive = this.fooChannel.receive(10_000)
        var payload = receive?.payload
        assertThat(payload)
            .isNotNull()
            .isEqualTo("test1")

        message = MessageBuilder.withPayload("test2")
            .setHeader("my_route", "bar")
            .build()

        this.routeByHeaderInputChannel.send(message)

        receive = this.barChannel.receive(10_000)
        payload = receive?.payload
        assertThat(payload)
            .isNotNull()
            .isEqualTo("test2")
    }

    @Configuration
    @EnableIntegration
    class Config {

        @Bean
        fun routeByHeader() =
            integrationFlow {
                route<Message<*>, String>({ it.headers["my_route"].toString() }) {
                    suffix("Channel")
                }
            }

        @Bean
        fun fooChannel() = QueueChannel()

        @Bean
        fun barChannel() = QueueChannel()
    }

}

What you just need is to enforce a route() function to deal with the whole Message as input. Then you have access to its headers in the lambda.

This might not be explained in the Kotlin DSL docs, but here is something in Java DSL: https://docs.spring.io/spring-integration/docs/current/reference/html/dsl.html#java-dsl-class-cast

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文