innealtóir
innealtóir

Reputation: 11

Spring Integration - Stop specific messages sending until other messages have been successfully sent

I'm quite new to Spring Integration and am having some difficulties with the following issue: I have several files stored in my DB. I have functionality that allows users to specify one or more of these files to export to a particular SFTP location. The functionality takes each specified file, say TEST1 and TEST2, and sends two Messages for each file:

Similarly for TEST2.

The problem is that TEST1_EMPTY arrives at the destination before TEST1_CONTENT (presumably because it is a lighter file). I need to ensure that the CONTENT file arrives before the EMPTY file. If, say, there are 100 files being processed from the DB (TEST1, TEST2, ..., TEST100), it is ok for TEST100_EMPTY to arrive before TEST53_CONTENT, so long as TEST53_EMPTY arrives after TEST53_CONTENT (and of course TEST100_CONTENT arrives before TEST100_EMPTY).

My code: I have a @MessagingGateway interface MyGateway with one method - send().

I have a configuration class MyChannelConfig with annotations:

It has one method - flow() that returns an IntegrationFlow:

return IntegrationFlows
    .from("test")
    .enrichHeaders(blah)
    .transform(myTransformer) //this converts an object that represents my file to a Resource
    .handle(myHandler) //this is where the TEST1_CONTENT and TEST1_EMPTY get created and returned as a Collection of Messages
    .route(myRouter) //contains a method with the @Router annotation
    .get();

More about MyHandler:

It contains a @ServiceActivator method that calls a method processResource in class ResourceProcessor. processResource returns the Collection (a List) of Messages.

Is there any way to ensure that the TESTX_EMPTY file isn't sent until the TESTX_CONTENT file arrives at its SFTP destination?

Upvotes: 1

Views: 58

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121552

It sounds like a correlation. The TESTX_CONTENT and TESTX_EMPTY belongs to the same message group. So, you need to use an aggregator and you won't let the group to be released until both those files are landed to the group. Of course, you might need a recipient list router (or PublishSubscribeChannel) to send the same message for processing and then to the aggregator. The TESTX_EMPTY may go to the aggregator immediately, but TESTX_CONTENT later on when its process is finished. By default both those distributors are sync, so the second send does not happen until the first is done.

See more in docs:

https://docs.spring.io/spring-integration/docs/current/reference/html/message-routing.html#aggregator

https://docs.spring.io/spring-integration/docs/current/reference/html/message-routing.html#router-implementations-recipientlistrouter

https://docs.spring.io/spring-integration/docs/current/reference/html/core.html#channel-implementations-publishsubscribechannel

Upvotes: 1

Related Questions