I am calling a REST API synchronously from a Spring boot app (in JAVA 11) to download a file.
This is how the REST API looks like:
@GetMapping("/download/{userId}/{fileName}")
public String download(final int userId, final String fileName) {
if(requested file is available) {
return {file location URL}
} else {
Begin generating a new file (takes 5 seconds to more than 5 minutes)
return "file generation in progress"
}
}
The API returns two things, either it returns the status of the file if it is generating the file (which takes anywhere from 5 seconds to 5 minutes or more), or the file location URL itself if the file is generated.
The problem I am trying to solve is, I need to call this "/download" API every 5 seconds and check if the file is ready or not.
Something like this:
- First call the "/download" -> API returns "file generation in progress" status
- so, call it again after 5 seconds -> API returns the "file generation in progress" status again
- so, call it again after 5 seconds -> API returns the "file generation in progress" status again (But by this time assume that the file is generated)
- so, call it another time after 5 seconds -> API returns the file location URL
- Stop calling the API and do the next things
Is there a way I can achieve this? I tried looking CompletableFuture
& @Scheduled
but I am so new to both of these options and couldn't find any way to implement them in my code. Any help would be appreciated!