users@grizzly.java.net

onEvent() not invoked by notify()

From: <lockekal1965_at_aol.com>
Date: Mon, 12 Jul 2010 18:54:56 -0400 (EDT)

 I am trying to get a simple Comet example working that I took from http://blogs.sun.com/msreddy/entry/how_to_make_grizzly_comet.

The issue is onEvent() never gets invoked. I have verified this by tracing through my code in NetBeans. I have verified that my comet server is
running, I can receive the requests from my browser, the comet handler is added and notify is invoked. But I get no response back from the server.
I am running NB 6.8, GF v3

Any ideas ?

 
My code is as follows:
public class MyCometServlet extends HttpServlet
{

    private class CometMsgHandler implements CometHandler<HttpServletResponse>
    {
        
        private HttpServletResponse response;
        
        public void onEvent(CometEvent event) throws IOException
        {
            if (CometEvent.NOTIFY == event.getType())
            {
                PrintWriter writer = response.getWriter();
                writer.write((String)event.attachment());
                writer.flush();
            }
        }
        
        public void onInitialize(CometEvent event) throws IOException
        {
        }
        
        public void onInterrupt(CometEvent event) throws IOException
        {
            removeThisFromContext();
        }
        
        public void onTerminate(CometEvent event) throws IOException
        {
            removeThisFromContext();
        }
        
        public void attach(HttpServletResponse attachment)
        {
            this.response = attachment;
        }

        private void removeThisFromContext() throws IOException
        {
            response.getWriter().close();
        }
    }
    
    private static final long serialVersionUID = 1L;
    
    @Override
    public void init(ServletConfig config) throws ServletException {
    }
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
    {
        doPost(req, res);
    }
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
    {
        String contextPath = req.getParameter("client");
        String message = req.getParameter("message");
    
        CometEngine engine = CometEngine.getEngine();
        if (message == null)
        {
            if (contextPath != null)
            {
               CometContext cc = engine.getCometContext(contextPath);
               if (cc == null)
               {
                   cc = engine.register(contextPath);
                   cc.setExpirationDelay(60000); // how long to hold client
                   CometMsgHandler handler = new CometMsgHandler();
                   handler.attach(res);
                   cc.addCometHandler(handler); //suspend connection
                   cc.notify("init ..."); //all suspended connections are are invoked.
               }
            }
        }
        else
        {
           CometContext cc = engine.getCometContext(contextPath);
            if (cc != null)
            {
                cc.notify(message);
            }
        }
    }
}