使用 Jersey 的 Java Async REST Web 服务?

2022-01-21 00:00:00 multithreading rest java jersey

我需要实现一个 Java REST Web 服务(我们使用 Jersey 框架),基本上可以

I need to implement a Java REST Web Service (we use Jersey framework) which can basically either

一个.在返回响应之前阻塞等待某个事件(或轮询事件)湾.提供某种 aysnc 行为以在请求已处理时通知客户端.

a. block waiting for some event (or poll for the event), before returning the response b. provide some kind of aysnc behaviour to notify the client when the request has been processed.

我正在考虑返回一个 transationID,并有一个/status 端点,客户端应该轮询该端点以确定请求是否已处理并获得特定结果.

I was thinking of returning a transationID, and having a /status endpoint which the client should poll to determine if the request was processed and get a specific result.

有什么想法吗?

推荐答案

Alan,正确的做法是使用 HTTP 的 202 Accepted 返回码,并在响应正文中包含一个链接,该链接指向客户端可以轮询以检查请求状态的页面.

Alan, the right thing to do is to use HTTP's 202 Accepted return code and include in the response body a link that points to a page the client can poll to check the status of the request.

在 machine2machine 上下文中,您需要为这些响应设计媒体类型(或微格式等),以便机器客户端可以理解"它们.

In a machine2machine context you will need to design your media type (or microformat etc.) for those responses so a machine client can 'understand' them.

参见 http://www.w3.org/协议/rfc2616/rfc2616-sec10.html#sec10.2.3

也许还为此目的查看几乎标准化的新 HTTP Link 标头.

Maybe also look at the almost standardized new HTTP Link header for that purpose.

https://datatracker.ietf.org/doc/html/draft-nottingham-http-link-header-03

一月

相关文章