Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Global Bean for Servlets/JSP

 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where to post this question was a toss up between here and JSP so I opted for here. If it needs to be moved, well, you know the drill....
I have a resource file that I need loaded into a Bean that is made available for my entire web app. I was wondering what the best way to do this is and most efficient way.
Should I load it when the application server starts (Tomcat)?
Should I load it when the first request is made to the app?
Should I just read the resource file and not worry about a bean every time?
Thanks for any help. Depending on the response, I will probably have follow up questions on how to implemenet it.
 
Eelco den Heijer
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We had a similar issue a while ago. We had an XML file that was used to initialize a component. Because it took about 5 second to read the entire file and initialize the component, we chose to initialize it at startup of the controller servlet (instead of deferring it to the first request). The web app was loaded at start-up, the servlet was loaded at startup, and in the init of the servlet we initialized the component. The component was implemented as a singleton.
Hope this helps.
 
Phil Chuang
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with the initialization method, but I say don't make it a singleton, but rather stick it into the servlet context, that way, it'll be available to the whole application, and you won't be crippled by the singleton pattern.
 
Joe Pluta
Ranch Hand
Posts: 1376
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've found that almost invariably things that I thought only needed to get loaded when the application started eventually required the ability to be reloaded without shutting down the application.
Thus, I tend to use a lazy initialization technique, where if the information doesn't exist, it gets loaded. I'll put all the related information into a single object and then store a static reference to that object. When I access the information, if the reference is null, I load the information. With this technique, reloading is as simple as clearing the object reference.
Joe
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe, sounds like a singleton approach. The only problem I have with your solution is I don't want to have to check for that object everytime I need it. I want to expect it to just be there. That is why I would prefer to load it when the app starts and just put it in the ServletContext. Is this a bad idea?
 
Joe Pluta
Ranch Hand
Posts: 1376
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:
Joe, sounds like a singleton approach. The only problem I have with your solution is I don't want to have to check for that object everytime I need it. I want to expect it to just be there. That is why I would prefer to load it when the app starts and just put it in the ServletContext. Is this a bad idea?

A man after my own heart. Does it cost too much to check for the value every time? Of course, you could put in a try/catch for null pointer exception; I wonder if that would have the same overhead? Anyway, at this point it's purely an implementation question. From a design standpoint, I think the various techniques are equivalent.
Joe
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic