Spring Integration – how do you poll an external service until condition is true?

I have the following scenario:

I send data to a 3rd party (a SOAP service as it happens). The external service accepts my data, and gives me a unique reference which I then can use to call a second SOAP service to see if my data has been processed.

So I need to poll the second service until I get a response back from it saying my data is ready. Once it’s ready my flow can end (I just want to write the fact that it’s ready to a database table). If I poll a certain number of times and it’s not ready I want to give up and call some sort of exception processing.

I can’t work out what I should use for the polling; i.e. the logic for checking the result of my soap call and polling again after a set amount of time. The time I have to wait could be dynamic – for example the 3rd party service that is called might say "it’s not ready yet, try again in 5 minutes" so that I have to poll again in 5 minutes.

Does anyone have an idea of how I would do the above in Spring integration, using the Java (or preferably) Kotlin DSL ?

I’m just trying to work out if Spring Integration is the right tool for the job here.

>Solution :

The poll in your case I would is more like "repeat until with delay in between".

So, after calling the first service you go to the channel for a second service.
Then you use a route() to determine if it is OK, or has to be repeated. At this point you send to delay(), when its duration indeed can be determined from a SOAP reply. At this point I also would increase some attempts headers to check with another route() to see if we are OK to repeat or it is already time to throw an exception.

Just don’t forget that you can have a channel() in the middle of the flow, so some other endpoint (like that router or delayer) can send messages directly to this channel.

Another solution could be done with plain Java which would do all these "repeat until" logic around calling a @MessagingGateway which would send a message to that second SOAP service and wait for its reply.

See more in docs about all the mentioned components:





Leave a Reply