users@grizzly.java.net

RE: OnEvent() never invoked, Comet source code analyzed

From: Jake Lockerbe <lockekal1965_at_aol.com>
Date: Tue, 13 Jul 2010 19:32:56 -0400 (EDT)

 Hi, Alexy

as I stated in my earlier posts it appears that onEvent is never invoked.
I apologize if I am misunderstanding you, but are you saying that it is correct for the addCometHandler method to not add the handler instance to the ConcurrentHashMap of the CometContext (I am referring to the source code here)? If so, then how does the onEvent method ever get invoked within the NotificationHandlers's notify() since the iterator iteratorHandlers seems to be pointing to an empty set ?

Can you get this code running or is there a flaw somewhere? Also, I have tried putting in some code in my
onInitialize() to write out a brief string. Unfortunately, the string only gets written once the expiration timer
expires.


This is my code that I posted yesterday.


 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();
               event.getCometContext().resumeCometHandler(this);
            }
        }
        
        public void onInitialize(CometEvent event) throws IOException
        {
                PrintWriter writer = response.getWriter();
                writer.write("I got here");
                writer.flush();
        }
        
        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);
            }
        }
    }
}