3

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){
...
}
hummingV
  • 1,014
  • 1
  • 11
  • 25

1 Answers1

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.

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • That looks amazing but I feel auto-picking methods like that is prone to errors. e.g: in my example, if a message of type MyObject comes in, I would want both handleMessage and handleMessage2 to run, in that order. I believe your technique will run only handleMessage, not handleMessage2. – hummingV Sep 21 '17 at 20:46
  • Well, "both methods" is definitely different story. That is out of Spring Integration scope. Although we can provide you a solution: `publish-subscribe` or `recipient-list-router`. But again: that deserves it own SO question. – Artem Bilan Sep 21 '17 at 20:49
  • Why do you think it is out of Spring integration scope? The actual use case for me is to handle FileSplitter SOF,EOF markers. Regular message handlers should not care about them but aggregators and other file handlers do. So, I want these SOF,EOF markers to just pass through the flow without having all of my handler methods along the flow to check types. – hummingV Sep 21 '17 at 21:01
  • Please, form a new SO question with more info to investigate. Don't see how it relates to the current one. – Artem Bilan Sep 21 '17 at 21:03
  • I totally think they are related but fine, I will post another more specific question. – hummingV Sep 21 '17 at 21:06
  • Well, one trick might be like to have one more method there which is like `Object handleOthers(Object payload) { return payload; }` . So, those `SOF,EOF` will be handled here. But what you want isn't available out-of-the-box. You only may form your own component on the matter. – Artem Bilan Sep 21 '17 at 21:10
  • Thanks, here is a new question anyway if you are still interested. https://stackoverflow.com/questions/46353317/spring-integration-dsl-dealing-with-filesplitter-start-end-marker-payloads – hummingV Sep 21 '17 at 21:18
  • Good, will take a look lately. Now I wonder if we are done here and my answer is enough to be accept. – Artem Bilan Sep 21 '17 at 21:24