Connector:
public class Connector {
/**
* 主机地址
*/
private String host;
/**
* 端口号
*/
private int port;
/**
* 套接字
*/
private Socket skt;
private InputStream in;
private ObjectInputStream ois;
private OutputStream out;
private ObjectOutputStream oos;
/**
* 有参构造方法
*
* @param host
* @param port
*/
public Connector(String host, int port) {
this.host = host;
this.port = port;
}
/**
* 发送对象
*
* @param obj
* @throws IOException
*/
public void send(Object obj) throws IOException {
this.connect();
oos.writeObject(obj);
}
/**
* 接受对象
*
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
public Object receive() throws IOException, ClassNotFoundException {
return ois.readObject();
}
/**
* 连接远程服务器
*
* @throws UnknownHostException
* @throws IOException
*/
public void connect() throws UnknownHostException, IOException {
connect(host, port);
}
/**
* 建立于远程服务器的连接
*
* @param host
* @param port
* @throws UnknownHostException
* @throws IOException
*/
public void connect(String host, int port) throws UnknownHostException,
IOException {
skt = new Socket(host, port);
out = skt.getOutputStream();
oos = new ObjectOutputStream(out);
in = skt.getInputStream();
ois = new ObjectInputStream(in);
}
public void close() {
try {
System.out.println("关闭与远程的连接");
} catch (Exception e) {
// TODO: handle exception
} finally {
try {
ois.close();
oos.close();
skt.close();
} catch (IOException e) {
System.out.println("关闭远程连接过程中出现错误了" + e);
e.printStackTrace();
}
}
}
}
静态代理类HelloServiceProxy:
/**
* 静态代理类
*
*/
public class HelloServiceProxy implements HelloService {
private String host;
private int port;
public HelloServiceProxy(String host, int port) {
this.host = host;
this.port = port;
}
/**
* 远程调用echo方法
*/
public String echo(String msg) {
Object result = null;
Connector conn = null;
try {
conn = new Connector(host, port);
Call call = new Call("com.hou.service.impl.HelloServiceImpl", "echo",
new Class[] { String.class }, new Object[] { msg });
// 发送数据
conn.send(call);
// 接受数据
call = (Call) conn.receive();
result = call.getResult();
if (result instanceof Throwable)
System.out.println("出现异常消息了" + result);
} catch (Exception e) {
System.out.println("出现异常消息了" + e);
} finally {
if (conn != null) {
conn.close();
}
}
return (String) result;
}
/**
* 远程调用getTime方法
*/
public Date getTime() {
Connector conn = null;
Object result = null;
try {
conn = new Connector(host, port);
Call call = new Call("com.hou.service.impl.HelloServiceImpl", "getTime",
new Class[] {}, new Object[] {});
conn.send(call);
call = (Call) conn.receive();
result = call.getResult();
if (result instanceof Throwable) {
System.out.println("出现异常消息了" + result);
}
} catch (Exception e) {
// TODO: handle exception
} finally {
if (conn != null) {
conn.close();
}
}
return (Date) result;
}
}
Call发转工具类:
/**
* 反转工具类
*
*
*/
public class Call implements Serializable{
/**
* 表示类名或者接口名
*/
private String className;
/**
* 表示方法名
*/
private String methodName;
/**
* 表示方法参数类型
*/
private Class[] paramType;
/**
* 表示参数值
*/
private Object[] params;
/**
* 表示方法执行结果 如果方法正常执行,则返回正常值,如果抛出异常则返回该异常
*/
private Object result;
/**
* 无参构造方法
*/
public Call() {
}
/**
* 有参构造方法
*
* @param clasName
* @param methodName
* @param paramType
* @param params
*/
public Call(String className, String methodName, Class[] paramType,
Object[] params) {
this.className = className;
this.methodName = methodName;
this.paramType = paramType;
this.params = params;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getMethodName() {
return methodName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
public Class[] getParamType() {
return paramType;
}
public void setParamType(Class[] paramType) {
this.paramType = paramType;
}
public Object[] getParams() {
return params;
}
public void setParams(Object[] params) {
this.params = params;
}
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
@Override
public String toString() {
return "ClassName=" + className + ";methodName=" + methodName;
}
}
Dynamic Class:
/**
* 动态代理类
*
*/
public class ProxyFactory {
public static Object getProxy(final Class classType, final String host,
final int port) {
/**
* 匿名内部类
*/
InvocationHandler handler = new InvocationHandler() {
Object result = null;
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Connector conn = null;
try {
conn = new Connector(host, port);
Call call = new Call(classType.getName(), method.getName(),
method.getParameterTypes(), args);
conn.send(call);
call = (Call) conn.receive();
result = call.getResult();
if (result instanceof Throwable) {
System.out.println("出现异常了" + result);
}
} catch (Exception e) {
// TODO: handle exception
} finally {
if (conn != null) {
conn.close();
}
}
return result;
}
};
return Proxy.newProxyInstance(classType.getClassLoader(),
new Class[] { classType }, handler);
}
}
Client:
/**
* 客户端
*
*
*/
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
// 静态代理测试
/*HelloService helloService1 = new HelloServiceProxy("localhost", 8000);
System.out.println("测试静态代理--->" + helloService1.echo("猴哥"));
System.out.println("测试静态代理--->" + helloService1.getTime());*/
// 动态代理
HelloService helloService2 = (HelloService) ProxyFactory.getProxy(
HelloService.class, "localhost", 8000);
System.out.println("测试动态代理--->" + helloService2.echo("猴哥哥"));
System.out.println("测试动态代理--->" + helloService2.getTime());
}
}
RMI远程调用原理就是利用java soket+Proxy来实现远程调用的。
public class Connector {
/**
* 主机地址
*/
private String host;
/**
* 端口号
*/
private int port;
/**
* 套接字
*/
private Socket skt;
private InputStream in;
private ObjectInputStream ois;
private OutputStream out;
private ObjectOutputStream oos;
/**
* 有参构造方法
*
* @param host
* @param port
*/
public Connector(String host, int port) {
this.host = host;
this.port = port;
}
/**
* 发送对象
*
* @param obj
* @throws IOException
*/
public void send(Object obj) throws IOException {
this.connect();
oos.writeObject(obj);
}
/**
* 接受对象
*
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
public Object receive() throws IOException, ClassNotFoundException {
return ois.readObject();
}
/**
* 连接远程服务器
*
* @throws UnknownHostException
* @throws IOException
*/
public void connect() throws UnknownHostException, IOException {
connect(host, port);
}
/**
* 建立于远程服务器的连接
*
* @param host
* @param port
* @throws UnknownHostException
* @throws IOException
*/
public void connect(String host, int port) throws UnknownHostException,
IOException {
skt = new Socket(host, port);
out = skt.getOutputStream();
oos = new ObjectOutputStream(out);
in = skt.getInputStream();
ois = new ObjectInputStream(in);
}
public void close() {
try {
System.out.println("关闭与远程的连接");
} catch (Exception e) {
// TODO: handle exception
} finally {
try {
ois.close();
oos.close();
skt.close();
} catch (IOException e) {
System.out.println("关闭远程连接过程中出现错误了" + e);
e.printStackTrace();
}
}
}
}
静态代理类HelloServiceProxy:
/**
* 静态代理类
*
*/
public class HelloServiceProxy implements HelloService {
private String host;
private int port;
public HelloServiceProxy(String host, int port) {
this.host = host;
this.port = port;
}
/**
* 远程调用echo方法
*/
public String echo(String msg) {
Object result = null;
Connector conn = null;
try {
conn = new Connector(host, port);
Call call = new Call("com.hou.service.impl.HelloServiceImpl", "echo",
new Class[] { String.class }, new Object[] { msg });
// 发送数据
conn.send(call);
// 接受数据
call = (Call) conn.receive();
result = call.getResult();
if (result instanceof Throwable)
System.out.println("出现异常消息了" + result);
} catch (Exception e) {
System.out.println("出现异常消息了" + e);
} finally {
if (conn != null) {
conn.close();
}
}
return (String) result;
}
/**
* 远程调用getTime方法
*/
public Date getTime() {
Connector conn = null;
Object result = null;
try {
conn = new Connector(host, port);
Call call = new Call("com.hou.service.impl.HelloServiceImpl", "getTime",
new Class[] {}, new Object[] {});
conn.send(call);
call = (Call) conn.receive();
result = call.getResult();
if (result instanceof Throwable) {
System.out.println("出现异常消息了" + result);
}
} catch (Exception e) {
// TODO: handle exception
} finally {
if (conn != null) {
conn.close();
}
}
return (Date) result;
}
}
Call发转工具类:
/**
* 反转工具类
*
*
*/
public class Call implements Serializable{
/**
* 表示类名或者接口名
*/
private String className;
/**
* 表示方法名
*/
private String methodName;
/**
* 表示方法参数类型
*/
private Class[] paramType;
/**
* 表示参数值
*/
private Object[] params;
/**
* 表示方法执行结果 如果方法正常执行,则返回正常值,如果抛出异常则返回该异常
*/
private Object result;
/**
* 无参构造方法
*/
public Call() {
}
/**
* 有参构造方法
*
* @param clasName
* @param methodName
* @param paramType
* @param params
*/
public Call(String className, String methodName, Class[] paramType,
Object[] params) {
this.className = className;
this.methodName = methodName;
this.paramType = paramType;
this.params = params;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getMethodName() {
return methodName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
public Class[] getParamType() {
return paramType;
}
public void setParamType(Class[] paramType) {
this.paramType = paramType;
}
public Object[] getParams() {
return params;
}
public void setParams(Object[] params) {
this.params = params;
}
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
@Override
public String toString() {
return "ClassName=" + className + ";methodName=" + methodName;
}
}
Dynamic Class:
/**
* 动态代理类
*
*/
public class ProxyFactory {
public static Object getProxy(final Class classType, final String host,
final int port) {
/**
* 匿名内部类
*/
InvocationHandler handler = new InvocationHandler() {
Object result = null;
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Connector conn = null;
try {
conn = new Connector(host, port);
Call call = new Call(classType.getName(), method.getName(),
method.getParameterTypes(), args);
conn.send(call);
call = (Call) conn.receive();
result = call.getResult();
if (result instanceof Throwable) {
System.out.println("出现异常了" + result);
}
} catch (Exception e) {
// TODO: handle exception
} finally {
if (conn != null) {
conn.close();
}
}
return result;
}
};
return Proxy.newProxyInstance(classType.getClassLoader(),
new Class[] { classType }, handler);
}
}
Client:
/**
* 客户端
*
*
*/
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
// 静态代理测试
/*HelloService helloService1 = new HelloServiceProxy("localhost", 8000);
System.out.println("测试静态代理--->" + helloService1.echo("猴哥"));
System.out.println("测试静态代理--->" + helloService1.getTime());*/
// 动态代理
HelloService helloService2 = (HelloService) ProxyFactory.getProxy(
HelloService.class, "localhost", 8000);
System.out.println("测试动态代理--->" + helloService2.echo("猴哥哥"));
System.out.println("测试动态代理--->" + helloService2.getTime());
}
}
RMI远程调用原理就是利用java soket+Proxy来实现远程调用的。