Spring 集成 DSL:配置仅在参数匹配时处理的处理程序 [英] Spring integration DSL: configure handler that handles only when the argument matches

查看:24
本文介绍了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:配置仅在参数匹配时处理的处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