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.
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)...