异步 RESTful Web 服务是否可行?

发布于 2024-12-07 03:37:35 字数 427 浏览 1 评论 0原文

阅读 RESTful 文档,似乎不可能实现异步实例,但有人可能更了解SO。

我在这里的意思是我想异步执行服务请求:

@Path("/helloworld", asyncSupported=true)
public class MyHelloWorldService {
    ...
}

我知道 asyncSupported 没有在 @Path 中定义,但我正在寻找类似于 @ 的东西WebServlet。然后,我想使用 AsyncContext 实例(或任何等效的东西)。

这可能吗?

Reading RESTful documentation, it does not seem like it is possible to implement an asynchronous instance, but someone may know better on SO.

What I mean here is I would like to execute service requests asynchronously:

@Path("/helloworld", asyncSupported=true)
public class MyHelloWorldService {
    ...
}

I know asyncSupported is not defined in @Path, but I am looking for something similar to @WebServlet. Then, I would like to use AsyncContext instances (or anything equivalent).

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

ヅ她的身影、若隐若现 2024-12-14 03:37:35

RestEasy 有一些支持1 - 使用名为 @Suspend 的自定义注释。

参见这里:
http://docs.jboss.org/resteasy/docs/2.2。 1.GA/userguide/html/Asynchronous_HTTP_Request_Processing.html

Jersey 之上还有一个名为 Atmosphere 的框架/库,但这对于您的用例来说可能有点过分了,因为它重点似乎是长轮询客户端/服务器 Web 应用程序(例如聊天 - https://github.com/Atmosphere/atmosphere )

[1] 您的请求的 CDI 范围将在实际执行逻辑的线程中丢失。请参阅 RESTEasy-682 问题了解更多信息。据我所知,目前任何 REST 框架都还没有解决这个问题[2014 年 3 月]

RestEasy has some support1 for it - using a custom annotation called @Suspend.

See here:
http://docs.jboss.org/resteasy/docs/2.2.1.GA/userguide/html/Asynchronous_HTTP_Request_Processing.html

There is also a framework/library on top of Jersey called Atmosphere however that might be overkill for your usecase as its focus appears to be on long-polling client/server web applications ( e.g. chats - https://github.com/Atmosphere/atmosphere )

[1] The CDI scope for your request will be lost in in the thread that actually executes the logic. See the RESTEasy-682 issue for more information. This is a problem that hasn't been solved by any REST frameworks that I know of at this moment[March 2014].

深海不蓝 2024-12-14 03:37:35

显然,CXF 和 Jetty Continuations 是可能的,但这只出现Jetty 6 成为可能;它们在 Jetty 7 中已更改为 Servlet 3.0 规范中的内容,我不知道 CXF 是否支持。此外,Jetty Continuations 似乎有点一个混乱的 API,有很多手动的东西所以我不知道转换代码是多么容易。

不过,似乎还是有可能的。当上帝愿意时,伴随着一阵微风。

It's apparently possible with CXF and Jetty Continuations but that only appears to be possible with Jetty 6; they've been changed in Jetty 7 to something that's in the Servlet 3.0 spec and I don't know if that's supported by CXF. Moreover, Jetty Continuations seem to be a bit of a messy API, with a lot of manual stuff so I don't know how easy it is to convert the code.

Still, somewhat possible it seems. With a following breeze and when God wills it.

半葬歌 2024-12-14 03:37:35

宁静的专业化仍处于其生命的早期阶段。但这个问题应该被视为两部分。客户端和服务器。

客户端:

对于客户端来说,去年的变化已经足够成熟了。最近,基于 Jeanfrancois Arcand 的非阻塞客户端已实现并推送到存储库。 此处有一个解释。

服务器:

对于服务器端来说,还不太成熟。新 servlet 规范的采用相当缓慢,作为开发人员,我期待 JSR 339 也解决这些问题。 JSR 规范中也用这些句子清楚地解决了这个问题。

JAX-RS 1.1 定义了服务器上的同步请求响应模型
边。这个JSR将指定一个简单的异步请求处理
模型使得响应可以异步返回到
要求。可以利用 Servlet 3.0 来实现此类支持,但是
实现可以选择使用其他特定于容器的 API
相反。

然而,还有其他选择。 Jetty 等项目正在优雅地解决此类问题,如这个示例 。随着社区的发展,我只能建议您考虑其他选择。

Restful spesification is still at early ages of its life. But this problem should be considered as 2 part. Client and Server.

Client:

For the client side recent changes at last year became mature enough. And recently a non blocking client from based on Jeanfrancois Arcand was implemented and pushed to repository. There is an explanation here.

Server:

For the server side, it is still immature. The adoption of the new servlet specification is quite slow and as a developer I am expecting JSR 339 to address these issues as well. And this is also addressed at the JSR spec clearly with these sentences.

JAX-RS 1.1 defines a synchronous request response model on the server
side. This JSR will specify a simple asynchronous request processing
model such that a response can be returned asynchronous to the
request. Servlet 3.0 can be leveraged to enable such support but
implementations may choose to use other container-specific APIs
instead.

However there are other alternatives too. Projects such as Jetty are addressing such kind of problems elegant as in this example. I can only suggest you to consider other alternatives as the community is growing.

吃不饱 2024-12-14 03:37:35

现在,您可以使用 JAX-RS 2.0 API 进行异步 RESTful 调用,该 API 是最近发布的 Java EE 7.0 的一部分

http://www.slideshare.net/reza_rahman/jaxrs2?ref=

Now you can make Asynchoronous RESTful calls using JAX-RS 2.0 API which is part of the recently released Java EE 7.0

http://www.slideshare.net/reza_rahman/jaxrs2?ref=

苏大泽ㄣ 2024-12-14 03:37:35

查看此处的 Pubsubhubbub,了解基于 REST 的异步协议的示例。它基于 Atom Syndicate 格式,比 WS-* 发布/订阅机制简单得多。

Check out Pubsubhubbub found here for an example of a REST-based asynchronous protocol. It is based on the Atom Syndication format and is a lot simplier than WS-* pub/sub mechanisms.

不知在何时 2024-12-14 03:37:35

您可能还想尝试 Spring Webflux 同时是异步和反应式的。但是,这不是 Java EE 的 JAX-RS 实现。

You may also want to try Spring Webflux which is async and reactive at the same time. However, this is not a JAX-RS implementation from Java EE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文