DeejUK
DeejUK

Reputation: 13501

Stopping Spring Integration app after processing all messages from FTP list

What would be the best way of stopping a Spring Integration application after it has processed all messages that resulted from a series of FTP Outbound Gateway ls commands?

As a one-off job I need to traverse an FTP directory structure, recursing for each directory found, and copy down each file and then send it on as message for processing. Once the FTP directory tree has been fully traversed, and all the messages that resulted from the traversal have been processed, I'd like to stop the application gracefully.

My current thoughts are that I could try and keep a track of the activity of the gateway and all queues, and when they've all had a size of 0 for a while, terminate the application. This could be made more sophisticated by doing something like keeping a count of all files for each directory (this'd require extending AbstractRemoteFileOutboundGateway), making sure the directory name stays on all message headers through the flow, and then having a downstream component that keeps a count of messages processed for each directory, and initiates shutdown once they've all been accounted for.

That sounds like a fair amount of work - does anyone know of a simpler way? I've got a choice between writing the app without Spring Integration and doing a load of tedious stuff by hand (iterating of FTP trees, copying files about), or using Spring Integration but instead having to extend bits of it to match my use case.

Upvotes: 0

Views: 723

Answers (1)

Biju Kunjummen
Biju Kunjummen

Reputation: 49935

Slightly round-about, but this is an approach that I can think of:

.1. Since you have to put messages to trigger the ftp outbound gateway, after you have triggered all messages put a dummy message with STOP say as payload. Just before the outbound ftp gateway put a router in place that will selectively send messages to the ftp gateway or to the step post FTP gateway - send it through a channel that accepts only one message at a time so that the messages are serialized.

.2. Once in your file processing channel when you receive the STOP message, send this message to a specific queue channel, say stop queue channel.

.3. In your main program get a reference to this stop queue channel and once you start the SI flow, just wait on message to come to the queue channel, once the STOP message comes to this queue channel your main method will continue and the flow should stop at this point.

Upvotes: 1

Related Questions