la vía láctea

Originally uploaded by sanabria

Simon Willison’s blog post made me try Comet finally. The Jetty and Dojo demo has worked without any hassle. Simon said:

The entire application took less than an hour to put together, which I think is a testament to the quality of the Bayeux implementation present in both Jetty and Dojo. […] Comet is probably about 90% of the way to being usable for mainstream project.

So I have decided to give it a go in my new experimental monitoring project and got a core feature working – a RESTish webservice being able to publish and cache a message and retrieve the latest message from the cache on top of Comet in Jetty and Dojo. Bayeux’s channel names are used as URIs in REST and keys in the cache.

What I wanted to archive with this feature is to mashup monitoring data from various tools, Nagios, Ganglia, etc. by server push. Even a simple chat can be in the same page!

First, I have replicated the demo into a webapp war and deployed it in tomcat 6.0, in which process I have learnt how it works more in details. Basically what I need is to deploy a Jetty’s Continuation Servlet, which does all server side magic for you.

I have chosen MemCached to cache Bayeux’s messages which are JSON objects.

The most difficult part so far was hooking the caching function into the Jetty Cometd Servlet. First, I tried DataFilter, but I can’t get the real channel id as I use a wildcard channel “/**”. It always returns the root channel “/”. Next, I tried BayeuxService used in the echo demo, but it does not allow to subscribe a wildcard channel. So I ended up adding a simple server side client which updates the cache on deliver method in Listener interface.

The RESTish webservice servlet, mapped to /galaxy/*, serves the only 2 methods, the HTTP GET to return the last Bayeux’s JSON message on the channel named by getPathInfo() and the HTTP POST to publish posted data onto the channel named by getPathInfo(). For example,

% curl http://localhost:8080/galaxy/nagios
{“channel”:”/nagios”,”id”:”5″,”data”:{“status”:”CRITICAL”}}
% curl -d “status=OK” http://localhost:8080/galaxy/nagios

At client side I use jQuery with Dojo and its color animations to notify a changed value by changing the background colors. It loads the initial data by getJSON on the galaxy servlet and updates the data by cometd subscriber.

So far so good.

About these ads