Spring integration DSL: configure handler that han

2019-07-14 08:55发布

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条回答
一夜七次
2楼-- · 2019-07-14 09:22

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.

查看更多
登录 后发表回答