引自-http://zeroturnaround.com/rebellabs/reloading-objects-classes-classloaders/
-來自JRebel的研發公司zeroturnaround
________________________________________________________________
Reloading Java Classes 101: Objects, Classes and ClassLoaders
Welcome to Turnaround article series from ZeroTurnaround.
In this article we will review how to reload a Java class using a dynamic classloader. To get there we’ll see how objects, classes and classloaders are tied to each other and the process required to make changes. We begin with a bird’s eye view of the problem, explains the reloading process, and then proceed to a specific example to illustrate typical problems and solutions. Other articles in the series include:
- RJC101: Objects, Classes and ClassLoaders
- RJC201: How do Classloader leaks happen?
- RJC301: Classloaders in Web Development — Tomcat, GlassFish, OSGi, Tapestry 5 and so on
- RJC401: HotSwap and JRebel — Behind the Scenes
- RJC501: How Much Does Turnaround Cost?
A Bird’s Eye View
The first thing to understand when talking about reloading Java code is the relation between classes and objects. All Java code is associated with methods contained in classes. Simplified, you can think of a class as a collection of methods, that receive “this” as the first argument. The class with all its methods is loaded into memory and receives a unique identity. In the Java API this identity is represented by an instance of java.lang.Class
that you can access using theMyObject.class
expression.
Every object created gets a reference to this identity accessible through the Object.getClass()
method. When a method is called on an object, the JVM consults the class reference and calls the method of that particular class. That is, when you call mo.method()
(where mo
is an instance of MyObject
), then the JVM will call mo.getClass().getDeclaredMethod("method").invoke(mo)
(this is not what the JVM actually does, but the result is the same).
Every Class
object is in turn associated with its classloader (MyObject.class.getClassLoader()
). The main role of the class loader is to define a class scope — where the class is visible and where it isn’t. This scoping allows for classes with the same name to exist as long as they are loaded in different classloaders. It also allows loading a newer version of the class in a different classloader.
The main problem with code reloading in Java is that although you can load a new version of a class, it will get a completely different identity and the existing objects will keep referring the previous version of the class. So when a method is called on those objects it will execute the old version of the method.
Let’s assume that we load a new version of the MyObject
class. Let’s refer to the old version asMyObject_1
and to the new one as MyObject_2
. Let’s also assume that MyObject.method()
returns “1” in MyObject_1
and “2” in MyObject_2
. Now if mo2
is an instance of MyObject_2
:
mo.getClass() != mo2.getClass()
mo.getClass().getDeclaredMethod("method").invoke(mo)
!= mo2.getClass().getDeclaredMethod("method").invoke(mo2)mo.getClass().getDeclaredMethod("method").invoke(mo2)
throws aClassCastException
, because theClass
identities ofmo
andmo2
do no match.
This means that any useful solution must create a new instance of mo2
that is an exact copy ofmo
and replace all references to mo
with it. To understand how hard it is, remember the last time you had to change your phone number. It’s easy enough to change the number itself, but then you have to make sure that everyone you know will use the new number, which is quite a hassle. It’s just as difficult with objects (in fact, it’s actually impossible, unless you control the object creation yourself), and we’re talking about many objects that you must update at the same time.
Down and Dirty
Let’s see how this would look in code. Remember, what we’re trying to do here is load a newer version of a class, in a different classloader. We’ll use an Example
class that looks like this:
private int counter;
public String message() {
return "Version 1";
}
public int plusPlus() {
return counter++;
}
public int counter() {
return counter;
}
}
We’ll use a main()
method that will loop infinitely and print out the information from the Example
class. We’ll also need two instances of the Example
class: example1 that is created once in the beginning and example2 that is recreated on every roll of the loop:
private static IExample example1;
private static IExample example2;
public static void main(String[] args) {
example1 = ExampleFactory.newInstance();
while (true) {
example2 = ExampleFactory.newInstance();
System.out.println("1) " +
example1.message() + " = " + example1.plusPlus());
System.out.println("2) " +
example2.message() + " = " + example2.plusPlus());
System.out.println();
Thread.currentThread().sleep(3000);
}
}
}
IExample
is an interface with all the methods from Example
. This is necessary because we’ll be loading Example
in an isolated classloader, so Main
cannot use it directly (otherwise we’d get aClassCastException
).
String message();
int plusPlus();
}
From this example, you might be surprised to see how easy it is to create a dynamic class loader. If we remove the exception handling it boils down to this:
public static IExample newInstance() {
URLClassLoader tmp =
new URLClassLoader(new URL[] {getClassPath()}) {
public Class loadClass(String name) {
if ("example.Example".equals(name))
return findClass(name);
return super.loadClass(name);
}
};
return (IExample)
tmp.loadClass("example.Example").newInstance();
}
}
The method getClassPath()
for the purposes of this example could return the hardcoded classpath. However, in the full source code (available in the Resources section below) you can see how we can use the ClassLoader.getResource()
API to automate that.
Now let’s run Main.main
and see the output after waiting for a few loop rolls:
1) Version 1 = 3 2) Version 1 = 0 |
As expected, while the counter in the first instance is updated, the second stays at “0”. If we change the Example.message()
method to return “Version 2”. The output will change as follows:
1) Version 1 = 4 2) Version 2 = 0 |
As we can see, the first instance continues incrementing the counter, but uses the old version of the class to print out the version. The second instance class was updated, however all of the state is lost.
To remedy this, let’s try to reconstruct the state for the second instance. To do that we can just copy it from the previous iteration.
First we add a new copy()
method to Example
class (and corresponding interface method):
if (example != null)
counter = example.counter();
return this;
}
Next we update the line in the Main.main()
method that creates the second instance:
Now waiting for a few iterations yields:
1) Version 1 = 3 2) Version 1 = 3 |
And changing Example.message()
method to return “Version 2” yields:
1) Version 1 = 4 2) Version 2 = 4 |
As you can see even though it’s possible for the end user to see that the second instance is updated and all its state is preserved, it involves managing that state by hand. Unfortunately, there is no way in the Java API to just update the class of an existing object or even reliably copy its state, so we will always have to resort to complicated workarounds.
___________________________________________________________________________________________
筆記記錄:
1.可以存在兩個相同名字的class,只要他們是由不同的classloader加載即可,即允許用一個不同的classloader加載一個新的class
2.如果想達到hotswap的目的,新加載的class實例必須要復制舊的實例的狀態并且替換所有舊的實例的引用。非常麻煩,就好比你自己更換電話號碼很簡單,但是要確保所有人都知道你換了新號碼一樣.
3.如同上述的例子一樣,必須手動的管理新舊實例的狀態保存。沒有任何的Java API幫助我們替換一個舊的實例的class或者可靠的復制它的狀態,所以我們有時不得不求助于更加復雜的解決方案.
4.可參考web containers,OSGI,Tapestry 5,Grails等是如何面對上述reloading class遇到的問題.
5.注意接口IExample很重要.-->
附本人測試代碼:
/**
*
* 接口
*
* @author landon
* @since 1.8.0_25
*/
public interface IExample {
public String message();
public int plusPlus();
public int counter();
public IExample copy(IExample example);
}
import java.net.URL;
import java.net.URLClassLoader;
/**
* 每次都new一個classloader來加載class并且實例化
*
* @author landon
* @since 1.8.0_25
*/
public class ExampleFactory {
public static IExample newInstance() throws Exception {
// 必須用url形式,本地file需要:file:形式-如"file:\\E:\\github\\mavsplus-all\\mavsplus-examples\\src\\main\\resources\\rjc\\"
// 這里硬編碼-該目錄下只有編譯后的Example.class_即只有Example.class用urlClassloader加載,其他均默認加載
// 重點:一個比較容易犯錯的地方:是最后的路徑少了"\",否則會報ClassNotFoundException
URLClassLoader newClazzLoader = new URLClassLoader(new URL[] { new URL(
"file:\\E:\\github\\mavsplus-all\\mavsplus-examples\\src\\main\\resources\\rjc\\") }) {
// 注意:必須覆寫loadClass方法-否則還會默認加載默認classpath下的Example(因為用Eclipse編寫的Example類,默認targe目錄也是在classpath下)
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
if (name.equals("com.mavsplus.example.java.rjc.Example")) {
return findClass(name);
}
return super.loadClass(name);
}
};
return (IExample) newClazzLoader.loadClass("com.mavsplus.example.java.rjc.Example").newInstance();
}
}
/**
* 用來reload的class
*
* @author landon
* @since 1.8.0_25
*/
public class Example implements IExample {
private int counter;
@Override
public String message() {
// return "Version 1";
return "Version 2";
}
@Override
public int plusPlus() {
return counter++;
}
@Override
public int counter() {
return counter;
}
@Override
public IExample copy(IExample example) {
if (example != null) {
counter = example.counter();
}
return this;
}
}
import java.util.concurrent.TimeUnit;
/**
* <a href=
* "http://zeroturnaround.com/rebellabs/reloading-objects-classes-classloaders/"
* >rjc series 1</>
*
* 測試reloading java class
*
* <pre>
* ___________________________________________
* 1.默認輸出
* 1)Version 1 = 0
* 2)Version 1 = 0
*
* 1)Version 1 = 1
* 2)Version 1 = 0
*
* 1)Version 1 = 2
* 2)Version 1 = 0
*
* 1)Version 1 = 3
* 2)Version 1 = 0
*
* 1)Version 1 = 4
* 2)Version 1 = 0
* --->即每次reload時,example2的狀態一直是0.
* _____________________________________________
* 2.修改Example.java,將message方法修改,返回Version 2,保存(可以直接在程序運行的時候進行修改,因為運行的程序是用的是另一個地方的Example的clazz)
* ->將Example.class放到指定的加載位置
* 1)Version 1 = 5
* 2)Version 2 = 0
*
* 1)Version 1 = 6
* 2)Version 2 = 0
*
* 1)Version 1 = 7
* 2)Version 2 = 0
*
* 1)Version 1 = 8
* 2)Version 2 = 0
*
* 1)Version 1 = 9
* 2)Version 2 = 0
* 從輸出看_確實新加載了修改過的Example.class_因為message返回值變化了--->但是因為每次都是新加載并且實例化的,所以實例的狀態還是0
* ________________________________________________________________________________________
* 3.繼續修改Example.java,增加一個copy方法,用來復制舊的狀態數據并且修改main(exampel2的賦值)_
* 1)Version 1 = 7
* 2)Version 1 = 7
*
* 1)Version 1 = 8
* 2)Version 1 = 8
*
* 1)Version 1 = 9
* 2)Version 2 = 9
*
* 1)Version 1 = 10
* 2)Version 2 = 10
* 從輸出看,每次reload example2后,舊的實例狀態也保存了_純手動維護這個實例狀態
* </pre>
*
* @author landon
* @since 1.8.0_25
*/
public class RJCTest {
private static IExample example1;
private static IExample example2;
public static void main(String[] args) throws Exception {
example1 = ExampleFactory.newInstance();
while (true) {
// 第一次運行的時候,example2為null->第二次后則會克隆舊的對象狀態
example2 = ExampleFactory.newInstance().copy(example2);
System.out.println("1)" + example1.message() + " = " + example1.plusPlus());
System.out.println("2)" + example2.message() + " = " + example2.plusPlus());
System.out.println();
TimeUnit.SECONDS.sleep(5);
}
}
}