Spring integration DSL: configure handler that handles only when the argument matches

牧云@^-^@ 提交于 2019-12-10 23:26:15

问题


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?

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){
...
}

回答1:


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);

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)
            ...

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.



来源:https://stackoverflow.com/questions/46350357/spring-integration-dsl-configure-handler-that-handles-only-when-the-argument-ma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!