Spring 集成 DSL:配置仅在参数匹配时处理的处理程序

编程入门 行业动态 更新时间:2024-10-28 05:24:01
本文介绍了Spring 集成 DSL:配置仅在参数匹配时处理的处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 Spring Integration DSL 配置.是否可以添加方法引用处理程序,以便仅在消息有效负载与处理程序参数类型匹配时才调用处理程序?

I am using Spring Integration DSL configs. Is it possible to add a method reference handler such that the handler is invoked only when the message payload matches the handler argument type?

例如:在下面的代码中,如果payload是MyObject2,Spring会在handleMessage处抛出ClassCastException.相反,我想要做的是绕过 handleMessage 并被 handleMessage2 接收.

For example: in the following code, if the payload is MyObject2, Spring will throw ClassCastException at handleMessage. Instead, what I want to do is to bypass handleMessage and get picked up by handleMessage2.

@Bean public IntegrationFlow myFlow() { return IntegrationFlows .from("myChannel") .handle(this::handleMessage) .handle(this::handleMessage2) ... } public MyObject2 handleMessage(MyObject o, Map headers){ ... } public MyObject2 handleMessage(MyObject2 o, Map headers){ ... }

推荐答案

.handle() 背后有一个技巧,它在初始化阶段和运行时选择所有合适的方法来处理消息它执行以下功能:

There is a trick behind .handle() that it selects all the appropriate methods for message handling during init phase and then at runtime it performs the function:

HandlerMethod candidate = this.findHandlerMethodForParameters(parameters);

因此,为了能够根据请求消息中的 payload 选择这个或那个方法,您应该说 .handle() 来做到这一点:

So, to be able to pick up this or that method based on the payload from the request message, you should say .handle() to do that:

return IntegrationFlows .from("myChannel") .handle(this) ...

当然,在这种情况下,最好将这些方法移到单独的服务类中,以避免从此 @Configuration 类中选择额外的方法.

Of, course in this case it would be better to move those method to the separate service class to avoid extra methods selection from this @Configuration class.

更多推荐

Spring 集成 DSL:配置仅在参数匹配时处理的处理程序

本文发布于:2023-08-01 20:10:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1271328.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   程序   Spring   DSL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!