my dog learned polymorphism*
The moose likes Tomcat and the fly likes Retrieving all active sessions at shutdown. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Retrieving all active sessions at shutdown." Watch "Retrieving all active sessions at shutdown." New topic
Author

Retrieving all active sessions at shutdown.

Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
Hello everyone,

I need to implement an enhancement in our web application whereby, all active sessions are invalidated when the Tomcat server, hosting our app, is shutdown.
By searching around, I've discovered that I can utilize the HTTPSessionListener [using a hashtable] to implement my requirement. However, our app will be mostly operating under a high load scenario [users > 1 million], so the overhead of maintaining the hashtable can be quite prohibitive. And also, from prior experience we've seen that the integrity of the data within the hashtable can run into inconsistencies, if the logic around preserving its state is not synchronized properly. This resulted in a performance hit we couldn't really afford.

I was wondering, since Tomcat already does the wonderful job of session maintenance, is there a way our app can leverage any of Tomcat's underlying classes to retrieve all the active sessions at Tomcat shutdown?
I came across an interface - Manager - within Catalina.jar. This interface can apparently be used to retrieve the number of active sessions using a method - findSessions(), but I'm not quite aware of where to find an implementation class for it, and or write my own implementation, so that I can probably connect it with my web app.

Appreciate any help.
[ September 17, 2008: Message edited by: Anirvan Majumdar ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60738
    
  65

Ummm, since the server is about to shut down, why do you need to do anything to the sessions?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
I guess it'll be correct to say that I need to know what has been the browsing history of the user through the portal. The efficiency of the portal is basically dependent upon understanding user behaviour. Say for example, the way Google maintains a web history of your searches. That way, it can not only direct a better bunch of search results every next time you search, but also allows Google to better serve their third party associates through accurate targeting.

Keeping this in mind, we need to be sure that if the server is ever shutdown, for say maintenance tasks, we do serialize/persist this information about active user sessions.

So any ways to go forward?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12756
    
    5
I think this is all configurable in Tomcat - search for "session" in server.xml for example. A google search for "tomcat session serialize" turned up lots of stuff too.

As near as I can tell, my Tomcat server serializes active sessions on shutdown by default.

Bill
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
Thanks William.

Your suggestion was definitely worth a watch and I could make it work too. However, the problem I face now is that - Tomcat serializes all active sessions at shutdown, only if cookies are enabled and Tomcat can do session management using the cookies retrieved from the browsers/clients. But if session management is achieved through the means of URL rewriting [jsesssionId], then Tomcat can't quite manage to do the serialization/de-serialization job properly.

Any idea about this scenario?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12756
    
    5
But if session management is achieved through the means of URL rewriting [jsesssionId], then Tomcat can't quite manage to do the serialization/de-serialization job properly.


I know of no way in which session management is dependent on how the session id is recovered. I am betting there is another reason for your de-serialization problem. Exactly what are you observing?

Bill
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
This is what I did:
----------------------

In the context.xml for my webapp, I had the following:


Note that I first tried this with the cookies attrib set to true. Then I set a session-timeout in my webapp's web.xml for 1 minute. I also put in a SessionListener so that I could print a message when a session got created and destroyed.

Thereafter, I started my webapp, created 2 sessions and shut down Tomcat cleanly. I saw that SESSIONS.ser file got created. Then I re-started Tomcat and waited for the session-timeout to occur. After a minute or so I saw that both the sessions got destroyed.

Since in our main webapp, we turn off cookies and rely on JSSESSIONID for session management through URL rewriting, I changed the cookie attrib in the context.xml to false. I proceeded with the same experiment. I even saw that the SESSIONS.ser file was created when Tomcat was shutdown, but upon re-starting, the console messages indicating a session destroy never appear.

That pretty much sums up what I've done :-/
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12756
    
    5
Sounds like there may be an interaction between cookies="false" and recovery of serialized sessions after all.

Perhaps you will have to create another mechanism for recording the browsing history?

Bill
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
Alas, that's what it has to be. Perhaps I'll try and figure out a way to determine what action of a user would constitute an end of session.

Nonetheless, thanks a lot Bill. I at least got to learn something new about the behaviour of Tomcat :-]
Mike Gabros
Greenhorn

