forked from JeffLi1993/java-core-learning-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRpcImporter.java
More file actions
60 lines (54 loc) · 2.5 KB
/
RpcImporter.java
File metadata and controls
60 lines (54 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package org.jee.rpc;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.InetSocketAddress;
import java.net.Socket;
/**
* 描述:Rpc本地服务代理类
* 1. 将本地接口调用转化为JDK的动态调用,在动态调用中实现接口的远程调用
* 2. 创建Socket客户端,根据制定地址连接远程服务提供者
* 3. 将远程服务调用所需的接口类,方法名,参数列表等编码后发送给服务提供者
* 4. 同步阻塞等待服务端返回应答,获取应答后返回
* Created by bysocket on 16/2/29.
*/
public class RpcImporter<S> {
public S importer(final Class<?> serviceClass, final InetSocketAddress address) {
// JDK动态代理,实现接口的远程调用
return (S) Proxy.newProxyInstance(serviceClass.getClassLoader(),
new Class<?>[]{serviceClass.getInterfaces()[0]},
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Socket socket = null;
ObjectOutputStream output = null;
ObjectInputStream input = null;
try {
// 连接远程服务提供者
socket = new Socket();
socket.connect(address);
// 对象输出流
output = new ObjectOutputStream(socket.getOutputStream());
output.writeUTF(serviceClass.getName());
output.writeUTF(method.getName());
output.writeObject(method.getParameterTypes());
output.writeObject(args);
input = new ObjectInputStream(socket.getInputStream());
return input.readObject();
} finally {
if (socket != null) {
socket.close();
}
if (output != null) {
output.close();
}
if (input != null) {
input.close();
}
}
}
});
}
}