0


I start try Comet to create a notification system using JMS in server side.
I need that a web service send asynchronous message to the servlet notifying the CometHandler.

I started using the counter sample of Glassfish (http://docs.oracle.com/cd/E18930_01/html/821-2418/ggrgt.html#ggrgr), but it seems it create a lot of handler.

The problem (I think) is that the comet handler is created in each request

protected void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {

    CounterHandler handler = new CounterHandler();
    handler.attach(res); //here the response of this request is attached to handler

    CometEngine engine = CometEngine.getEngine();
    CometContext context = engine.getCometContext(contextPath);

    context.addCometHandler(handler);
}

Then in the CometHandler onEvent the Handler send the response to client and then resume itself

public void onEvent(CometEvent event) throws IOException {
    if (CometEvent.NOTIFY == event.getType()) {
        response.getWriter().write("something") //response is the attached one
        // commented out the resume if it is Http Streaming
        event.getCometContext().resumeCometHandler(this);
    }
}

And in the HTML page I use JQuery to do a longPoll like this

function poll() {
    var url = pollUrl;
    $.getJSON(
        url,
        function(data) {
            updateAnswer(data);//do things with data
            poll();
        }
    );
}

So, each getJSON, a new CometHandler is created and the one before is "forgetted".
The question is: there are a better method to do this?
And why I need to resume the handler if the next that I use is another different?
Thank you for help!

-- EDIT --

I try to put the cometHandler in a HttpSession attribute, but the problem still remain, because I need to use addCometHandler(handler) because as javadoc say:

Add a CometHandler which will starts the process of suspending the underlying response.     
The underlying HttpServletResponse will not get committed until   
CometContext.resumeCometHandler(CometHandler) is invoked, unless the 
CometContext.setExpirationDelay(long) expires.

And I see another things using resumeCometHandler(this)

Resume the Comet request and remove it from the active CometHandler list. Once resumed,    
a CometHandler must never manipulate the HttpServletRequest or HttpServletResponse as 
those object will be recycled and may be re-used to serve another request. If you cache  
them for later reuse by another thread there is a possibility to introduce corrupted  
responses next time a request is made.

So I don't know if there are a manner to don't create a new CometHandler for each request. I put a counter and increment it in onInitialize handler method and I decrement it in onTerminate handler method and it increase in each request.

Enrico
  • 71
  • 1
  • 6

1 Answers1

0

finally it seems that there aren't problems.
Although each request create a handler, it will be remove from list and will be cleared when garbage collector is called.
To try it I force the gc and every CometHandler that is not in the suspended list is finalized.
I try to force a lot of new request and the garbage collector is called when there are a lot of comet handler instantiated, so I still don't like so much this approach.
Every new idea is welcome.
Regards

Enrico
  • 71
  • 1
  • 6