users@grizzly.java.net

Re: onEvent() not invoked by notify()

From: Oleksiy Stashok <Oleksiy.Stashok_at_Sun.COM>
Date: Tue, 13 Jul 2010 14:44:57 +0200

Hi,

you need to resume the CometHandler in the onEvent method like:

         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);

             }
         }


WBR,
Alexey.

On Jul 13, 2010, at 0:54 , lockekal1965_at_aol.com wrote:

> 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);
> }
> }
> }
> }