Sreeni's Weblog

How to make Grizzly Comet work with Java Client instead of Web Client

Thursday Feb 19, 2009

comet_client There are many examples illustrating the usage of Grizzly Comet for Web Client but not for Stand alone java client. I needed one and with help from Jeanfrancois Arcand, users@grizzly.dev.java.net and my colleague Prashant Abbagani, I came up with the following example.

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
    

[3] Comments
Like this post? del.icio.us | furl | slashdot | technorati | digg
Comments:

Hi! Thank you for this! I'm new in Comet programming and this example is just what I needed to get me started.

I noticed something strange: the CometClient works properly only after you restart GlassFish, on successive runs it doesn't receive any messages (not even 'init ...').

Correct me if I'm wrong, but it seems that the cause of this problem is that the CometContext is registered with the CometEngine in doPost(), and I think this can be done only once, so the successive attempts fail.

I placed the reg. code in the init() method of the servlet, which solved the problem.

public void init(ServletConfig config) throws ServletException {
ServletContext context = config.getServletContext();
contextPath = context.getContextPath();

CometEngine engine = CometEngine.getEngine();
CometContext cometContext = engine.register(contextPath);
cometContext.setExpirationDelay(30 * 1000);

}

Posted by Kitty on April 29, 2009 at 10:20 AM PDT #

Thanks for reading my blog. We need more than one context as we allow more than one client hence it was done in post method. It makes sense to have in the init method if only one context is needed.

Posted by Sreeni on April 29, 2009 at 09:33 PM PDT #

I understand now what I did wrong: I tried to run the client more than once with the same user name :P No wonder it did not work...

Posted by Kitty on April 30, 2009 at 05:28 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed