users@grizzly.java.net

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

From: Oleksiy Stashok <Oleksiy.Stashok_at_Sun.COM>
Date: Wed, 14 Jul 2010 01:58:05 +0200

Hi Jake,


> 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 ?
CometHandler gets added to the CometContext right after control will
leave servlet doGet or doPost method.
So when you call context.notify(...) it won't touch the CometHandler,
which was added during the same doGet/doPost invocation.


> 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.
I tried your code and it worked as expected for me.
I've run 2 requests in separate browser windows one after another:
1) http://localhost:8080/app?client=test (request suspended
here)
2) http://localhost:8080/app?client=test&message=hello (this request
resumed window #1)

Can you pls. check this?

Thanks.

WBR,
Alexey.

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