Joined: Sep 29, 2008
Posts: 4
Hi Anirvan,
Can you post the code, or let me know how did you accessed StandardManager from within your portal, I am pretty new to tomcat, we are trying to get all active sessions and print them, I would really appreciate your help.

Thanks,

Mike
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
Hello Mike,

There are 2 ways to go about this:

1] This method would be of help if your application is not going to handle a huge traffic [> 1 million users] on a daily basis. What you can do is, write a SessionListener for your webapp and maintain a concurrent hashmap in it. Into this hashmap you put in every new session that is created and remove any session that is destroyed. Thereafter, you can write a ContextListener which can access the concurrent hashmap of the SessionListener during Tomcat shutdown and print all the entries, which would actually be all active sessions, at that point.

2] The more circuitous way of accomplishing this would be for you to write a class which would extend Tomcat's StandardManager. In your class, you'll need to override the "unLoad" method. In this, invoke the "findSessions" method which would return an array of all active sessions for the current manager. Then you can do what you want with these sessions. However, do remember to JAR this sub-class and place the JAR file in the "/common/endorsed" folder of your Tomcat installation. Create a "context.xml" file in the META-INF folder of your webapp, and put in the following code into it:
---------------------------------------------------------------------------


Depending upon your requirements, choose the method which fits best. In my opinion, the StandardManager sub-classing method is a bit cumbersome, because you'll need to place the JAR file in the Tomcat installation directory wherever you have to install your webapp. That may not work well in all circumstances.

Hope this helps!
Mike Gabros
Greenhorn

Joined: Sep 29, 2008
Posts: 4
Thanks Anirvan that cleared lots of my doubts, still I have some silly questions which I hope you can help me with.
1. StandardManager class is in {TOMCAT_INSTALLION}/server/lib/catalina.jar what do I have to do in order my web app can access it. Do I have to copy catalina.jar in {MY_WEB_APP}/WEB-INF/lib ?
2. My application is just simple I dont have to persist sessions all I need to do is print all active sessions when server is up and running, so I dont need to extend StandardManager, right ? and context.xml should be like this
<Context path="/webApp" cookies="true" > <Manager className="org.apache.catalina.session.StandardManager"/> </Context>
3. How to I get access of StandardManager in JSP
is it
StandardManager sm = new StandardManager();
or tomcat creates an instance of it and you just call something like
sm.getInfo(); without create instance of the class like above.

thanks for being patient I really appreciate your help.

Mike
[ September 30, 2008: Message edited by: Mike Gabros ]
Anirvan Majumdar
Ranch Hand

Joined: Feb 22, 2005
Posts: 261
Hello Mike,

I'm quite sure that intantiating the StandardManager isn't really that straightforward. Somehow you'll need to get a handle on the container and then, from there work your way upwards towards retrieving a reference to the StandardManager. Unfortunately, I'm just as clueless about this as you are. I did try and search high and dry for a way to go about it, but the best I managed to do was to dig up this 7 years old forum post from the Apache archives. What the person in this post tries to do with a Tomcat 3.2.1 is pretty much what you want. However, it appears that most of the classes used were part of a JAR file - tomcat-core, which is no longer part of any of the recent Tomcat distros. Some of the classes I did manage to find in catalina.jar, but some of them remained untraceable.

I'm sorry but at this point this is the best I can do to assist you in this matter. In case I come across anything which might help you, I'll surely update this thread.
Mike Gabros
Greenhorn

Joined: Sep 29, 2008
Posts: 4
Thanks Anirvan for your help, I tried searching too, but didn't find anything, I will try and open new thread for this and see anyone can help me in this forum. Again thanks for your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Retrieving all active sessions at shutdown.
 
Similar Threads
Enumerate active sessions
Checking active sessions connected to weblogic
Tomcat session logistics
Getting active session from the session id
How to retreive all active sessions from an web app