How to make Grizzly Comet work with Java Client instead of Web Client
Thursday Feb 19, 2009
Conventionally the client either polls or pulls the message from server periodically, the draw back with this approach is that the client wastes the resources and the messages may not be received in real time. Instead, in comet approach, the server pushes the messages to client in real time. For this example, I have used GlassFish v3 which comes bundled with Grizzly and Comet. Here are the steps:
-
Download and install GlassFish v3.
-
Enable comet support in GlassFish v3.
... <http-listener port="8080" id="http-listener-1" address="0.0.0.0" default-virtual-server="server" server-name=""> <property name="cometSupport" value="true" /> <http-listener> ... -
Develop and deploy comet.war to push messages from server to client. You may like to refer to MyCometServlet.java source code.
 
-
Code the client CometClient.java and listen for server messages.
Start the client.
punit[105]: java CometClient init ...
-
Simulate posting messages to server using Post2CometServlet.java.
Post message to server
punit[54]: java Post2CometServlet punit[55]: java Post2CometServlet punit[56]: java Post2CometServlet punit[57]: java Post2CometServlet punit[58]: java Post2CometServlet
The client output gets updated in real time as the messages are posted to the servlet.punit[105]: java CometClient init ...my message sent at 1235082867986 my message sent at 1235082891002 my message sent at 1235082892018 my message sent at 1235082893714 my message sent at 1235082894420