Search |
||
Introducing Grizzlet! Building Ajax/Comet based application with POJOPosted by jfarcand on September 21, 2007 at 11:00 AM PDT
The GlassFish's Comet implementation is 'live' for more than one year and fortunately, there is some really cool applications build on top of it (ex: 4homemedia, the co-founders of Kaazing nicely talked about it, internally our Sun Instant Messaging(XMPP protocol) systems is also using it, and much more). The Grizzly user list get regularly questions about Comet, and I'm getting a lot of private emails on the topic as well. But one thing I've learned with all those interactions is the fact that Ajax developers aren't that interested about writing a 'heavy' Servlet|JSP|JSF components in order to use the power of Comet. Even with Jetty, which was the first to support it and as a well defined API for Comet, it is still difficult....I'm not talking about Tomcat, as the community is collapsing those days, fighting about internal problems instead of innovating with Comet :-(. Hence most of the Ajax developers I've talked recently are not interested to learn how to write a Servlet, how to package it (although it is quite simple thanks to the Java EE 5), then learn what Grizzly Comet is and how you can use it. It would be much more simple if we can write a single POJO class, hook it to an Ajax client, and bang make it work! So here comes Grizzlet, an extremely simple POJO based approach to write Comet based application!
What is a Grizzlet exactly? A Grizzlet is a very simple POJO that implement a single interface, the com.sun.grizzly.grizzlet.Grizzlet interface: The interface consists of two simple methods: onRequest and onPush. The onRequest method is similar to the Servlet.service method. The Grizzlet Container will invoke that method when a request is coming. From the AsyncConnection, it is possible to decide if the connection needs to be suspended(meaning parking the request), resumed or handled normally (synchronous request). This is also where you will decide to initiate a server push (to all connections that are suspended). The onPush method will be invoked by the Grizzlet Container when the AsyncConnection.push() is invoked, and this is where you will decide if you want to push data on a suspended connection. Still looking complicated? Let's re-write the Chat application TAKAI Naoto did on top of Grizzly Comet, but this time using a Grizzlet POJO. First, let's take a look at the onRequest method: First, line 52/53 show how to get the Request/Response object. From the Request object, we get the http method, write some information, and then suspend the request. Once suspended, the connection is parked by the Grizzlet Container. As an example, if the connection was initiated by an iFrame on the client side, the browser logo will spin, waiting for the response to complete, which means the connection has been suspended by the server. From the client, any POSTs will ends up on line 70. Then for the Chat demo, a server push will be initiated when the AsyncConnection.push() method is invoked. As soon as the AsyncConnection.push is invoked, all suspended connections will have a chance to resume and push data back to their associated clients: The interesting part here is line 102 to 104. This is actually where the push happens. Every suspended/parked connections can now push back some data to their client, and then return in the suspended state. This is it! Nothing more need to be written on the server side. As for the client side, you can use any Ajax libraries to render and communicate with the server side. BTW Download the Grizzlet Container here and the Chat demo from here. But wait, which Web Server supports Grizzlet? As of now, I've build a Grizzlet Container on top of Grizzly Web Server. My upcoming works on Grizzlet will consist of writing a generic Servlet that can redirect requests to a Grizzlet when an application is deployed in GlassFish v2, similar to what Goldspike does for JRuby applications. If peoples are interested to help, just join Project Grizzly! As usual, I've try to made it extremely simple and easy to start the Grizzlet Container (even faster than GlassFish v3 :-)): Mainly, the first parameters is the port on which the server listen, the second is the application folder, and the last one is the Grizzlet class name to use. The application folder is quite simple: A single index.html, the Grizzlet class and the javascripts/stylesheets. That's it. At least I've find it myself quite simple :-). The good news is the Grizzlet's extension work like the Grizzly's JRuby on Rails, which means we gonna soon be able to invoke Grizzlet from a JRuby on Rails application. But wait, from the example above, you can only "deploy" one Grizzlet per Grizzlet Container? That's true....for now, I've made it simple but if the features interest peoples, then it will be quite trivial to do the same trick as the JRuby on Rails extension: plug it into GlassFish v3/hk2 micro-kernel and have all the nice deployments features v3 is offering. Another long story from the North.... Finally, if you want to see a killer demo on Grizzlet, just stop next week at AjaxWorld when I will unleash the Grizzlets everywhere and demo nice applications.... technorati: grizzly grizzlet asynchronous request processing comet ajax glassfish »
Related Topics >>
Java Enterprise Comments
Comments are listed in date ascending order (oldest first)
|
||
|
|