简体   繁体   中英

Discarding a Message within a Spring Integration DSL Service Activator

Is there a way that we can discard a message from within a Spring Integration Service Activator that is defined using the Spring Integration DSL .handle() method?

More specifically, assuming the below IntegrationFlow definition...

public IntegrationFlow sampleFlow() {
    return IntegrationFlows
             .from("someChannel")
             .handle(someServiceClass::someMethod)
             .get();
}

Is there a way to have a message discarded based on some evaluation within the someMethod definition? Ideally, I would like to perform some logging and then discard.

I have found is that returning null from someMethod seems to effectively end the flow but I am not sure if this is the most graceful/correct solution. If this is the recommended solution, that is fine.

That's correct. It is really non-formal pattern to stop processing returning a null from the service method. We even have documented it in non-official manner: https://docs.spring.io/spring-integration/docs/5.2.3.RELEASE/reference/html/messaging-endpoints.html#service-activator-namespace

The service activator is one of those components that is not required to produce a reply message. If your method returns null or has a void return type, the service activator exits after the method invocation, without any signals.

You also can achieve a discard-and-log goal with a filter() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM