JAX-RS + Asynch HTTP


A RESTEasy user (and JBoss customer) asked about 6 weeks ago if we were planning on supporting Resteasy with Asynchronous HTTP.  For those of you not familiar with Asynchronous HTTP, take a look at Servlet 3.0, Tomcat Comet APIs, or Jetty’s continuations.  I decided to add some limited support after doing some research.

The primary (and IMO, the only) usecase for Asynchronous HTTP is in the case where the client is polling the server for a delayed response.  The usual example is an AJAX chat client where you want to push/pull from both the client and the server.  These scenarios have the client blocking a long time on the server’s socket waiting for a new message.  What happens in synchronous HTTP is that you end up having a thread consumed per client connection.  This eats up memory and valuable thread resources.  No such a big deal in 90% of applications, but when you start getting a lot of concurrent clients that are blocking like this, there’s a lot of wasted resources.

As for Resteasy and asynchronous HTTP, I don’t want to create a COMET server.  Maybe I’m totally wrong, but it seems that the idea of COMET is to use HTTP solely for the purpose of initiating a dedicated socket connection to your server.  After the initial connection is made, the client and server just tunnel their own message protocol across the socket for any number of requests.

Now, this isn’t necessarily a bad idea.  Its actually a good one for performance reasons.  As a JBossian David Lloyd explained to me in private email, what this API allows you to do is create a direct connection to your service so that you don’t have to redispatch with every message that is exchange from the client and server.  Sure, HTTP Keep Alive allows you to avoid re-establishing the socket connection, but you don’t have to worry about all the other path routing logic to get the message to the Java object that services the request.

Still, this is something I don’t want or need to provide through RESTEasy.  Its really using HTTP to tunnel a new protocol and something that is very unRESTful to me.  Basically it requires that the client knows how to communicate the COMET protocol, which IMO, makes ubiquity very hard.  Besidesk, Tomcat, JBoss Web, and Jetty will probably do a fine job here.  There are also already a number of COMET servers available on the web.  No, I will focus on giving asynchronous HTTP features to a pure HTTP and JAX-RS request.

What I will initially provide through RESTEasy is a very simple callback API.

