Are asynchronous RESTful web services possible?
Solution 1
RestEasy has some support1 for it - using a custom annotation called @Suspend
.
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].
Solution 2
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.
Solution 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
http://www.slideshare.net/reza_rahman/jaxrs2?ref=
Solution 4
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.
Solution 5
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.
Related videos on Youtube
Comments
-
Jérôme Verstrynge almost 2 years
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 useAsyncContext
instances (or anything equivalent).Is this possible?
-
Buhake Sindi over 12 yearsWhy not use AJAX to call your RESTful method asynchronously?
-
Jérôme Verstrynge over 12 years@The Because AsyncContext was implemented to solve the excessive thread-per-request issue in Servlet. Using Ajax to call RESTful would not tackle this issue (on the server side). The idea/solution is to queue requests for processing on a limited set of threads (a bit like Nginx).
-
Jan Algermissen over 12 yearsYou might want to ask on the java.net/projects/jersey/lists/users/archive list. IIRC Jersey experimented with futures some time ago. Not sure whether that evolved.
-
BillMan over 12 yearsYou may want to look something like Apache ServiceMix for inspiration.
-
b_erb over 12 yearsAre you asking whether this is possible in your library, or how to tackle asynchrony in REST interfaces in general?
-
Jérôme Verstrynge over 12 years@PartlyCloudy If I implement my RESTful service, can I generate and use an AsyncContext for each received request or not? That's what I am wondering...
-
b_erb over 12 yearsSo you are asking about a specific Java-based implementation of a RESTful webservice, and not how to design an API and its resources for asynchronous patterns.
-
Cemo over 12 yearsSimply you can not. However the future version of the Restful Web Services for Java (JSR 339) will address these issues. I have also replied too.
-
ravindrab about 8 yearshave a look at this. spring.io/guides/gs/async-method
-
-
Jérôme Verstrynge almost 12 yearsThat's a Ruby solution, do you have the Java one?
-
Jérôme Verstrynge almost 12 yearsWhen I refer to asynchronous, I mean asynchronous request processing on server side...