aspose file tools*
The moose likes Servlets and the fly likes Best way tol load and access application scoped collections? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Best way tol load and access application scoped collections?" Watch "Best way tol load and access application scoped collections?" New topic
Author

Best way tol load and access application scoped collections?

David Heffington
Greenhorn

Joined: Mar 17, 2007
Posts: 17
Hi-
I need to load some collections with values used to populate pull-down selection menus throughout the web app. I am sure the most efficient approach is to make these collections accessible from application scope, thus eliminating the need to load the collections for each sessions (or worse, any request requiring the collection). I know this can be done with static variables in a class (and just test to see if the variables are loaded or not whenever they are accessed), but I would like use a better method for handling this.

ps. I hope this post is in the right place
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
Are the values constant ? Will they be changed ?

If they won't be changed, then as you said, use statics.

Otherwise, if they are going to be populated from some data-source (database as an example) i suggest the Ajax technology to be used.

Regards,


Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
David Heffington
Greenhorn

Joined: Mar 17, 2007
Posts: 17
The values will be constant most of the time. However, the possibility exists that they could change in the database. When they do change in the database, there will need to be a 'refresh' mechanism in place that can be manually invoked in the event it is needed (i.e. for the static var approch mentioned above, there could be an accompanying static var (such as 'static boolean isLoaded') that could be resest to false by some process and then tested by the next request needing the collections, triggering the reload of the variables). Just wondering if there was a better way than the use of static vars.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

If you want to fetch these values from the DB every time they are need -- thereby guaranteeing that they are always up to date -- you'd just handle that (with proper delegation to the Mode, of course) in the page controller for the JSP.

However, it sounds as if you;d rather cache the values at app startup in order to avoid the overhead of continually grabbing the values from the DB.

In that case, I'd use a context listener to get the values from the DB at app startup and store them in the application context (servletContext) so that they can be easily obtained while the app is running.

The interesting question then is: what do you do if the DB changes?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David Heffington
Greenhorn

Joined: Mar 17, 2007
Posts: 17
I would much rather use the listener approach. But, am I then stuck with restarting the application every time a refresh is needed? (even though it would be rare)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

Perhaps not.

If an update is rare, what I'd probably do is have an administrative command that I could send to the web app that would reload the values from the DB.
[ April 17, 2008: Message edited by: Bear Bibeault ]
David Heffington
Greenhorn

Joined: Mar 17, 2007
Posts: 17
From your response, I think you are referring to a separate utility to reload the existing collection objects on command. I guess I could include a button (just add a button ) that only adminitrators could see with something like 'reload cache'.

That should work.

Thanks!!!
David Heffington
Greenhorn

Joined: Mar 17, 2007
Posts: 17
Wait a minute. What if multiple srvers are being used for load balancing. Would the 'reload' button mentioned above only reset the cache on the server the user happens to be on?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

Sure that's possible. In fact, some of my apps have an entire admin console sub-system.

But it's also possible for really simple stuff to just have a URL you can hit that executes an "admin" servlet to perform the administrative action.
David Heffington
Greenhorn

Joined: Mar 17, 2007
Posts: 17
That's true. The utility to perform the updates could point to the specific URLs for each server in the cluster. I guess this URL list could be maintained as init-params in the DD as well.

So, would you write a separate servlet for admin functions, point to the explicit URL for each admin servlet, and do the refresh using something like this: �getServletContext().setAttribute( �list�, refreshedCacheObject )� ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best way tol load and access application scoped collections?