aspose file tools*
The moose likes Tomcat and the fly likes Load objects on session upon restart Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Load objects on session upon restart" Watch "Load objects on session upon restart" New topic
Author

Load objects on session upon restart

Erap Estrada
Ranch Hand

Joined: Nov 08, 2006
Posts: 92
Hi. I have objects that I would like to load in session upon tomcat restart.

How would I do that?

Thanks in advance!


SCJP with 98% by...
Tarun Yadav
Ranch Hand

Joined: Sep 20, 2007
Posts: 134
Originally posted by Erap Estrada:
Hi. I have objects that I would like to load in session upon tomcat restart.

How would I do that?

Thanks in advance!


You can't do this, exactly, since when the container restarts, there aren't sessions in existence ( Someone please correct me if I'm wrong ). You'll have to wait for the client to participate in a session first.

You could use the sessionCreated() event of the HttpSessionListener to populate your session with appropriate data.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12681
    
    5
Tomcat can serialize sessions to disk so they will survive if you shutdown and restart. I suspect they wont be reloaded until needed.

What sort of objects are you needing to load? If we are talking about user specific data there are better ways of ensuring long term data storage than the session mechanism.

Bill


Java Resources at www.wbrogden.com
Erap Estrada
Ranch Hand

Joined: Nov 08, 2006
Posts: 92
Thanks Tarun.

I need to load an array of java beans.

Can you please lead me on how to properly implement this?

Thanks!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You want to load the array of beans when the session is created or when Tomcat starts/restarts?

When Tomcat initially starts, there won't be any sessions.
When it restarts, if your code allows it, it will deserialize any sessions that existed when it was shut down; but those sessions had to already exist.

If you can explain why you want to do this, maybe someone can suggest a better alternative (or, if we're misunderstanding you, maybe it would make things more clear for us).


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Erap Estrada
Ranch Hand

Joined: Nov 08, 2006
Posts: 92
My site list in the homepage like "Top 10 Retail Stores". This information is available to all users and changes about every other day.

What I did is, in my index page, I retrieve those data and save it to session. So whenever user access the data, it is readily available.

I just wonder if there's a better way to do it without invoking any of my web pages.

Thanks!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12681
    
    5
This information is available to all users and changes about every other day.


If the same information is available to all, it certainly doesn't need to be repeated in each user's session. Sessions are for data unique to each user.

This collection of bean objects could be loaded as a ServletContext attribute.

The ServletContextListener interface looks like a great candidate for a custom class to load the collection on startup and save it when the server is shutdown.

Bill
Erap Estrada
Ranch Hand

Joined: Nov 08, 2006
Posts: 92
I knew it. There's a better way. Thank you Bill. I'll research on that and I'll post questions on things that are unclear.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Load objects on session upon restart
 
Similar Threads
detecting delayed timer after server restart
web server/fmt JSTL and properties bundle caching
Need clarification in TimerService
When is destroy of servlets exaclty called
password and redirect question