session-specific info to comet's "onEvent" method

56 views Asked by At

I have two questions about using the Comet feature with Glassfish. I'm pretty new at this, so if there's an easy answer or some documentation I should read please let me know! Thanks.

I'm building a system that has multiple microcontrollers sending data to a central DB. Users view the data via their browsers ... in formats (metric vs. English, say) of their own choosing. The display needs to be updated without user action. It looks like Glassfish + Comet should be perfect. And so I started with Oracle's "hidden_Comet" example, and that works great.

So question #1 is this: how can one get session-specific information into the "onEvent" method?

As context, here's the code; it’s straight from the Oracle example:

private class CounterHandler implements CometHandler<HttpServletResponse> {

    private HttpServletResponse response;

    public void onEvent(CometEvent event) throws IOException
    {
        if (CometEvent.NOTIFY == event.getType())
        {

            PrintWriter writer = response.getWriter();

            writer.write("<script type='text/javascript'>");

            [... etc. Here is where I need to pass some session-specific
             info to the JavaScript]

            event.getCometContext().resumeCometHandler(this);
        }
    }

It would seem that session attributes would be perfect, but it looks like you can't get the 'session' variable from the "HttpServletResponse". I thought about using cookies, but they seem to be accessible only with HttpServletRequest, not "...Response", and, as above, only ‘response’ is available in the “onEvent” method.

So question #1 is: how do you do this?

Question #2 is: is this just the wrong way to attack this problem and is there a better way?

1

There are 1 answers

0
user3092017 On

I'm not sure I understand the data structures and control flow of Comet very well yet, but it seems that this works:

Add a constructor to "class CounterHandler" and pass in the 'session' variable from 'doGet()' where "new CounterHandler" is called. Specifically, change:

 CometHandler handler = new CometHandler();

to

 HttpSession session = request.getSession();
 CometHandler handler = new CometHandler(session);

Have the constructor save the session variable in a class instance variable. And then the "onEvent()" method has access to session attributes. And Bob's your uncle.

(It seems straightforward enough ... well, now.)