Search code examples
javaspringapitaskpolling

Polling application untill condintions are met in SpringBoot


I have to build a service using Java and SpringBoot which will be responsible for polling an application via API till the conditions are met. So I'm sending POST request and the app has to do something. Then I'm sending GET request towards this app and fetching the data. I'm checking if the task is done. If not, I need to send this GET request one more time eg. every 10 seconds. If analysis is done, then I should stop polling. I've tried to use schedulers but there is a problem with checking conditions. Runnable object cannot return any value so I can't cancel the task in a proper moment. The second thing is that I can have more than one task at the same time to polling. Do you have any idea how I can resolve my problem?


Solution

  • I'm thinking at two options:

    1. Each time when sending a POST save details of the request in a table. Using a @Scheduled annotation, once at x secons, you can query the table and call GET api for all requests which are not processed yet. Once a request is processed you can update the status in the table so it won't be picked up next time.
    2. Each time after sending a POST request, using ThreadPoolExecutor add a thread which will poll GET api. Edit: having the full picture now, i think you should persist the request details for both options, because if something will go wrong you need to have the details saved somewhere for a future retry.