Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Create and Compile Java class on application startup via Servlet

 
Sachin Deokar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am working on restructuring and migrating an old J2EE app to our new unix environment(deployed in Weblogic server). As part of the ant build process we have a target that calls a GenerateDBMetadata.java file which creates a DBMetadata.java class by connecting to the database and getting a list of all procedures and storing them in a hashmap. This class is then compiled as part of the build process. I am trying to take this out of the build script and was wondering if i could call the class GenerateDBMetadata.java in a Servlet on application startup in weblogic server and then create and compile DBMetadata.java class. Will <load-on-startup> element in web.xml work for this requirement? I have never done this before and would greatly appreciate your inputs.

Thanks in advance for your help. Please accept my apologies if I have posted this in wrong forum.

Regards,
Sachin
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
firstly don't use load-on-startup for anything that isn't related to bootstrapping the SErvlet. If you want to do it then use a ContextListener instead
Secondly, if you load and store the values, why hardcode it? Sure it can be hardcoded in the generated file from the build script, but on startup you can create an instance of a class for managing this information and then read and populate the data.
No need for dynamically creating and loading a class at runtime.
 
Sachin Deokar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I populate the data as static members of the class on application startup, how do I make sure that the object stays alive as long as the application is up? Any kinda cache mechanism?

- Sachin

David O'Meara wrote:firstly don't use load-on-startup for anything that isn't related to bootstrapping the SErvlet. If you want to do it then use a ContextListener instead
Secondly, if you load and store the values, why hardcode it? Sure it can be hardcoded in the generated file from the build script, but on startup you can create an instance of a class for managing this information and then read and populate the data.
No need for dynamically creating and loading a class at runtime.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Place it in application scope.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic