我正在从Spring启动应用程序(在JAVA 11中)同步调用REST API以下载文件。
这是REST API的外观:
@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"
}
}
API返回两件事,或者是在生成文件时返回文件的状态(这需要5秒钟到5分钟或更长时间),或者在生成文件时返回文件位置URL本身。
我想解决的问题是,我需要每5秒调用一次此“ / download” API,并检查文件是否已准备好。
类似这样的东西:
我有办法实现这一目标吗?我尝试查看CompletableFuture
和@Scheduled
,但是我对这两个选项都很陌生,无法在我的代码中找到实现它们的任何方法。任何帮助,将不胜感激!
我正在从Spring启动应用程序(在JAVA 11中)同步调用REST API以下载文件。 REST API如下所示:@GetMapping(“ / download / {userId} / {fileName}”)public String ...
您可以使用Java Failsafe Library。请参考[Retry a method based on result (instead of exception)。我已经修改了答案中的代码以符合您的情况。