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