Thursday, January 24, 2008

Hot Code Changes

Hot Code Changes

ClassLoaders let you modify code on the fly without shutting down an application. New code is loaded with Class.forname. It creates new versions of the objects by reloading the classes for the objects with a ClassLoader. You then have both old and new versions of the same object, both with the same name, but with different data and different code. From the JVM’s point of view, a class loaded with a new ClassLoader is a different animal entirely from the one loaded with the standard ClassLoader even if the classes have the same name. You can manipulate both old an new objects with a common interface.

With a new ClassLoader, you can load a different version of a class. The old objects continue to use the old code. New objects use the new code. A given ClassLoader can load a given class only once. There is no need to unload a class. When the objects using it are no longer referenced, the class object itself, along with the code, will be garbage collected. The same classes, loaded by different class loaders are considered distinct classes. They are not instanceofs each other!

You will have to instantiate a new ClassLoader every time you have a new generation of classes. You can load all the replacement classes of a generation with the same ClassLoader. However, when you want to replace the replaced classes, you need a yet another new ClassLoader. You can do this with multiple instances of the same ClassLoader. You only need to write one ClassLoader, perhaps not even one, not one for each generation.

Have a look at the java.net.URLClassLoader. You may find for your given problem you don’t even have to write whole new ClassLoader, just instantiate one of Sun’s.

http://mindprod.com/jgloss/classloader.html

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home