javaspring-bootspring-integrationspring-integration-dsl

Stopping message flow in the middle of a Spring integration flow


I have a spring integration flow which listens to JMS messages using the Java DSL through the use of a JMS messageDrivenChannelAdapter. It has a subflow as follows:

f -> f.handle(ServiceA)
      .handle(ServiceB);

In Service A, I am writing a message to a DB. If I receive a transient error, I simply throw this error which causes the message to get redelivered as expected.

However if I receive a non-transient exception in ServiceA, I want to stop processing the message in this flow (I do not want it to be sent further down the integration flow to ServiceB, or other downstream endpoints) and I want to acknowledge the jms message so that it is removed from the queue. How can I do this?


Solution

  • There is some convention in messaging if result of the service invocation is null, then reply message is not created and nothing is emitted to the output channel of the endpoint.

    The handle() operator in Spring Integration Java DSL is for Service Activator EIP. We mention that null behavior briefly in the docs: https://docs.spring.io/spring-integration/reference/service-activator.html

    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.

    I think this is something you can do in your ServiceA: just catch that "non-transient exception" and return null from your service method.