简体   繁体   English

异步RESTful Web服务是否可行?

[英]Are asynchronous RESTful web services possible?

Reading RESTful documentation, it does not seem like it is possible to implement an asynchronous instance, but someone may know better on SO. 阅读RESTful文档,似乎没有可能实现异步实例,但有人可能在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 . 我知道@Path没有定义asyncSupported ,但我正在寻找类似于@WebServlet东西。 Then, I would like to use AsyncContext instances (or anything equivalent). 然后,我想使用AsyncContext实例(或任何等效的实例)。

Is this possible? 这可能吗?

RestEasy has some support 1 for it - using a custom annotation called @Suspend . RestEasy的有它一定的支撑1 -使用一个被称为自定义注解@Suspend

See here: http://docs.jboss.org/resteasy/docs/2.2.1.GA/userguide/html/Asynchronous_HTTP_Request_Processing.html 见: 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 ( eg chats - https://github.com/Atmosphere/atmosphere ) 在泽西岛上还有一个名为Atmosphere的框架/库,但是对于你的用例来说可能有些过分,因为它的重点似乎是在长轮询的客户端/服务器web应用程序上(例如聊天 - https://github.com/Atmosphere/气氛

[1] The CDI scope for your request will be lost in in the thread that actually executes the logic. [1]您的请求的CDI范围将在实际执行逻辑的线程中丢失。 See the RESTEasy-682 issue for more information. 有关更多信息,请参阅RESTEasy-682问题。 This is a problem that hasn't been solved by any REST frameworks that I know of at this moment [March 2014] . 这是我目前所知的任何REST框架[2014年3月]尚未解决的问题。

Now you can make Asynchoronous RESTful calls using JAX-RS 2.0 API which is part of the recently released Java EE 7.0 现在,您可以使用JAX-RS 2.0 API进行Asynchoronous RESTful调用,这是最近发布的Java EE 7.0的一部分

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

It's apparently possible with CXF and Jetty Continuations but that only appears to be possible with Jetty 6; 显然可以使用CXF和Jetty Continuations,但这似乎只适用于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. 他们已经在Jetty 7中更改为Servlet 3.0规范中的内容,我不知道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. 此外,Jetty Continuations似乎是一个混乱的API,有很多手动的东西,所以我不知道转换代码是多么容易。

Still, somewhat possible it seems. 看起来有点可能。 With a following breeze and when God wills it. 伴随着微风,当上帝愿意的时候。

Restful spesification is still at early ages of its life. 宁静的时期仍处于生命的早期阶段。 But this problem should be considered as 2 part. 但这个问题应该被视为2部分。 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. 最近,一个基于Jeanfrancois Arcand的非阻塞客户端被实现并推送到存储库。 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. 采用新的servlet规范非常慢,作为开发人员,我期待JSR 339也能解决这些问题。 And this is also addressed at the JSR spec clearly with these sentences. 这些句子也清楚地解决了JSR规范。

JAX-RS 1.1 defines a synchronous request response model on the server side. JAX-RS 1.1在服务器端定义了同步请求响应模型。 This JSR will specify a simple asynchronous request processing model such that a response can be returned asynchronous to the request. 此JSR将指定一个简单的异步请求处理模型,以便可以将异步响应返回给请求。 Servlet 3.0 can be leveraged to enable such support but implementations may choose to use other container-specific APIs instead. 可以利用Servlet 3.0来启用此类支持,但实现可以选择使用其他特定于容器的API。

However there are other alternatives too. 然而,还有其他选择。 Projects such as Jetty are addressing such kind of problems elegant as in this example . 像Jetty这样的项目正在解决这个例子中的优雅问题。 I can only suggest you to consider other alternatives as the community is growing. 随着社区的发展,我只能建议你考虑其他选择。

Check out Pubsubhubbub found here for an example of a REST-based asynchronous protocol. 查看这里的 Pubsubhubbub 了解基于REST的异步协议的示例。 It is based on the Atom Syndication format and is a lot simplier than WS-* pub/sub mechanisms. 它基于Atom Syndication格式,比WS- * pub / sub机制简单得多。

You may also want to try Spring Webflux which is async and reactive at the same time. 您可能还想尝试同时具有异步和反应性的Spring Webflux However, this is not a JAX-RS implementation from Java EE. 但是,这不是Java EE的JAX-RS实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM