hadoop中的RPC是个很重要的机制,datanode与namenode之间的通信通过RPC方式在实现,其中java的动态代理与反射等相关机制在里面扮演了重要的角色。下午花了点时间,弄了个简单的代理实例,按照个人的理解来粗讲下的proxy的内部实现。
一.相关类及其方法:
java.lang.reflect.Proxy: Proxy 提供用于创建动态代理类和实例的静态方法.
newProxyInstance(): 返回一个指定接口的代理类实例,该接口可以将方法调用指派到指定的调用处理程序
java.lang.reflect.InvocationHandler: InvocationHandler 是代理实例的调用处理程序 实现的接口,主要是帮助proxy来完成调用转发,实现目标类的接口调用。
invoke() : 在代理实例上处理方法调用并返回结果。在与方法关联的代理实例上调用方法时,将在调用处理程序上调用此方法。
以上的相关说明 可以直接查看jdk下面的相关类,比较清楚。
二.测试的源代码:
目标类 及需要调用的接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package test;
public interface Server {
public class NameServer implements Server { private String name = null;
|
实现调用转发的InvocationHandler类:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package test;
import java.lang.reflect.InvocationHandler; import org.apache.ojb.jdo.jdoql.ThisExpression; public class MyInvocationHandler implements InvocationHandler { Object target = null; |
客户端:
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import test.MyInvocationHandler;
import test.NameServer;
public class MyProxyTest {
public static void main(String[] args)
throws ClassCastException, ClassNotFoundException,
IllegalAccessException,InstantiationException{
Class<?> nameServerClass = (Class<NameServer>)Class.forName("test.NameServer");
NameServer nameServerImpl = (NameServer)nameServerClass.newInstance(); //目标接口实例
//创建一个InvocationHandler实例给proxy,用来转发调用
Class<MyInvocationHandler> invocationHandlerClass =
(Class<MyInvocationHandler>)Class.forName("test.MyInvocationHandler");
MyInvocationHandler invocationHandler = (MyInvocationHandler)invocationHandlerClass.newInstance();
invocationHandler.setTarget(nameServerImpl);
//MyInvocationHandler invocationHandler = new MyInvocationHandler(nameServerImpl);
//创建nameserver的代理类
Server serverProxy = (Server)Proxy.newProxyInstance(
nameServerClass.getClassLoader(),
// new Class[]{Server.class},
nameServerImpl.getClass().getInterfaces(),
invocationHandler
);
serverProxy.setName("Michael");
String name = serverProxy.getName();
System.out.println("name: " + name);
}
}
三.执行结果:
begin to call method: setName
method: setName has been called!
begin to call method: getName
method: getName has been called!
name: Michael
四.机制分析:
Proxy.(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)做了以下几件事.
(1)根据参数loader和interfaces调用方法 getProxyClass(loader, interfaces)创建代理类$Proxy.
$Proxy0类实现了interfaces的接口,并继承了Proxy类.
(2)实例化$Proxy0并在构造方法中把BusinessHandler传过去,接着$Proxy0调用父类Proxy的构造器,为h赋值,如下:
1
2
3
4
5
6
7
|
class
Proxy{
InvocationHandler h=
null
;
protected
Proxy(InvocationHandler h) {
this
.h = h;
}
...
public static Object newProxyInstance(ClassLoader loader,
}
}
|
关键在于$Proxy0它的内部实现机制,下面是本测试实例中$Proxy0的内部代码,通过观察$Proxy0,代理实现的机制一目了然:
import
java.lang.reflect.InvocationHandler;
import
java.lang.reflect.Method;
import
java.lang.reflect.Proxy;
import
java.lang.reflect.UndeclaredThrowableException;
public
final
class
$Proxy0
extends
Proxy
implements
Server
{
private
static
Method m1;
private
static
Method m0;
private
static
Method m3;
private
static
Method m2;
private static Method m5;
static
{
try
{
m1 = Class.forName(
"java.lang.Object"
).getMethod(
"equals"
,
new
Class[] { Class.forName(
"java.lang.Object"
) });
m0 = Class.forName(
"java.lang.Object"
).getMethod(
"hashCode"
,
new
Class[
0
]);
m3 = Class.forName(
"test.Server"
).getMethod(
"setName"
,
new
Class[
0
]);
m2 = Class.forName(
"java.lang.Object"
).getMethod(
"toString"
,
new
Class[
0
]);
m5 = Class.forName("test.Server").getMethod("getName",
new Class[0]);
}
catch
(NoSuchMethodException nosuchmethodexception) {
throw
new
NoSuchMethodError(nosuchmethodexception.getMessage());
}
catch
(ClassNotFoundException classnotfoundexception) {
throw
new
NoClassDefFoundError(classnotfoundexception.getMessage());
}
}
public
$Proxy0(InvocationHandler invocationhandler) {
super
(invocationhandler);
}
@Override
public
final
boolean
equals(Object obj) {
try
{
return
((Boolean)
super
.h.invoke(
this
, m1,
new
Object[] { obj }))
.booleanValue();
}
catch
(Throwable throwable) {
throw
new
UndeclaredThrowableException(throwable);
}
}
@Override
public
final
int
hashCode() {
try
{
return
((Integer)
super
.h.invoke(
this
, m0,
null
)).intValue();
}
catch
(Throwable throwable) {
throw
new
UndeclaredThrowableException(throwable);
}
}
public
final
void
setName
(String arg) {
try
{
super
.h.invoke(
this
, m3, arg
);
return
;
}
catch
(Error e) {
}
catch
(Throwable throwable) {
throw
new
UndeclaredThrowableException(throwable);
}
}
@Override
public
final
String toString() {
try
{
return
(String)
super
.h.invoke(
this
, m2,
null
);
}
catch
(Throwable throwable) {
throw
new
UndeclaredThrowableException(throwable);
}
}
public final String getName() {
try{
retrun (String) super.h.invoke(this, m5, null);
} catch(Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
}
接着把得到的$Proxy0实例强制转换成Server.
当执行serverProxy.setName()方法时,就调用了$Proxy0类中的setName()方法.
在setName方法中,调用父类Proxy中的h($Proxy0的构造器初始化为我们定义的invocationhanddler)的invoke()方法.
即InvocationHandler.invoke(); Proxy.newProxyInstance(..)方法执行时生成了$Proxy0的内存字节码文件并return出来赋给了
serverProxy,强制转化成了server接口,同时$Proxy0也实现了server接口中的所有方法.
下面是Proxy类中的关于$Proxy0的生成:
public static Class<?> getProxyClass(ClassLoader loader,
Class<?>... interfaces)
throws IllegalArgumentException
{
..........
long num;
synchronized (nextUniqueNumberLock) {
num = nextUniqueNumber++;
}
String proxyName = proxyPkg + proxyClassNamePrefix + num;
/*
* Verify that the class loader hasn't already
* defined a class with the chosen name.
*/
/*
* Generate the specified proxy class.
*/
byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
proxyName, interfaces);
try {
proxyClass = defineClass0(loader, proxyName, //这里就得到了$Proxy0的实现,从Proxy继承,并实现了用户目标接口Server
proxyClassFile, 0, proxyClassFile.length);
}......
return proxyClass;
}
所以JAVA的动态代理的关键就在Proxy.newProxyInstance(..)方法执行时生成了$Proxy0的内存字节码以及JDK的反射机制!
1.产生代理类$Proxy0类
执行了Proxy.newProxyInstance(ClassLoader loader, Class[] interfaces, InvocationHandler h)
将产生$Proxy0类,它继承Proxy对象,并根据第二个参数,实现了被代理类的所有接口,自然就可以生成接口要实现的所有方法了(这时候会重写hashcode,toString和equals三个方法),但是还没有具体的实现体;
2. 将代理类$Proxy0类加载到JVM中
这时候是根据Proxy.newProxyInstance(ClassLoader loader, Class[] interfaces, InvocationHandler h)它的第一个参数----就是被代理类的类加载器,把当前的代理类加载到JVM中
3. 创建代理类$Proxy0类的对象
调用的$Proxy0类的$Proxy0(InvocationHandler)构造函数,生成$Proxy0类的对象
参数就是Proxy.newProxyInstance(ClassLoader loader, Class[] interfaces, InvocationHandler h)它的第三个参数
这个参数就是我们自己实现的InvocationHandler对象,我们知道InvocationHandler对象中组合加入了代理类代理的接口类的实现类;所以,$Proxy0对象调用所有要实现的接口的方法,都会调用InvocationHandler对象的invoke()方法实现;
4. 生成代理类的class byte
动态代理生成的都是二进制class字节码