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.
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?
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].
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.
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.
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=
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.
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.
精彩评论