[Jersey] Simple suspend/resume

From: Christopher Piggott <>
Date: Tue, 12 Apr 2011 15:42:00 -0400


I have a fairly large jersey-guice application that is a kind of
RESTful database interface. It uses a bunch of different POJOs
encoded with JAXB. Everything is created via guice.

In one part of this application remote device upload objects. These
objects are stored in a database (using JdbcTemplate, but that's
probably not relevant). More happens than simple storage, but ignore
that for now.

Everyone object that is added has an EventID associated with it - it's
just a number that counts up.

One of my resources allows clients to "observe" the stream of incoming
objects. An observer does something like:

    GET http://server/events?start=100

This will return either a list of events, or an empty list. If it
returns a list of events, they will be 100, 101, 102, etc. then the
observer makes a new request:

    GET http://server/events?start=103

asking for 103 or higher.

Currently, the observer accomplishes this by polling. My goal is to
eliminate that polling.

I have been looking into atmosphere as a way to do this, but I'm
frustrated by the complexity of initializing and configuring it.
There are plenty of examples but they all require me to bend my
application and modify a lot of existing code. Whats worse, the whole
@Suspend/_at_Resume thing doesn't really seem to provide an easy way to
return immediately if any new events are available, and only suspend
(with timeout) if the current queue is empty. The only example I
could find of how to do this was to have my resource return type
SuspendResponse, and if there's no need to suspend, throw a
WebApplicationException with the "real" result. Throwing exceptions
in the normal course of things doesn't seem palatable to me.

I'm wondering if there's a better way.