问题
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