Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding files....dynamically. to the classpath.

 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Suppose we have a application accessing a properties file and the JVM starts and application starts using the properties file...

Now i want to modify the properties file ...

Does this require the restart of the application..

Are there any features like hot deployment of applications in servers which would allow me to dynamically access the modified version as the latest version??

Regards
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create a new ClassLoader every time you find a changed class file and want to load it. I looked at JUnit source code as an example of how to do it. Google for articles on custom class loaders; it's non-trivial to get completely right.

It's possible to get into weird places with custom class loaders, equals() or compareTo() methods, reflection, static variables, Singletons and such. If you run into trouble later, remember custom loaders might be involved.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stan ,

Thanks for responding ...

I dont have much idea about this....Suppose the server has already started and the app. has started ..Later the file /class has changed...

Now how can i load the changed .class file using the classloader...mechanism..

Should i write another program...for this???
 
Bakul Ghude
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think if you are asking about *.properties file and
if you made changes in that *.properties file then you must restart it for getting required changes that you have made in that file.
ok
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think if you are asking about *.properties file and
if you made changes in that *.properties file then you must restart it for getting required changes that you have made in that file.



Cant we make us of the class loader mechanisms here???
 
Ravindra Rawat
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i understand the problem correctly, you need to reload changes to your configuration etc from a properties file on the fly.

If this properties file is on the filesystem then you can check the timestamp of properties file using . If modified you can invoke the method configuring your application(synchronously) and blocking access to configuration till reloaded. This is fine in case changes are done in off peak hours for the application else handling is complex and better to reload the app, if possible.

It is similar to a file watchdog e.g http://logging.apache.org/log4j/docs/api/org/apache/log4j/helpers/FileWatchdog.html

Thanks
Ravindra
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW: Google for "java reload class" got a first hit called Dynamically Reloading a Modified Class. Search around and you'll find far more expert advice than mine.

Reloading configuration is a very cool capability. In my last big work project I made a "dashboard" with a button to reload part or all of configuration from a variety of sources. In my Wiki I edit the configuration pages through the application and the "save" operation publishes an event that different components listen for.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic