Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Load objects on session upon restart

 
Erap Estrada
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I have objects that I would like to load in session upon tomcat restart.

How would I do that?

Thanks in advance!
 
Tarun Yadav
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Erap Estrada
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Erap Estrada
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic