springspring-bootspring-integrationspring-integration-dsl

Spring Integration : do something when timeout occure


I'm waiting for a message to come, but when it doesn't I want to do some calculation.

Something like the following (what I want):

IntegrationFlows.from(messageChannel)
                .timeout(1, SECONDS)
                .handle(....) //only when timeout happens
                .get();

Is it possible to do it with spring integration ?


Solution

  • That's not how messaging works. The message is really a trigger of an action in such an application.

    You may consider to model an expected behavior via BarrierMessageHandler (the barrier() in Java DSL). You configure there a discardChannel and call its trigger() with some message. So, if original message does not arrive to the flow in time, that trigger is going to be discarded where you can already do your logic on timeout. Otherwise the flow is going to be processed with an aggregated message, which you can customize through MessageGroupProcessor. Both trigger and request message must have the same correlation key.

    See more info in docs: https://docs.spring.io/spring-integration/reference/barrier.html.

    Apparently we have missed to expose a discardChannel option into Java DSL. Will fix that shortly.

    Otherwise you can try to model a behavior via @Scheduled. Either way something has to start that timeout somehow. But what you are asking with that timeout() is not what we can do since it does not fit into messaging architecture.