Are asynchronous RESTful web services possible?

27,657

Solution 1

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].

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.

Share:
27,657

Related videos on Youtube

Jérôme Verstrynge
Author by

Jérôme Verstrynge

You can contact me via my LinkedIn profile.

Updated on July 09, 2022

Comments

  • Jérôme Verstrynge
    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 use AsyncContext instances (or anything equivalent).

    Is this possible?

    • Buhake Sindi
      Buhake Sindi over 12 years
      Why not use AJAX to call your RESTful method asynchronously?
    • Jérôme Verstrynge
      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
      Jan Algermissen over 12 years
      You 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
      BillMan over 12 years
      You may want to look something like Apache ServiceMix for inspiration.
    • b_erb
      b_erb over 12 years
      Are you asking whether this is possible in your library, or how to tackle asynchrony in REST interfaces in general?
    • Jérôme Verstrynge
      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
      b_erb over 12 years
      So 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
      Cemo over 12 years
      Simply 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
      ravindrab about 8 years
      have a look at this. spring.io/guides/gs/async-method
  • Jérôme Verstrynge
    Jérôme Verstrynge almost 12 years
    That's a Ruby solution, do you have the Java one?
  • Jérôme Verstrynge
    Jérôme Verstrynge almost 12 years
    When I refer to asynchronous, I mean asynchronous request processing on server side...