aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes global session counter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "global session counter" Watch "global session counter" New topic
Author

global session counter

Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

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.



How can it be improved?

EDIT
What do you think about that:


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Mike Mitchell
Ranch Hand

Joined: May 28, 2008
Posts: 37
Hi Lukas,

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?

Thanks,
Mike


SCJP 5, SCWCD 5
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi Lukas.


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.
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

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
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Mike Mitchell wrote:
If so, and short of keeping track of the session count externally (e.g. in a database), I wonder if this is even possible.

It would be possible, but there would be performance hit.
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Can anybody help?
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: global session counter
 
Similar Threads
Invalidate Question
HttpSessionListener
Listeners automatically thread safe?
Count number of users accesing the web application
How to test HttpSessionListener