public void getMyResource(@Suspend AsynchronousResponse response) {

   ... hand of response to another thread ...

public interface AsynchronousResponse {

   void setResponse(javax.ws.rs.core.Response response);


The @Suspend annotation tells Resteasy that the HTTP request/response should be detached from the currently executing thread and that the current thread should not try to automaticaly process the response.  The AsynchronousResponse is the callback object.  It is injected into the method by Resteasy.  Application code hands off the AsynchronousResponse to a different thread for processing.  The act of calling setResponse() will cause a response to be sent back to the client and will also terminate the HTTP request.

Servlet 3.0 has asynchronou APIs.  When you suspend a request, the request may be redelivered when a timeout occurs.  I don’t want to have a Resteasy replacement for these APIs.  What I want is something that complements these APIs and makes a specific usecase easier to write.  The use case I want to solve is detaching the processing of an HTTP response from the current thread of execution so that another thread can do the work when ready.  That’s it.  So, there will be no redelivery of a request.

Initially I plan to work with the asynchronous apis within the Tomcat 6.0 NIO transport as this is what is distributed with JBoss 4.2.3.  Next I plan to work with the JBoss Web asynchronous apis, then Jetty 6, then finally with a Servlet 3.0 implementation.

If you have any ideas on this subject, please let me know.  I can work them into the prototype I’m developing.

Atom too SOAPy for me


I’ve heard a lot of talk lately about using the Atom protocol for something other than a better RSS feed, like using it as a messaging protocol.  For instance, I was reading this excellent article on using REST to design a workflow on Infoq.com where they use the Atom format to describe and interact with a work queue.  The example was that a order fulfilment worker would the server and would get back an Atom feed of what jobs needed to be fulfilled.  Other, like James Strachan, had talked about using atom as well.

For me, the value of Atom haven’t really clicked with me yet.  Its just too SOAPy for me.  If you look at ATOM, the ATOM protocol, and how people are talking about using it, they’re really using it as an envelope.  One of the things that attracted me to REST was that I could focus on the problem at hand and ignore bulky middleware protocols (like SOAP) and lean on HTTP as a rich application protocol.

For example, if you want to get a list of messaging, why not just multipart/mixed?  Its a very simple format.  Its easy to parse.  You can assign specify headers to each body and use well-known headers like Content-Location if you want to replace actual content with a link. Its all much more compact.  Better yet, why not just send back a comma-delimited list of URLs.

Maybe I just haven’t seen the light yet.  It took me months to accept REST as a viable way of doing things.  Maybe I just need somebody to yell at me about ATOM.

RESTEasy DZone articles and Beta 8


If you’re wondering why I haven’t done a signficant blog since June, wonder no more.  Instead of creating content for my blog, I’ve written a couple of articles for DZone.com:

Also, today I put out RESTEasy 1.0 Beta 8.  I small tiny bug was found by the community which I rolled up quick into a release.  Follow the downloads link at http://jboss.org/resteasy to check it out.

RESTEasy 1.0-beta-7: quick bug fixes


A quick release from our beta-6 to fix a few quick bugs that users found.  See our main project page at:


to download and browse docs.  Have fun.

RESTEasy 1.0-Beta-6 Released!


A lot of changes in this release.  The specification is basically done and going final any day now.  The spec lead just needs to finish the TCK and it will go to a vote with the JCP EC and become final.  We are up to date with the specification.  We have a lot more providers in this release.  YAML, JSON, to name a few.  The distribution now contains docbook generated documentation and a bunch of example apps so you can see RESTEasy in action.  I want to especially thank:

  • Ryan McDonnough, for his hard work on writing the JAXB+XML,  JAXB+JSON, and Multipart providers.
  • Olivier Brand, for his Spring/Hibernate/RESTEasy example application
  • Martin Algesten, for all his bug fixing, patching, and the YAML provider

One final thing,  The main project page has moved off of JBoss WIKI to jboss.org:


Have fun. The next release will be focusing on innovations, specifically:  Asynchronous JAX-RS, JAX-RS Messaging, Asynchronous HTTP, and HTTP Caching.

RESTEasy MOM: An exercise in JAX-RS RESTful WS design


Edited 10/28/2010: Since I wrote this blog, we’ve released the HornetQ REST Interface.  Check it out and tell me what you think.

A few months ago after my first release of RESTEasy JAX-RS, I realized that I had made a cardinal mistake with our JAX-RS implementation. I had yet to build any real application with it. I had yet to test drive it. In fact, I had actually never written a RESTful application period. So, with that in mind, I decided to build something so I could get a feel how good JAX-RS and REST actually was. But what to build? I thought of redoing Pet Store with AJAX and RESTEasy as the backend, but then I’d actually have to learn AJAX or some AJAX framework. Then I thought, I’m a middleware developer, why not make some piece of middleware RESTful? Thus, RESTEasy MOM was born. This blog entry is about both an exercise in RESTful Web Service design as well as its Java counterpart, JAX-RS.

RESTEasy MOM is a RESTful interface over any generic JMS implementation. I’ve only tested it with JBoss MQ on JBoss AS 4.2, but I’m sure it will work just fine with other application servers and/or JMS implementations with a little tweaking. It is a little different than vanilla JMS in that it deals with sending and receiving of HTTP messages and thus is a little more HTTP aware than regular JMS.

RESTful Queues

My first prototype with RESTEasy MOM was to build a publisher and subscriber interface to a JMS queue. I thought I’d start off simple and get more complicated from there. I’ll describe the interface as both an HTTP request/URI template as well as a JAX-RS interface so you can get two perspectives, a client one, and a server-side implementation perspective.

POST /queue/{queue-name}?persistent=[true/false]
GET /queue/{queue-name}

Publishing to the queue would be a POST HTTP request with an optional persistent flag that would tell the JMS provider whether or not the publisher wants the message stored and forwarded. Queue subscribers would pull the queue via a simple GET and they would get the same content posted by the original POST request. Here’s what the server-side JAX-RS implementation would look like:

public interface QueueManager {
   public void send(@PathParam("name") String destination,
                          @QueryParam("persistent") @DefaultValue("true") boolean persistent,
                          @Context HttpHeaders headers,
                          InputStream body);

   public Response receive(@PathParam("name" String destination);

We want the persistent parameter to the send() method to default to true so we use the @DefaultValue parameter annotation. We want access to all of the HttpHeaders sent with the request because we may want to forward certain non-request headers to the receiver of the message. We represent the body of the HTTP request as an InputStream because we’re going to deal with the request generically. Basically all we’re really going to do is store the HTTP request and send it as-is as an HTTP response when a client wants to pull a message from the queue.

What’s cool about RESTful messaging here?

*sarcasm* Yay team! We’ve created a RESTful messaging interface over JMS. *end sarcasm* What’s so interesting about it? What do we get out of making something RESTful?

1. Addressability

Because we are representing our queue as a URI we have a standard way to identify where our queues live on the network. The URI specification is old, well understood, and stable. Believe it or not the JMS specification does not specify where a client is supposed to connect to a remote queue. It is up to the vendor. Many vendors use JNDI to do this, but they are not required to.

2. A uniform, constrained interface

We are constraining ourselves to interact with our queue through the methods provided by the HTTP protocol. If we want to evolve the functionality of our RESTful messaging system, we are constrained to only 4-6 HTTP methods (GET, POST, DELETE, PUT, HEAD, OPTIONS). This forces us architecturally to define new functionality as new resources (different URI paths). Why is this a good thing? Well, think of versioning over time. If new functionality is represented as different URI paths we do not have to worry about breaking existing older clients of our messaging system. Even if we add a new attribute to a specific URI request, things like Query parameters are always optional to a request and we won’t break older clients that are not aware of the interface change.

3. A stable protocol

HTTP is an old and well defined protocol. In the JMS specification there really is no remote protocol defined. It is hidden by the implementation. In the distributed world, distributed messaging specifications are either dead or dieing (DCE, CORBA) or a moving target (WS-*) or out of style (JAX-RPC vs. design by contract). HTTP on the otherhand is stable. I’ll also gander that there are a lot more people that know how to pass HTTP messages around than there are people that know CORBA or WS-*.

Points 1-3 are interesting, but honestly, not really *that* compelling. But, when you start analyzing the side-effects of points 1-3 you really start to see why a RESTful messaging system is a great idea:

4. Interoperability

JMS has no interoperable protocol defined between vendors. So there is really no way a IBM client could talk to a JBoss one. If you had an application that needed to interact remotely with both a JBoss MQ and IBM MQSeries, you’d have to install both libraries on your system. Both systems would even probably have different ways to configure, connect to, and bootstrap themselves. With HTTP it is all well understood and simple. Because JMS is not interoperable, what happens if you want to ditch your ultra-expensive IBM MQSeries implementation and slide in the ultra-reliable, ultra-cheaper, JBoss solution? You have to upgrade both your client and server. Not too bad if you have one client, but what if you have a 1000 clients? Not very fun…

With REST over HTTP all you have to worry about is that your server and receivers understand the data you are sending them, which is really no different than what you have to worry about with sending regular JMS messages.

5. Ubiquity == platform/language independence

Most (all?) credible programming languages have an HTTP client stack. Because we have created a RESTful interface to our messaging system, virtually any client from any language can use it. You just cannot say the same about JMS (Java Messaging Service), or really any WS-* or CORBA equivalent (or even AMQP). Because we are using a standard, ubiquitous, messaging protocol (HTTP), we can have a Java server in which a C++ client is posting XML or JSON messages and a Ruby client receiving and processing them. We could have a RESTFul Messaging Service written in PHP in which Java clients are exchanging Java objects (No reason the HTTP body can’t be a Java object!). IMO, this is the most compelling reason to use REST and HTTP.

6. Familiarity

Software developers are usually a curious animal. They don’t mind learning new technologies, its fun to the. I hope I don’t insult anybody too much with the next statement, but, IT operations aren’t as open to change and new things. Usually, its not because they don’t want to learn new technologies. Its just that they just have one more complication to worry about. For the app developer, putting their project into production, they only have to worry about their application, their world. For ops, they have to worry about the whole organization. So, what does this have to do with REST? Well, take security for instance. In a Java EE, WS-*, or CORBA world, the ops guy is going to have to learn how that particular platform configures security. For REST? Well, securing HTTP requests is a well known and practiced trade. All the ops professional needs is a set of URI’s and HTTP operations the application exposes. Since we’re contrained by the HTTP uniform interface, its also intuitive on what is being secured. GET is read access. PUT/POST update/insert access. DELETE delete access. Most admins know how to define authentication and authorization for URI patterns and HTTP methods. Many times it is as simple as configuring the good, old reliable Apache HTTPD.

What’s cool about JAX-RS?

The easiest way to appreciate JAX-RS is just to write the same application as a raw servlet and compare the two. I think if you do this, you’ll find that JAX-RS is simpler, more elegant, and just plain fun. It doesn’t get in your way and you can focus on writing your application rather than struggling with an API. Details?

1. Easy access to URI parameters

If we were implementing our messaging service as a servlet, we’d have to do a lot of URI parsing. @PathParam makes things so much easier

2. WYSIWYG Development

Another thing I like about JAX-RS, is that, because of the annotations, you can look at a servler class and know exactly how to invoke on it. You know what headers are being used, params, and the base URI.

3. A la carte HTTP

With servlet development, you have to extend a specific class and follow a specific contract. You get access to the entire HTTP request and response, but, you have to write a bit of gluecode to pull that information out. With JAX-RS, you can pull in exactly what you want from the HTTP request and have JAX-RS do any String->to->primitive conversions you need.

4. Pluggable message body mappings

RESTEasy MOM does not take advantage of this feature of JAX-RS because it is dealing with HTTP message generically and is really just a router of messages. It doesn’t really care about the content. But, this will not be true in other, less-generic, business oriented applications. Through the @Provider contract, developers can write plugins that know how to marshal and/or unmarshall a specific MIME type to a specific Java class. JAX-RS itself requires built-in support for JAXB. Other mappings like JSON->Java could easily be plugged in as well.

Evolving our RESTFul Messaging interface

There are few things that need to be tweeked in this simple RESTful interface.

GET is idempotent

Our interface for receiving messages has broken our constrained uniform interface. How you ask? Well, the HTTP GET operation is supposed to idempotent. What that means is no matter how many times you invoke it it is not supposed to change the state of the underlying resource. It is supposed to be repeatable with no side affects. This is not the case here as when we GET from the queue, we are consuming a message, sending it back to the receiving client, and removing the message from the queue. This is changing the state of the resource and using GET in the manner is poor RESTful design.

This may sound like nit-picking, but its not. Consider if the Unix file system allowed users to change the state of a file or device with a read access? It would break file permissions and our system wouldn’t be consistent. Later, in another blog , (this article is already too long) when I talk about caching and Cache Control headers, I’ll show you why this might be important for a RESTful messaging system. But for now, I hope you get what I’m saying.

What HTTP method should we use then?

So, we have to replace GET. Your first insite might be, what about DELETE? We are consuming/deleting a message from the queue when we send it back to the client and DELETE is allowed to send back content as a response. Unfortunately, again, DELETE is supposed to be an idempotent method. It is supposed to be repeatable. The only logic choice is POST because POST allows you to change the state of your resource. Unfortunately, we already have POST assigned to an operation so what should we do?

I don’t like overloaded POST

What some people like to do is overload a POST operation by adding an action query parameter:

POST /queues/{name}?action=[send|receive]

I don’t like this because what you’ve done is provided multiple operations for what is supposed to be a uniform operation. You’ve created a mini-rpc framework. Beyond that, it doesn’t map very well to JAX-RS. You’d have to do some mini dispatching in a specialized generic method. Might as well just write a servlet :).

Just create a new resource

Instead, a better plan is to just create a different resource, a.k.a. just extend the URI path.

POST /queues/{name}/receiving

Now that receiving is its own resource we use POST for getting and consuming the message. We could, in the future, use GET to peek at the message. In much the same way in programming we’d want to separate the sender and receiver into two different interfaces and/or objects, we can do the same with URI resources.

PUT instead of POST for sending

When we send a message via POST, POST does not guarantee any idempotency. This kinda sucks because we could possibly send a duplicate message. For example, lets say we POST a message, the server locally sends it over JMS successfully, but crashes before it can send the acknowledgement. The sending client has no idea whether the message posted to the internal JMS queue or not, so, it might resend the message. So can we solve this problem? We can make sending a message idempotent by writing to a specific URI that is dedicated to that particular message. The client would be responsible for generating a unique identifier for that and PUTing it at a specific URI:

PUT /queues/{name}/messages/{message-id}

Using PUT in this way makes sending the message idempotent. Why? Well, if, on the initial send, there is a failure, the sending client can just retransmit. When the server processes a send, it has the identifier of the message. It can look in its persistent store to see if the message already exists and has been posted, or look in memory. If a message of that id doesn’t exist, just post it, if it does exist then do nothing. Our logic doesn’t even have to be that complicated. Anytime a message comes in, just log a message of that id and overwrite any existing one. Since they should be the same data, no harm done. We would use a PUT instead of POST here because the standard HTTP 1.1 definition of PUT tells the user of our messaging system that sending a message is idempotent and they don’t have to worry about performing resends.

Unfortunately, we cannot implement this with our JAX-RS facade over JMS. While JMS does let you specify a message id, it is really undefined on what happens if you post a message of the same id. We could browse the queue to see if the message is there, but, if the message gets processed before we can’t check to see if it already exists. We would have to extend the functionality of an open source JMS implementation like JBoss Messaging.

Improvements to JAX-RS design

So, after our RESTful design improvements we have the follow JAX-RS design:

public interface QueueManager {
   public void send(@PathParam("name") String destination,
                          @QueryParam("persistent") @DefaultValue("true") boolean persistent,
                          @Context HttpHeaders headers,
                          InputStream body);

   public Response receive(@PathParam("name" String destination);

One of the problems with defining our JAX-RS interface in this way is that we now have two things coupled together when they shouldn’t be. Any implementation of our QueueManager interface would have to both locate a JMS ConnectionFactory and Destination as well as process the request and interact with a Connection and JMS Session. The problem is that, locating the ConnectionFactory and Destination is going to be implementation specific. Each JMS implementation could have their own specific way of finding these items. So, it would be better if we could separate these concerns into two separate classes. What’s cool is that JAX-RS allows us to model such a scenario with Subresources and Subresource Locators.

JAX-RS Subresources and Subresource Locators

A cool feature of JAX-RS is that you can divide the processing of an individual requests between multiple objects. A top-level resource could gather some information from part of the HTTP request and hand back to the JAX-RS runtime an object that can handle the remainder of the request. These are called Subresource Locators and Subresources. What we are going to use them for in our messaging system is to break up finding a JMS destination and processing a sending or receiving of a message into two separate classes/objects/resources.

public class RestfulJBossQueueManager {

   public RestfulQueue locate(@PathParam("name") String destination) {
       ConnectionFactory connectionFactory = (ConnectionFactory)jndi.lookup("java:/ConnectionFactory");
       Destination queue = (Destination)jndi.lookup("queue/" + destination);
       return new RestfulQueue(connectionFactory, queue);

The sole purpose of the RestfulJBossQueueManager class is to find the target destination through the destination @PathParam parameter. It must not have a HTTP method annotation like @POST and must return an object that can process the rest of the request. The locate() method does just this. It finds a ConnectionFactory and Destination and uses these variables to construct an instance of the subresource class, RestfulQueue.

public class RestfulQueue {
   public RestfulQueue(ConnectionFactory factory, Destination destination) {

   public void send(@QueryParam("persistent") @DefaultValue("true") boolean persistent,
                    @Context HttpHeaders headers,
                    InputStream body) {...}

   public Response receive(@PathParam("name") String destination) {...}

The JAX-RS runtime dynamically scans the returned RestfulQueue class for annotated methods that could be used to finish processing the HTTP request. This is a simple example of Subresources and their locators, but imagine if your Hibernate object/rational model served the dual purpose of a data and restful interface. Just some thoughts… 🙂

More to come…

I had a lot more to write but I’m already at 3000 words, a little bit long for a blog entry. I plan on writing one or two more entries diving into some more complexities and features you could add to RESTEasy MOM including content negotiation, listeners, tighter reliability, and overall better RESTFul design, applying more RESTFul techniques and architecture to a distributed messaging system. For now, you can check out the RESTEasy MOM project, download the code, read the WIKI and see what I’ve experimented with so far. If you like what you see, post us an email on resteasy-developers@lists.sourceforge.net

RESTEasy 1.0 Beta 5 Released

1 Comment

Customer bug fixes and new features in this release. We’ve implemented some JSON support after some users requested this feature. Using the Jettison Framework, you can use JAXB annotated classes and marshal them to and from JSON. A pretty cool framework and easily integrated into RESTEasy JAX-RS. The RESTEasy JAX-RS documentation has also been updated to reflect changes and functionality updates.

Please see our main project page to learn how to download, post questions, etc.

Release Notes – Resteasy – Version Beta 5


  • [RESTEASY-47] – Autoscan thinks a Subresource is a root resource, result is error
  • [RESTEASY-50] – Client framework throws exception if anything other than OK is returned as a response code
  • [RESTEASY-53] – DELETE/POST with no entity returns 200 should return 204 (NO_CONTENT)

Feature Request

  • [RESTEASY-52] – JSON/JAXB and raw JSON @Provider support
  • [RESTEASY-54] – ClientResponse object for ClientFramework
  • [RESTEASY-55] – Allow return of Response.Status in client proxy framework
  • [RESTEASY-56] – Allow turning off of default built-in @Provider classes


  • [RESTEASY-49] – Support ExceptionHandlers
  • [RESTEASY-57] – resteasy.providers should not turn off builtin

Speaking Thursday, June 12th NEJUG: REST and JAX-RS

Leave a comment

I’ll be speaking this Thursday, June 12th at the Boston area JUG (NEJUG) at the Sun campus in Burlington at 6:00 pm. My talk will be on on REST and how the new JAX-RS specification makes it easier for you to write RESTFul Web Services in Java. Please stop by, at least to say hi. Hecklers welcome too!

JAX-RS passes public review ballot unanimously

Leave a comment

I guess nobody has a problem with JAX-RS on the JCP EC.  Sweet!  🙂  It really is a good spec.  Kudos to Marc Hadley for putting together such a good spec and managing the JSR so professionally.

Resteasy JAX-RS Beta 4 Released

1 Comment

I’m trying to release much earlier and more often. This release brings RESTEasy JAX-RS up to the current public draft of JAX-RS, JSR-311, minus a few methods that are being debated upon within specification. I didn’t want to implement something where the behavior is going to change. The RESTEasy JAX-RS documentation has also been updated to reflect changes and functionality updates.

Please see our main project page to learn how to download, post questions, etc.

Release Notes – Resteasy – Version Beta4


  • [RESTEASY-28] – if a class or super class is annotated, then ignore any interface’s annotations
  • [RESTEASY-34] – Locator method and resource method with same URI does not dispatch
  • [RESTEASY-45] – You should be able to use multiple path params per uri segment
  • [RESTEASY-46] – Subresource class annotated via its interface does not work


  • [RESTEASY-3] – Implement UriInfo.getAncestorResources and UriInfo.getAncestorResourcesURIs
  • [RESTEASY-7] – Implement MessageBodyWorkers
  • [RESTEASY-21] – Support for @Path.encoded
  • [RESTEASY-22] – CacheControl marshalling/delegate
  • [RESTEASY-30] – Implement encoding in UriBuilder
  • [RESTEASY-32] – Implement @Encoded support
  • [RESTEASY-35] – Support for javax.security annotations
  • [RESTEASY-37] – Update API to Public Draft
  • [RESTEASY-38] – Implement UriBuilder.extension()
  • [RESTEASY-41] – Implement Httpheaders.getAcceptableLanguages() and getRequestHeader()
  • [RESTEASY-42] – Support for MediaType and Language file suffix extension mappings
  • [RESTEASY-43] – Test servlet init-param parsing of map extensions
  • [RESTEASY-44] – Support for ApplicationConfig

Older Entries Newer Entries

%d bloggers like this: