Skip to main content

Grizzly attacks! DWR's Reverse Ajax to support The Comet Implementation!

Posted by jfarcand on November 23, 2007 at 10:05 AM PST

Since day one of Grizzly Comet, I'm getting requests on how to use DWR's Reverse Ajax with the monster. Well, I did my lesson and looked at the DWR's code (quite good) and submitted a patch. Most probably the next official release will contains Grizzly Comet support (Read below to download a preview)! The result is quite impressive!


Since DWR already supports Jetty Continuation, it wasn't that complicated to find where the Grizzly Comet code should go. But wait, that wasn't that simple because the way Comet is implemented in Grizzly is quite different from Jetty's implementation. With Jetty, to suspend a connection, you invoke:

  Continuation continuation = ContinuationSupport.getContinuation(request, mutex);

In Grizzly Comet, to suspend the connection, you need to invoke:

  CometContext ctx = CometEngine.getEngine().register("DWR");
  CometHandler continuationHandler = new CometHandler() {
        public void attach(Object o) {
        public void onEvent(CometEvent cometEvent) throws IOException {
        public void onInitialize(CometEvent cometEvent) throws IOException {
        public void onInterrupt(CometEvent cometEvent) throws IOException {
        public void onTerminate(CometEvent cometEvent) throws IOException {

As you can see, you need more operations to achieve the same behavior using Grizzly, but I consider Grizzly implementation more flexible because you have more control over the connection life cycle (ex: the Grizzly Comet share the connection state with the CometHandler, which Jetty Continuation isn't offering unless you explicitly query the API...and no exception is thrown when suspending the connection, and the Servlet.service is not called twice ;-) )...anyway kind of a blind statement knowing that my goal for this blog is not to compare both implementation (but one day I should compare both Jetty's Comet with Grizzly's Comet following this comparison :-)). In short, I would think that Grizzly Comet is more a framework than a simple Comet/Continuation API. More important, I will start lobbying Greg to have a common Continuation implementation now than Jetty supports the latest Grizzly!

Now back to DWR avoid any hard dependencies on Jetty, DWR uses reflection for loading the Jetty Continuation classes. Since DWR is established since a long time, my goal was to avoid adding any new API to DWR code base so I've decided to wrap the Grizzly Comet "Framework" classes into a simple Continuation interface that offer the same kind of API than Jetty offer. Another reason to come with a simple API was to be able to support all version of Grizzly Comet, independent of the container where you are running (GlassFish, Sailfin, Grizzly, OpenESB, etc.). So I came with a new Grizzly 1.6.x API that wrap the code described above and offer:

  GrizzlyContinuation continuation = Continuation.getContinuation();

Yes, it looks like quite similar to Jetty Continuation, but Greg Wilkins (Jetty lead) have also submitted his API to the Servlet 3.0 EG, so I'm just ahead of the spec :-). Using that API (which under the hood hide the CometHandler handling code described above), it was quite simple to add support for Grizzly Comet in DWR's Reverse AJAX.

Wants to see the result? Just download a 'pre-release' version of DWR supporting Grizzly Comet here. It's a pre-release because the official one will be available inside the next DWR release, and my patch would have been reviewed by the DWR's team as well. To install the demo, just do:

  Download GlassFish and install it. See instruction here.
  % cd ${glassfish.home}/bin
  % ./asadmin set
  % Start (or restart) GlassFish
  % ./asadmin deploy /path/to/dwr.war

Open your favorite browser and type http://host:ip/dwr. Click on the Reverse Ajax examples link. Simple demo, but demonstrate the power of DWR's Reverse Ajax!!!!

OK, now you don't have any reason to not use Grizzly Comet as you have several client technologies available: Dojo Toolkit (for Cometd), ICEfaces (for Comet), DWR's Reverse Ajax (for Comet), behaviour.js (for Comet), JMaki (for Comet). The Comet Revolution has started!!


Related Topics >>