请继续调用rest API,直到服务器完成任务为止

问题描述 投票:0回答:1

我正在从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,并检查文件是否已准备好。

类似这样的东西:

  • 首先调用“ / download”-> API返回“文件生成中”状态
  • 因此,请在5秒钟后再次调用-> API再次返回“文件生成中”状态]]
  • 因此,请在5秒钟后再次调用它-> API再次返回“文件生成进行中”状态(但此时假定已生成文件)
  • 因此,请在5秒后再次调用它-> API返回文件位置URL
  • 停止调用API并执行以下操作
  • 我有办法实现这一目标吗?我尝试查看CompletableFuture@Scheduled,但是我对这两个选项都很陌生,无法在我的代码中找到实现它们的任何方法。任何帮助,将不胜感激!

我正在从Spring启动应用程序(在JAVA 11中)同步调用REST API以下载文件。 REST API如下所示:@GetMapping(“ / download / {userId} / {fileName}”)public String ...

java spring-boot rest restful-url
1个回答
0
投票

您可以使用Java Failsafe Library。请参考[Retry a method based on result (instead of exception)。我已经修改了答案中的代码以符合您的情况。

© www.soinside.com 2019 - 2024. All rights reserved.