gpt4 book ai didi

java - 等待 RESTful Web 服务中的数据下载

转载 作者:行者123 更新时间:2023-12-01 15:52:39 24 4
gpt4 key购买 nike

我正在发送一个请求以在服务器端 RESTful Web 服务上获取数据,现在在我的网络服务中我编写了一段代码来从外部服务获取历史数据,当从外部服务获取所有历史数据时,会调用通知函数,指示数据下载完成。

问题是当我向我的网络服务发送请求时,我的网络服务调用该函数从外部服务获取数据,并且不等待通知并返回。为了让它等待,我需要延迟。

那么这是正确的方法吗?还是有其他替代方案,因为我无法预测网络服务下载数据需要多长时间。

最佳答案

您必须创建一个标识长操作的“事务资源”。这是一个很好的解释:

One of the common criticism of REST is because it is so tied in to HTTP (which doesn't support a client callback mechanism), doing asynchronous service or notification on REST is hard. So how do we implement long running transactions (which typically require asynchronicity and callback support) in REST ?

The basic idea is to immediately create a "Transaction Resource" to return back to the client. While the actual processing happens asynchronously in the background, the client at any time, can poll the "Transaction Resource" for the latest processing status. Lets look at an example to request for printing a book, which may take a long time to complete

(example ...)

Note that a response is created immediately which contains the URI of a transaction resource, even before the print job is started. Client can poll the transaction resource to obtain the latest status of the print job.

(从“Common REST Design Pattern”获得)。

关于java - 等待 RESTful Web 服务中的数据下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5699896/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com