I would like to write session counter that works with session's migration.
There is an example in HFS&J, but it will not work with session's migration.
Is the issue that there will be an instance of the Counter class in each JVM, and so the actual count of sessions will be the sum of Counter.activeSessions variables in each JVM? If so, and short of keeping track of the session count externally (e.g. in a database), I wonder if this is even possible.
Is the A class meant to suggest a possible solution?
I think, static variables are not serializable. If we use serializable attribute, during session migration there is no guarantee that container calls writeObject() and readObject(). We don't need to use serializable attribute, container will take of that.
I would like to write session counter that works with session's migration.
Increment counter in sessionDidActivate() method of HttpSessionActivationListener' class.
Mike Mitchell wrote:Hi Lukas,
Is the A class meant to suggest a possible solution?
Yes, but Chinmaya said that there was no guarantee that container will invoke that methods.
Using listener will be better .
What do you think?
Static variables are not the part of serialization - they are class variables, not instance variables. But I explicitly write that value to the stream and then I read it back
If you want to keep track of the number of sessions in one JVM, then you can use SessionListener and SessionActivationListener together. Increase a counter when session is created or activated and decrease the counter if session is destroyed or passivated. If you want to know how many total sessions are there in all the JVMs, then you'll have to use some external help (like Database as Mike said)...