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?

Upvotes: 16

Views: 27902

Answers (6)

Piotr Gwiazda
Piotr Gwiazda

Reputation: 12212

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.

Upvotes: 0

Eike D
Eike D

Reputation: 315

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

Upvotes: 8

V.Vidyasagar
V.Vidyasagar

Reputation: 713

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=

Upvotes: 2

Shawn H
Shawn H

Reputation: 1247

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.

Upvotes: 1

Cemo
Cemo

Reputation: 5570

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.

Upvotes: 2

Donal Fellows
Donal Fellows

Reputation: 137587

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.

Upvotes: 2

Related Questions