Java 如何处理 REST 中的异步操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16214244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:49:21  来源:igfitidea点击:

How to handle asynchronous operations in REST

javahttprestasynchronous

提问by harsh

I need to understand what approaches there are to handle asynchronous operations in REST and what their advantages and disadvantages are. Some approaches I found:

我需要了解在 REST 中有哪些处理异步操作的方法以及它们的优缺点。我发现的一些方法:

  1. Resource Based:Where the status of an operation is modeled as a status. User makes an async REST call (PUT, POST etc.) gets Acceptedor In-Progressresponse (202). Further a status URI is polled repeatedly via GET to check status/progress/messages from operation execution.

    Question:How long should this resource be active at Server? If the client polls in large intervals where in between the operation completes, how do we return the status? Seems like persisting the execution status would work. But how long to persist, when to archive/delete, is this kind of standard approach?

  2. Callback Based:Where an async request is required to have a callback URI. The request gets processed asynchronously and upon completion makes a call to the callback URI with the operation status/result.

    Question:This seems more elegant and involving less overhead at the server side. But how to handle scenarios where the callback server is intermittently down, not responding, etc.? Implement a typical retries where the callback URI provides retries configuration as well? Is there any other downside to this approach?

  3. Servlet 3.0 Asynchronous support:Where an HTTP client makes a connection to a Java Servlet, which remains open until it is explicitly closed and until closed client and server can communicate asynchronously over it.

    Question:Since its Servlet 3.0 spec, I think Jersey, the Spring REST implementation, doesn't utilize this approach as of now. Is there any specific REST implementation which utilizes a similar approach or pointer on ways to make it possible?

  4. Any other approaches, maybe commercial ones?

  1. 基于资源:将操作的状态建模为状态。用户进行异步 REST 调用(PUT、POST 等)获取AcceptedIn-Progress响应 ( 202)。此外,通过 GET 重复轮询状态 URI 以检查来自操作执行的状态/进度/消息。

    问题:这个资源应该在服务器上活跃多久?如果客户端以较大的间隔轮询操作完成之间的位置,我们如何返回状态?似乎坚持执行状态会起作用。但是坚持多久,什么时候存档/删除,是这种标准的做法吗?

  2. 基于回调:异步请求需要具有回调 URI。请求被异步处理,并在完成后调用带有操作状态/结果的回调 URI。

    问题:这看起来更优雅,并且在服务器端涉及更少的开销。但是如何处理回调服务器间歇性宕机、无响应等场景呢?实现一个典型的重试,其中回调 URI 也提供重试配置?这种方法还有其他缺点吗?

  3. Servlet 3.0 异步支持:HTTP 客户端与 Java Servlet 建立连接,Java Servlet 保持打开状态,直到它被明确关闭,并且直到关闭的客户端和服务器可以通过它异步通信。

    问题:自从它的 Servlet 3.0 规范以来,我认为 Jersey,Spring REST 实现,目前还没有使用这种方法。是否有任何特定的 REST 实现使用类似的方法或指示使其成为可能的方法?

  4. 任何其他方法,也许是商业方法?

回答by user2256686

I think, the approach depends on time gap between initial request and the end of operation.

我认为,该方法取决于初始请求和操作结束之间的时间间隔。

  • For short-time operations ( < 10s ) I would just keep the request open and return response when operation finished;
  • For long operations ( < 30m ) I would use servlet 3.0 or Comet model;
  • For extremely long operations ( hours, days ) good enough way, as for me, is just client-based polling or Comet with big timeouts.
  • 对于短时间操作(<10s),我只会保持请求打开并在操作完成时返回响应;
  • 对于长时间操作(< 30m),我会使用 servlet 3.0 或 Comet 模型;
  • 对于极长的操作(小时、天),对我来说,足够好的方式只是基于客户端的轮询或具有大超时的 Comet。

回答by javivelasco

I'm dealing now with the same situation and found the common approach of using Locationheader response to give a resource that can be monitored to check status (by polling of course). That seems to be the best, but in my case, I'm not creating a resource so I don't have a location to check the status (my async process is just to build a cache page).

我现在正在处理相同的情况,并找到了使用Location标头响应来提供可以监视以检查状态的资源的常用方法(当然是通过轮询)。这似乎是最好的,但在我的情况下,我没有创建资源,所以我没有位置来检查状态(我的异步过程只是为了构建一个缓存页面)。

You can always use your own headers to give an estimated time to complete the operation. Anyway I'm thinking of using Retry-Afterheader to give an estimated time. What do you guys think?

您始终可以使用自己的标题来给出完成操作的估计时间。无论如何,我正在考虑使用Retry-After标题来给出估计的时间。你们有什么感想?

回答by matsev

Spring 3.2+ supports the async features of Servlet 3.0. From the Spring Blog:

Spring 3.2+ 支持 Servlet 3.0 的异步特性。来自春季博客

you can make any existing controller method asynchronous by changing it to return a Callable. For example a controller method that returns a view name, can return Callable instead. An @ResponseBody that returns an object called Person can return Callable instead. And the same is true for any other controller return value type.

您可以通过将任何现有控制器方法更改为返回 Callable 来使其异步。例如,返回视图名称的控制器方法可以改为返回 Callable。返回名为 Person 的对象的 @ResponseBody 可以改为返回 Callable 。对于任何其他控制器返回值类型也是如此。

Jersey 2+ also supports asyncronous servers. See the Asynchronous Services and Clientschapter in the reference docs.

Jersey 2+ 还支持异步服务器。请参阅参考文档中的异步服务和客户端章节。

回答by robert_difalco

I know this is old but I thought I'd chime in here to say that if what you want is something that can scale out in a stateless environment then you should go with your first option. You can perform the underlying operation anywhere and if you put the result in something like redis it will not matter to what web server the client makes subsequent polling requests. I'll usually put the polling interval in the response I sent to the client. When there a result is ready I will return the client a SEE OTHER that includes the id of the result in the URI.

我知道这已经过时了,但我想我会在这里说,如果您想要的是可以在无状态环境中扩展的东西,那么您应该选择第一个选项。你可以在任何地方执行底层操作,如果你把结果放在 redis 之类的东西中,那么客户端发出后续轮询请求的 Web 服务器将无关紧要。我通常会将轮询间隔放在我发送给客户端的响应中。当结果准备好时,我将向客户端返回一个 SEE OTHER,其中在 URI 中包含结果的 ID。