Search |
||
Writing a Twitter like application using Grizzly Comet part 1: The ServletPosted by jfarcand on November 30, 2008 at 9:01 PM PST
Twitter is more and more popular and I've decided to write my own Twitter using Grizzly Comet. The result is amazing: 150 lines of Java code and an amazing grizzly transformed into a bird! Grizzly Comet is a framework build on top of the Grizzly. With Grizzly Comet, you can create powerful asynchronous application. Grizzly Comet support:
OK Enough charabia! Let's demonstrate the power of the framework by re-writing Twitter and make it a full real time and asynchronous application. No need to refresh the page anymore. In this first part, I will explain the server side...the client will comes next, but get ready, I'm far from an expert with JavaScript :-). But first, let's see what the end results will looks like...all of this using a single Servlet!
Now let's deep dive into the monster's Comet API CometEngine: The CometEngine is the entry point to the framework. The first steps when writing a Grizzly Comet application is to first create a 'group' or 'topic' object that can be used to suspend, share, filter, throttle, aggregate and resume connections: In the above, we grab the static instance of CometEngine and call register(id) to create a CometContext, an object representing suspended connection based on a topic. Note that CometContext can be created from everywhere like EJB, POJo, etc. This is quite important if you are planning to push events from non web components CometContext: The most important object of the Grizzly Comet Framework. A CometContext represents a group of suspended connections. From a CometContext you can push events, define your own mechanism of filtering/aggregating/throttling, suspend and resume connection: In the code above, we first create a CometContext, then add a ReflectorCometHandler (more on this below) and add some attributes. Invoking addCometHandler(handler) automatically suspend the connection. CometHandler: This interface represents your suspended connection. Defining a CometHandler allow a web application to handle the lifecycle of a suspended connection. Events are pushed to a CometHandler as soon as they occurs. Event like when the connection get suspended (onInitialize), when server event are pushed (onEvent), when your application decide to resume the connection (onTerminate) or when the client close a suspended connection, or the connection was idle for X times (onInterrupt). The most important method is the onEvent, where you are usually define what you will do with the event, e.g. store it, write it, discard it etc. As a very simple example (this is the one used by this Twitter application), Grizzly Comet ships with the RefectorCometHandler, which does nothing except writing all messages it gets: But how CometHandler gets invoked? CometHandler gets invoked when an application invoke CometContext.notify(CometEvent). As an example, let's take explain how a chat application works. First, users (browsers) enter a chat room, waiting for message. As soon as they enter the chatroom, a Grizzly Comet implementation will invoke CometContext.addCometHandler(). That means those connections are suspended, waiting for events. If a user enter some message, the way to share (or push) that information back to the users is by invoking CometContext.notify("Salut"). Automatically the CometHandler.onEvent() will be called, and if the ReflectorCometHandler described above is used, then the message will be directly written to the suspended connection. In effect, that operation will push back the message to the client. You can always filter messages before they reach CometHandler (details in part III) So, four simple steps:
And now, Twitter Twitter Twitter Let's first define what the application will do:
![]() So let's go steps by steps on how we can build a Twitter like application. For Twitter, we will use a single TwitterServlet, and we will define the basic as: All the logic happens inside the doPost. First, sign in Twitter: I will talk in more details in part II about how the client works, so let's just describe the basic. Below is the code that allow a user to sign in On the server side, this is as simple as: In short, we grab the action value from the doPost, and also we look for a CometContext. The CometContext here represents the user blogs (or update), and all CometHandlers added to this "user" CometContext will receive updates when the user micro blogs/updates his page. Of course on the first request the CometContext is null as their is no suspended connections. On the client/broser side, the first action sent is 'start' (when the page is loaded), and this is where we will suspend the connection: The most important code above is when we create the CometContext (based on the session id), then create a ReflectorCometHandler that gets invoked when it is time to write back to the browser updates made to the CometContext. The CometContext as described above. Next, we suspend the connection/response by invoking addCometHandler. We use the session to store the CometContext and also the CometHandler representing the user suspended connection. As soon as the user sign in: From the user's name, we push our first message back to this user using the suspended connection. As we noted above, we push the message using the CometContext.notify, which in turn will invoke the ReflectorCometHandler.onEvent(), which will write the message back to the client. Now the user is ready to micro blog/updates. Time to micro blog using Grizzzly!!!!! As soon as the user update his status, the client sent: This looks like way to simple, right? For any update, we just need to get the CometContext and invoke notify on it, and BINGO, all our followers will be updated in REAL-TIME!. What is a follower? A follower is another users that want to get updated when another user enter a new micro blog. Followers register themselves by entering the name of the user they want to follow: The 'name' is the user which want to follow another user, which is represented by the 'message'. First, we get the CometContext representing the user we want to follow, and add our CometHandler to it. That means that every time the followerContext ill be updated, we will also get the update via our CometHandler. Too cool!. We also update our CometContext by pushing a message telling our follower that we are now following a new user. We also update the followerContext by pushing a message saying we are now following that user. Yes, that the only steps you have to write in order to build a Twitter like application using Grizzly Comet. One thing to remember is that everything happens using a CometContext. From that object, you can notify/filter/aggregate/ etc. a set of suspended connection represented by a CometHandler. In the current example I'm using the ReflectorCometHandler, but you can write your own by implementing the CometHandler interface. Now ready to try it? Two possibilities. For development, I recommend you use the embedded Grizzly Comet Server and just do For production or if you need to uses Eclipse or Netbeans, download GlassFish v3 and deploy the grizzly-twitter.war application like any other web application. The application and source can be downloaded here. If you want to improve it or contribute, you are welcome to join the Grizzly community! OK next time I will explain in details how the client works (at least I will try :-)). Please post your questions on users@grizzly.dev.java.net so the Grizzly community can help and respond :-) technorati: grizzly comet grizzly glassfish v3 comet Twitter »
Related Topics >>
Glassfish Comments
Comments are listed in date ascending order (oldest first)
Submitted by ashvini007 on Mon, 2008-12-01 02:17.
hi jean, if u can give me a favour.........please tell me..........how to set a jar file in to a window service..............i have a jar file.......and i don't want to execute it by double clicking..........rather i want to set this jar file as a windows service,,,,,,,,,,,,,do u have any idea..............please......
guide me .......ashvini007@gmail.com
Submitted by gustav3d on Mon, 2008-12-01 13:15.
in comethandler you use :
public synchronized void onEvent(CometEvent event)
why do you let worker threads to block waiting for the current worker thread to finish its client io ?.
thats what your design allows for.
i did send you a patch where the comethandler queues io and allows other threads then the current one to return and not block and wait.
Submitted by jfarcand on Mon, 2008-12-01 13:21.
@Gustav, the CometHandler must be synchronized because two CometContext might invoke simultaneously the CometHandler. I still need to look at you patch :-)
Thanks!!
Submitted by pollux0505 on Wed, 2008-12-03 22:03.
hi jfrcand:
could grizzly comet work in tomcat?thanks
Submitted by aalcamo on Thu, 2008-12-11 10:07.
Jean Francois,
I am having difficulties updating my current installation of Glassfish v3 Prelude with the latest Grizzly snapshot. Specifically, I am working on an application that uses the DeliverResponse object to to notify clients and I have posted my issue on nabble, etc. Would you please let me know what the latest version of Grizzly Glassfish v3 Prelude supports, how to download it, and how to install it?
Thank you,
Anthony
|
||
|
|