users@glassfish.java.net

Re: Getting a list of all active ServeltSessions?

From: Wolfram Rittmeyer <w.rittmeyer_at_jsptutorial.org>
Date: Sat, 04 Apr 2009 12:47:31 +0200

glassfish_at_javadesktop.org wrote:
> Hi Guys,
>
> We're woprking on a very interactive app. As such the code handling one user sometimes needs to be aware of the state of other users in the system.
>
> Is there a proper servlet way to do this? Logically info on a user's state belonsg in his or her session, but I cant find a way to read that from a servlet being executed on behalf of a different user.

There is nothing in the API to do this. But you could use the
application scope (ServletContext) to store sessions. Either in a map
(in your case the username or some kind of groupname might be a useful
key) or simply in a set if you always need to access all sessions.

To ensure that no stale sessions are kept in this set/map you should use
a HttSessionListener to add and remove sessions.

I guess this approach is not suitable in a clustered environment because
the application scope is in fact only a scope per VM. You might have to
use an HttpSessionActivationListener as well in a clustered environment
which in itself can cause overhead in the case of passivating to disk
when the server shuts down or as part of its memory management.

Thus you might look at sharing the state that is relevant to all users
in a database. This way you wouldn't run into problems in a clustered
environment. And it probably makes it easier to query the relevant
session state as well.

--
Wolfram Rittmeyer