users@grizzly.java.net

Re: Problem with comet-based web app

From: Richard Corbin <igf1_at_yahoo.com>
Date: Wed, 29 Oct 2008 10:59:39 -0700 (PDT)

________________________________
From: Hiram Coffy <hiram.coffy_at_pb.com>
To: "users_at_grizzly.dev.java.net" <'users_at_grizzly.dev.java.net'>
Sent: Wednesday, October 29, 2008 12:57:25 PM
Subject: Re: Problem with comet-based web app


Hi all,
 
I am still struggling with
getting my comet-based servlet to work. Any help would be much appreciated
 
I guess what would be helpful
for me is a simple step-by-step procedure. So far, what I have found is
scattered and fragmented bits and pieces of information, critical details
appear to be glossed over.

<reply I too had a difficult time getting acclimated to Grizzly. Documentation is kinda scarce and much of what you will find is contributed in respect to older versions, that said its not as complicated as it seems at first.. />

 
I suppose I can boil down my
request to the following set of questions:
1) APP SERVER: What do I need to
get comet-based servlet working? e.g. glassfish V2UR2 with comet support
enabled.
<reply>
In GF 2 you simply open the Http Listeners section in the admin (default: localhost:4848 {username:admin pass:adminadmin}):

1. From the control tree on the left, navigate to HTTP Service->HTTP Listeners -> http-listener-1 (thats the default 8080)
under the "Additional Properties" section click the "Add Property" button and add name="cometSupport" value="true"

now restart the server and it should say something about Grizzly ARP , thats how you know your in business ;-).
</reply>



2) API: What compatible comet
api should I compile the source code with?
<reply>
this is anoher one that got me, in your GF 2 project you cant use the ones posted on the Grizzly site (well you can , but it has to be the version that GF loaded at startup), you have to use the <Glassfish Install Dir>/appserv-rt.jar , this will include your comet functionality -> import com.sun.enterprise.web.connector.grizzly.comet.*
</reply>


Iv attached a simple comet servlet that pushes an incrementing number to a client Once you do steps 1 and 2 just cut/paste this, "fix imports" and access it.
rules.
1. dont close the ServletResponse
2. dont forget to call flush() on the ServletResponse's PrintWriter once you do a write();
 
You see all you really need to uderstand is, you create a CometContext this is the heart of the comet server, in fact you can think of it as an instance based server. You add client connections to this "server" / object, Clients in this case are Comet Handlers. when you want to push a message to every client you have in a given context you just call CometContext.notify(<message>) , this fires off the onevent(Object o) in all of the observer instances of CometHandler to the given CometContext..

GF2 Servlet.
<servlet>

public class CometServlet extends HttpServlet{

    public void destroy() {}
    public ServletConfig getServletConfig() {return getServletConfig();}
    public String getServletInfo() { return getServletInfo();}
    public void init(ServletConfig arg0) throws ServletException {
        
    }

    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
        try{
           this.ch.attach(response);
           cc.addCometHandler(ch);
              cc.setExpirationDelay(-1); // time in milis before the handler terminates. -1 means don't ever terminate

           new datapush(); // starts a new thread that pushes count++ to clients.
            
        }catch(Exception i){i.printStackTrace();}
    }
    
    
CometContext cc = CometEngine.getEngine().register("comet");
CometHandler ch = new CometHandlerImpl();
    public CometServlet() {
       
    }
    
     class datapush{
        Timer t = new Timer();
        Integer i = 0;
        public datapush() {
               
        t.scheduleAtFixedRate(new TimerTask() {

                @Override
                public void run() {
                  try{
                  String Message ="i="+i++ ;
                      
                  cc.notify(Message);
                  }catch(IOException ioe){ioe.printStackTrace();}
                }
            }, 5000, 5000);
        }
        
        
    }
    
    class CometHandlerImpl implements CometHandler<HttpServletResponse>{
    HttpServletResponse response;
  
        public void attach(HttpServletResponse arg0) {
           this.response = arg0;
        }

        public void onEvent(CometEvent arg0) throws IOException {
         System.out.println(""+arg0.attachment());
            this.response.getWriter().write(""+arg0.attachment());
            this.response.getWriter().flush();
        }

        public void onInitialize(CometEvent arg0) throws IOException {
            System.out.println("Initialized Handler");
        }

        public void onInterrupt(CometEvent arg0) throws IOException {
            arg0.getCometContext().removeCometHandler(this);
        }

        public void onTerminate(CometEvent arg0) throws IOException {
            arg0.getCometContext().removeCometHandler(this);
        }
        
    }
   
    
}

</servlet>

Good Luck!!
-- Richard Corsale