icepush application list

I want to build an application allow some updates from a user to be notified to all other online users.
I choose icepush to implement the comet thing and it works very well.
The problem is I’m confused about where to store references to the applications.
I try both a static list and an event/listener implementation (my application don’t need to be able to scale out horizontally)
but still don’t know when to add and remove the reference/listener from the list/event-provider.
I’m worrying about the application resource cannot be freed after session expired.
So can anyone provide some practices to do this?
Can modifying the icepush servlet source code help me in some way?

If I’m using the Blackboard addon and threadlocal pattern (my ui classes call MainApplication.getCurrent() everywhere and I have a blackboard for each application),
can I set the application I want to push to the current threadlocal anywhere so I don’t have to modify those ui classes?

Normally, you would use a servlet listener (HttpSessionListener) to cleanup your session. At that point, you are not running in a thread, so a ThreadLocal is no help.

The simplest workaround is to add a cleanup() method to your Application. In your application, retrieve the http session, add a session attribute to keep a reference to your application (session.setAttribute(“vaadinApp”, myApplication) ) .

When the session ends, the method sessionDestroyed() will be called, and from there, you can get your session through the session event. Then you do a getAttribute(“vaadinApp”) to get your application, and call cleanup().

You should also implement the window close handlers; these should also call session.invalidate() when appropriate; this will trigger the sessionDestroyed() as well and call cleanup().

Thanks for your response. I will try the SessionListener.
But do we have to store another application reference?
Vaadin ApplicationServlet always store all the application state in session, doesn’t it?

When the session expires, the class you registered as your HttpSessionListenerl will get called. The ONLY thing available at that point is the HttpSession.

Remember that there is EXACTLY ONE servlet instance, shared between all the sessions. When the container calls you, it creates a new thread, and in that thread, it calls the service() method ot the servlet object, and makes sure you get the correct HttpSession. So contrary to what might be expected, there is only one servlet instance and not one per request, but several threads, each calling service().

So even if you make a special servlet class, and make it implement the HttpSessionListener interface, you are not any closer to a solution: the servlet’s instance variables are shared between all sessions, because there is only one. The only way to get your application back to clean things up is for you to register it in the session. A cursory glance at the source makes me think that Vaadin does not do such a registration, and if it did, you wouldn’t want to rely on it because as far as I know there is no public specification on how/where it does.

So make your application implement HttpServletRequestListener, and inside there, copy your reference to the current application instance to a session attribute.