users@grizzly.java.net

Re: unregister context

From: Kawajiri Takeshi <taken.kz_at_gmail.com>
Date: Wed, 22 Oct 2008 10:57:22 +0900

Hello.
Thank you for your quick response.

> Grizzly will invoke the onTerminate()

I don't know this. thanks.
But My onTerminate didn't do anything.

> Yes you should.

Great! :)

> Can you share a test case?

OK. How about this.

----------------------------------------------------------------------------------------
package web;

import com.sun.grizzly.comet.CometContext;
import com.sun.grizzly.comet.CometEngine;
import com.sun.grizzly.comet.CometEvent;
import com.sun.grizzly.comet.CometHandler;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {

    private final static Logger logger =
Logger.getLogger(TestServlet.class.getName());

    @Override
    protected void doGet(HttpServletRequest request,
HttpServletResponse response)
            throws ServletException, IOException {
        String topic = request.getParameter("topic");

        logger.info("process: " + topic);

        CometContext context = CometEngine.getEngine().getCometContext(topic);
        if (null == context) {
            context = CometEngine.getEngine().register(topic);
            context.setExpirationDelay(-1);
            logger.info("create context. topic:" + topic);
        }

        CometHandler handler = new MyHandler(topic);
        handler.attach(response.getWriter());
        context.addCometHandler(handler);
        logger.info("add handler. topic:" + topic + " size:" +
context.getCometHandlers().size());
    }

    class MyHandler implements CometHandler<PrintWriter> {

        PrintWriter writer = null;
        String topic = null;

        public MyHandler(String topic) {
            this.topic = topic;
        }

        public void onEvent(CometEvent event) throws IOException {
        }

        public void onInitialize(CometEvent event) throws IOException {
        }

        public void onTerminate(CometEvent event) throws IOException {
            logger.info("==Terminate==");
        }

        public void onInterrupt(CometEvent event) throws IOException {

            logger.info("==Interrupt==");

            CometContext context = event.getCometContext();
            if (context.getCometHandlers().size() == 1) {
                CometEngine.getEngine().unregister(topic);
                logger.info("unregister context. topic:" + topic);
            } else {
                context.removeCometHandler(this);
                logger.info("remove handler. topic:" + topic + "
size:" + context.getCometHandlers().size());
            }
        }

        public void attach(PrintWriter writer) {
            this.writer = writer;
        }
    }
}

----------------------------------------------------------------------------------------

I deployed this servlet. and invoke a browser(A).
and access http://localhost:8080/test/TestServlet?topic=1
-------------log----------------------
INFO: process: 1
INFO: create context. topic:1
INFO: add handler. topic:1 size:1
--------------------------------------

and then, I invoke another browser(B)
and access http://localhost:8080/test/TestServlet?topic=2

-------------log----------------------
INFO: process: 2
INFO: create context. topic:2
INFO: add handler. topic:2 size:1
--------------------------------------

In this time, browser (A) and (B) is loading correctly.
and then, I close browser(B)

-------------log----------------------
INFO: ==Interrupt==
INFO: unregister context. topic:2
ERROR: CometSelector
java.lang.IllegalStateException: cometTask cannot be null
        at com.sun.grizzly.comet.CometEngine.interrupt(CometEngine.java:465)
        at com.sun.grizzly.comet.CometSelector.cancelKey(CometSelector.java:273)
        at com.sun.grizzly.comet.CometTask.doTask(CometTask.java:286)
        at com.sun.grizzly.http.TaskBase.call(TaskBase.java:359)
        at com.sun.grizzly.util.WorkerThreadImpl.processTask(WorkerThreadImpl.java:325)
        at com.sun.grizzly.util.WorkerThreadImpl.run(WorkerThreadImpl.java:184)
--------------------------------------

browser(A)'s loading is stop.

> How is you application after such exception?

looks like fail to interrupting.
In above testcase, browser(A)'s onInterrupt is never called.

thanks.



> alut,
>
> Kawajiri Takeshi wrote:
> > Hello.
> >
> > I'm using glassfish-v3-prelude-b28(Grizzly-Comet-1.8.6.1, NetBeans6.5
> > Dev 200810041417)
> >
> > My application have to use multi comet-contexts in a servlet.
> > and I want to unregister it when the context has no handlers.
> >
> > So I wrote CometHandler like this.
> > --------------------------------------------------------------------
> > public void onInterrupt(CometEvent event) throws IOException {
> >
> > CometContext context = event.getCometContext();
> >
> > if (context.getCometHandlers().size() == 1) {
> > CometEngine.getEngine().unregister(topic);
> > } else {
> > context.removeCometHandler(this);
> > }
> >
> > }
> > --------------------------------------------------------------------
> >
> > But when CometEngine#unregister is called,
> > all handler(Response) is closed regardless of attaching context.
> >
> > and a exception occoured
> >
> > --------------------------------------------------------------------
> > CometSelector
> > java.lang.IllegalStateException: cometTask cannot be null
> > at com.sun.grizzly.comet.CometEngine.interrupt(CometEngine.java:465)
> > at com.sun.grizzly.comet.CometSelector.cancelKey(CometSelector.java:273)
> > at com.sun.grizzly.comet.CometTask.doTask(CometTask.java:286)
> > at com.sun.grizzly.http.TaskBase.call(TaskBase.java:359)
> > at com.sun.grizzly.util.WorkerThreadImpl.processTask(WorkerThreadImpl.java:325)
> > at com.sun.grizzly.util.WorkerThreadImpl.run(WorkerThreadImpl.java:184)
> > --------------------------------------------------------------------
> >
> > If I comment out CometEngine#unregister, these don't happen.
>
>
> Hum...looks like a bug. Are you doing something inside your
> onTerminate() method? Because when calling the unregister, Grizzly will
> invoke the onTerminate(). I've filled issue:
>
> https://grizzly.dev.java.net/issues/show_bug.cgi?id=281
>
> to improve the documentation that doesn't state that.
>
> > My questions are
> >
> > (1) Can I use multi contexts?
>
> Yes you should.
>
>
> > (2) How to unregister context without effect to other contexts ?
> >
>
> Can you share a test case? Mainly, it seems the connection get resumed
> and at the same time interrupted. I've filled:
>
> https://grizzly.dev.java.net/issues/show_bug.cgi?id=282
>
> because I do think the exception should not be popped. How is you
> application after such exception?
>
> Thanks
>
> -- Jeanfrancois
>
>
>
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
> > For additional commands, e-mail: users-help_at_grizzly.dev.java.net
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
> For additional commands, e-mail: users-help_at_grizzly.dev.java.net