Home > Uncategorized > Asynchronous Comet Processing

Asynchronous Comet Processing

Recently I’ve had the opportunity to visit companies that build their product on top of GlassFish Grizzly’s Comet API and brainstorm about what improvements we should add to the current Comet API, what limitation, etc. The most interesting idea I got from what I’ve seen is what I call Asynchronous Comet Processing. But wait, since Comet is already asynchronous, what does that means? Well let’s take an example to illustrate the problem most if not all existing Server that support Comet implementation “suffer”. In case this is the first time you read about Grizzly’s Comet, you might want to read this Comet example before continuing.


In Grizzly, a request will be marked as a Comet request (or long polled request) when its response object is attached to a CometHandler:

                   CometRequestHandler handler = new CometRequestHandler();

After the CometHandler has been added, the request will be polled, waiting for server data push coming from other http clients, EJBs or databases. The server will push data as soon as they come, e.g. when the CometContext’s notify method is invoked (a small AJAX example):

                    cometContext.notify("<script id='" + username + "_" 
                                 counter++ + "'>" + "window.parent." + callback
                                 + "(" +  message + ");</script>")

Internally, the cometContext will notify every CometHandler (Comet request or long polled connection) by invoking their:

                    public void onEvent(CometEvent);

For a limited number of CometHandler, the component that invoke the notify method will block a few seconds and then continue. But let say (as I’ve learned last week by seeing real world Comet apps) you have 5000 CometHandler (ya, 5000 Comet requests). Invoking the CometComet.notify method will block until all CometHandler has been notified. This is what I call synchronous Comet processing. For some application it will be desired to block on the notify, but I suspect for the majority of Comet application, blocking might not have the desired effect. Hence I’ve implemented in Grizzly Comet what I call asynchronous Comet processing (ACP). I’ve also enabled the feature by default, meaning invoking:


will no longer block. For some application (if not all) it will significantly improve the performance (or at least the user experience). The important thing to remember is you don’t have to write any thread management code inside your application because this is handled internally by the Grizzly default pipeline. Of course you can always spawn a thread from your Servlet to do the same, but I always prefer to delegate thread management stuff to the container. With Grizzly, you just need to decide if the notification process needs to be blocking or not. Event better, an application might decide that in some case the notify method needs to block (like when you know which CometHandler to call), which is easy to do by just invoking:

                    cometContext.notify("ACP is really nice");

The drawback of asynchronous Comet processing is now you must make sure your CometHandler is thread safe because Grizzly internal threads may call the CometHandler.onEvent() concurrently as multiple http requests could fires cometContext.notify() operations. The simplest way (but not the fastest way..read/write lock might be better) is to do something like:

                    public synchronized void onEvent(CometEvent event){

OK I’ve learned something during my last California travel!

_uacct = “UA-3111670-1”;


Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: